ES 基础语法
索引
创建索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| PUT /movie { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "properties": { "title": { "type": "text", "analyzer": "english" }, "tagline": { "type": "text", "analyzer": "english" }, "release_date": { "type": "date", "format": "yyyy-MM-dd" }, "popularity": { "type": "double" }, "overview": { "type": "text", "analyzer": "english" } } } }
|
查看索引是否存在
查看索引
查看索引设置
删除索引
关闭索引
打开索引
克隆索引
1
| POST /employee/_clone/clone_employee
|
添加映射
1 2 3 4 5 6 7 8 9
| PUT /employee,employee2/_mapping { "properties": { "remark": { "type": "text", "analyzer": "ik_max_word" } } }
|
1 2 3 4 5 6 7 8 9 10 11
| PUT /employee { "mappings": { "properties": { "remark": { "type": "text", "analyzer": "ik_max_word" } } } }
|
查看映射
文档
创建索引和文档
如果索引不存在,则创建索引,如果文档不存在则创建文档,否则全量更新文档
1 2 3 4 5
| PUT /employee/_doc/1 { "name": "陈开开", "age": 25 }
|
创建文档
如果文档已存在则失败
1 2 3 4 5
| POST /employee/_create/1 { "name": "中国人民", "age": 20 }
|
删除文档
检查文档是否存在
查看文档总数量
查看指定文档
查看源
修改文档
1 2 3 4 5 6
| POST /employee/_update/1 { "doc": { "name": "陈21" } }
|
批量查看
1 2 3 4 5 6 7 8 9 10 11 12 13
| GET /_mget { "docs": [ { "_index": "employee", "_id": "1" }, { "_index": "employee2", "_id": "1" } ] }
|
1 2 3 4 5 6 7 8 9 10 11
| GET /employee/_doc/_mget { "docs": [ { "_id": "1" }, { "_id": "2" } ] }
|
1 2 3 4 5 6 7
| GET /employee/_mget { "ids": [ "1", "2" ] }
|
批量操作
1 2 3 4 5 6 7 8
| POST _bulk {"index":{"_index":"test","_id":"1"}} {"field1":"value1"} {"delete":{"_index":"test","_id":"2"}} {"create":{"_index":"test","_id":"3"}} {"field1":"value3"} {"update":{"_id":"1","_index":"test"}} {"doc":{"field2":"value2"}}
|
1 2 3 4 5
| POST /employee/_bulk {"index":{"_id":"1"}} {"name":"男子偷上万元发红包求交女友 被抓获时仍然单身"} {"index":{"_id":"2"}} {"name":"16岁少女为结婚“变”22岁 7年后想离婚被法院拒绝"}
|
根据查询删除
1 2 3 4 5 6 7 8
| POST /employee/_delete_by_query { "query": { "match": { "name": "中国" } } }
|
根据查询修改
1 2 3 4 5 6 7 8 9 10 11 12
| POST /employee/_update_by_query { "script": { "source": "ctx._source.age++", "lang": "painless" }, "query": { "match": { "_id": 1 } } }
|
搜索
match 查看
按照字段上定义的分词分析后去索引内查看,默认 operator 为 or,默认 minimum_should_match 为 1
1 2 3 4 5 6 7 8 9 10 11 12
| GET /movie/_search { "query": { "match": { "title": { "query": "Wall Street: Money Never Sleeps", "operator": "or", "minimum_should_match": 2 } } } }
|
term 查看
不会进行分词,精确匹配
1 2 3 4 5 6 7 8
| GET /movie/_search { "query": { "term": { "title": "killer" } } }
|
1 2 3 4 5 6 7 8 9 10 11
| GET /movie/_search { "query": { "terms": { "title": [ "killer", "elite" ] } } }
|
查看全部
1 2 3 4 5 6
| GET /movie/_search { "query": { "match_all": {} } }
|
短语查看
1 2 3 4 5 6 7 8
| GET /movie/_search { "query": { "match_phrase": { "title": "steve zissou" } } }
|
短语前缀查看
1 2 3 4 5 6 7 8
| GET /movie/_search { "query": { "match_phrase_prefix": { "title": "steve zis" } } }
|
多字段查看
- best_fields:默认,查找匹配任何字段的文档,但使用来自最佳字段的 _score
- most_fields:查找匹配任何字段的文档,并合并每个字段的 _score
- cross_fields:使用同一个分析器处理字段,就像它们是一个大字段一样。查找任何字段中的每个单词
- phrase:在每个字段上运行 match_phrase 查看,并使用来自最佳字段的 _score
- phrase_prefix:在每个字段上运行 match_phrase_prefix 查看,并使用来自最佳字段的 _score
- bool_prefix:在每个字段上创建一个 match_bool_prefix 查看,并合并每个字段的 _score
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| GET /movie/_search { "query": { "multi_match": { "type": "most_fields", "query": "basketball with cartoom aliens", "fields": [ "title", "overview" ] } }, "from": 0, "size": 10, "_source": [ "title", "overview" ] }
|
query_string
1 2 3 4 5 6 7 8 9 10 11
| GET /movie/_search { "query": { "query_string": { "fields": [ "title" ], "query": "steve AND jobs" } } }
|
过滤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| GET /movie/_search { "query": { "bool": { "filter": [ { "range": { "popularity": { "gt": 10 } } } ] } } }
|
多条件过滤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| GET /movie/_search { "query": { "bool": { "filter": [ { "term": { "title": "steve" } }, { "range": { "release_date": { "lte": "2015-01-01" } } }, { "range": { "popularity": { "gte": "25" } } } ] } } }
|
带分数的过滤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| GET /movie/_search { "query": { "bool": { "must": [ { "match": { "title": "life" } } ], "filter": [ { "term": { "title": "steve" } }, { "range": { "release_date": { "lte": "2015-01-01" } } }, { "range": { "popularity": { "gte": "25" } } } ] } } }
|
聚合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| GET /movie/_search { "sort": [ { "popularity": { "order": "desc" } } ], "aggs": { "group_by_release_date": { "terms": { "field": "release_date", "size": 5 } } } }
|
Bool 查看
- must:必须都是 true
- must not:必须都是 false
- should:其中有一个为 true 即可,但 true 的越多得分越高
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| GET /movie/_search { "query": { "bool": { "should": [ { "match": { "title": "basketball with cartoom aliens" } }, { "match": { "overview": "basketball with cartoom aliens" } } ] } } }
|
突出显示
- “tags_schema” : “styled”,
- “pre_tags”: [““], “post_tags”: [““],
1 2 3 4 5 6 7 8 9 10 11 12 13
| GET /movie/_search { "query": { "match": { "title": "world" } }, "highlight": { "fields": { "title": {} } } }
|
其它
校验语句是否合法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| GET /movie/_validate/query?explain { "query": { "multi_match": { "query": "steve job", "fields": [ "title", "overview" ], "operator": "or", "type": "most_fields" } } }
|
分析调试
1 2 3 4 5 6 7 8 9 10
| GET /movie/_search { "explain": true, "query": { "match": { "title": "steve" } } }
|
修改打分权重
1 2 3 4 5 6 7 8 9 10 11 12 13
| GET /movie/_search { "query": { "multi_match": { "query": "basketball with cartoom aliens", "fields": [ "title^10", "overview" ], "tie_breaker": 0.3 } } }
|
自定义打分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| GET /movie/_search { "query": { "function_score": { "query": { "multi_match": { "query": "steve job", "fields": [ "title", "overview" ], "operator": "or", "type": "most_fields" } }, "functions": [ { "field_value_factor": { "field": "popularity", "modifier": "log2p", "factor": 10 } } ], "score_mode": "sum", "boost_mode": "sum" } } }
|