ElasticSearch 基础 1

ElasticSearch 基础
=============================== 索引创建 ==========================

1. RESTFUL API
API 基本格式: http://<ip>:<port>/<索引>/<类型>/<文档id>
常用的HTTP动词: GET/PUT/POST/DELETE

2. PUT 127.0.0.1:9200/people
json结构:
{
"settings":{
"numbers_of_shard": 3,
"numbers_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"name": {
"type": "text"
},
"country":{
"type": "keyword"
},
"age":{
"type": "integer"
},
"date": {
"type":"date",
"formate": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
},
"woman":{
}
}
}
settings: 指定索引配置
number_of_shard: 指定索引的分片数
number_ofreplicas: 指定索引的备份数
mappings: 索引的映射
man: 类型
properties: 属性的定义
国家不可切分 它是关键词
epoch_millis: 时间戳的格式

================================= 插入 ===============================
1. 指定文档id插入
2. 自动生成文档id插入

3. 指定文档id插入: PUT http://127.0.0.1:9200/people/man/1
JSON结构:
{
"name": "张波",
"country": "China",
"age": 26,
"date": "1992-04-23"
}

4. 自动生成文档id插入: POST http://127.0.0.1:9200/people/man/
JSON结构:
{
"name": "美男张波",
"country": "China",
"age": 20,
"date": "1998-04-23"
}
=========================== 更新 ==============================
1. POST http://127.0.0.1:9200/people/man/1/_update
{
"doc":{
"name": "谁是张波"
}
}
修改了id为1 的文档数据的属性name 修改成功了
2. 脚本修改
(1)
{
"script":{
"lang": "painless",
"inline":"ctx._source.age += 10"
}
}
(2)
{
"script":{
"lang": "painless",
"inline":"ctx._source.age = params.age"
"params": {
"age": 100
}
}
}
script: 指定脚本修改
lang: 指定脚本语言 ,painless指定脚本语言(es内置脚本语言 ) 也支持js, python等
inline: 指定脚本内容
ctx: 代表es上下文
_source: 代表es文档
执行的结果是age年龄增加了10
params: 参数修改

================================= 删除 ================================

1. 删除文档 DELETE http://127.0.0.1:9200/people/man/1
2. 删除索引 DELETE http://127.0.0.1:9200/people

注意: 删除操作需要谨慎

================================= 查询 =================================

======== 简单查询 =========
POST http://127.0.0.1:9200/book/_search
{
"query":{
"match_all":{
}
},
"from": 1,
"size": 1
}
match_all: 查询所有数据
返回结果:
took: 花费时间 ms
hits:响应的全部结果
total: 数据条数,
默认返回10条数据
from: 指定从哪里返回
size: 返回的数据条数

========= 条件查询 ========
{
"query":{
"match":{
"title": "ElasticSearch"
}
},
"sort":{
"publish_time": {"order": "desc"}
}
}
match: 关键词查询,返回title包含ElasticSearch的文档
publish_time: 降序排序

======== 聚合查询 ==========
{
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
},
"group_by_publish_time": {
"terms": {
"field":"publish_time"
}
}
}
}
aggs: 聚合查询关键词
group_by_word_count: 自定义(聚合条件的名字)
terms: 关键词
field: 指定聚合字段
返回的聚合信息
{
"key": 1000,
"doc_count": 5
}
{
"aggs": {
"grades_word_count": {
"stats": {
"field": "word_count"
}
}
}
}
grades_word_count:自定义
stats: 进行统计计算
返回最小,最大,文档总数,平均,总量
{
"aggs": {
"grades_word_count": {
"min": {
"field": "word_count"
}
}
}
}
min: 直接指定最小值也是可以的
原文地址:https://www.cnblogs.com/zhangboblogs/p/9826416.html