ES常见查询示例
環境:es(7.14.0)+kibana(7.14.2)
一、ES查詢示例
1、查看es信息
GET /
2、創建索引
PUT demo_person
3、刪除索引
DELETE demo_person
說明:
DELETE /index_one,index_two? --刪除兩個索引
DELETE /index_*? --刪除index_k開頭的索引
DELETE /_all? --刪除全部索引
DELETE /*? --刪除全部索引
4、創建索引包含setting和mapping
PUT demo_person
{"settings": {"number_of_shards": 5,"number_of_replicas": 1 },"mappings": {"properties": {"about": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"age": {"type": "long"},"first_name": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"interests": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}},"fielddata": true},"last_name": {"type": "keyword"}}}
}
5、修改setting
PUT demo_person/_settings
{"number_of_replicas" : 2
}
只能修改副本分片,主分片在創建索引時確定,后續不可以再次修改
6、修改mapping
PUT demo_person/_mapping/_doc?include_type_name=true
{"properties":{"interests":{"type":"text","fielddata":true}}
}
7、查詢總數
GET demo_person/_count
{ "query": {"match_all": {}}
}
8、添加/修改數據
PUT /demo_person/_doc/1
{"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests": [ "sports", "music" ]
}
9、批量插入數據
POST demo_person/_bulk
{"index":{}}
{"first_name":"zhang","last_name":"san","age" :44,"about":"I like to collect hehe albums","interests":["music"]}
{"index":{}}
{"first_name":"li","last_name":"si","age":12,"about":"I like to drink","interests":["drink"]}
10、查詢所有數據
GET demo_person/_search
{ "query": {"match_all": {}}
}
11、查詢指定條數
GET demo_person/_search
{ "size": 20, "query": {"match_all": {}}
}
12、根據ID查詢
GET /demo_person/_doc/1?pretty
13、一個查詢字符串搜索
GET /demo_person/_search?q=last_name:Smith
14、match搜索
GET /demo_person/_search
{"query" : {"match" : {"last_name" : "Smith"}}
}
15、term搜索
GET /demo_person/_search
{"query": {"term": {"last_name": {"value": "Smith"}}}
}
基于詞項的查詢
如?term?或?fuzzy?這樣的底層查詢不需要分析階段,它們對單個詞項進行操作。用?term?查詢詞項?Foo?只要在倒排索引中查找?準確詞項?,并且用 TF/IDF 算法為每個包含該詞項的文檔計算相關度評分?_score?。
記住?term?查詢只對倒排索引的詞項精確匹配,這點很重要,它不會對詞的多樣性進行處理(如,?foo?或?FOO?)。這里,無須考慮詞項是如何存入索引的。如果是將?["Foo","Bar"]?索引存入一個不分析的(?not_analyzed?)包含精確值的字段,或者將?Foo Bar?索引到一個帶有?whitespace?空格分析器的字段,兩者的結果都會是在倒排索引中有?Foo?和?Bar?這兩個詞。
基于全文的查詢
像?match?或?query_string?這樣的查詢是高層查詢,它們了解字段映射的信息:
如果查詢?日期(date)?或?整數(integer)?字段,它們會將查詢字符串分別作為日期或整數對待。
如果查詢一個(?not_analyzed?)未分析的精確值字符串字段,它們會將整個查詢字符串作為單個詞項對待。
但如果要查詢一個(?analyzed?)已分析的全文字段,它們會先將查詢字符串傳遞到一個合適的分析器,然后生成一個供查詢的詞項列表。
16、bool搜索
GET /demo_person/_search
{"query": {"bool": {"must": [{"match": {"last_name": "Smith"}}],"filter": [{"range": {"age": {"gte": 30}}}]}}
}
說明:
must:?? 完全匹配條件????? 相當于sql中的and
should: 至少滿足一個條件???? 相當于sql中的 or
must_not: 文檔必須不匹配條件???? 相當于sql中的!=
17、must多條件匹配查詢
GET /demo_person/_search
{"query": {"bool": {"must": [{"match": {"last_name": "Smith"}},{"match": {"age": 32}}]}}
}
18、Should滿足一個條件查詢
GET /demo_person/_search
{"query": {"bool": {"should": [{"match": {"last_name": "Fir"}},{"match": {"age": 32}}]}}
}
19、must_not必須不匹配查詢
GET /demo_person/_search
{"query": {"bool": {"must_not": [{"match": {"last_name": "Fir"}},{"match": {"age": 32}}]}}
}
20、多個字段查詢內容
GET /demo_person/_search
{"query": {"multi_match": {"query": "collect rock","fields": ["last_name","about"]}}
}
21、一個字段查詢多個內容
GET /demo_person/_search
{"query": {"terms": {"about": ["rock","hehe" ]}}
}
22、通配符和正則匹配
GET /demo_person/_search
{"query": {"bool": {"filter": [{"wildcard":{"last_name":"*mi*"}}]}}
}
23、前綴查詢
GET /demo_person/_search
{"query": {"prefix": {"last_name": {"value": "Smi"}}}
}
24、短語匹配
GET /demo_person/_search
{"query" : {"match_phrase" : {"about" : "rock climbing"}}
}
25、輸入即搜索
GET /demo_person/_search
{"query": {"match_phrase_prefix": {"about": "I like to collect"}}
}
26、高亮搜索
GET /demo_person/_search
{"query" : {"match_phrase" : {"about" : "rock climbing"}},"highlight": {"fields" : {"about" : {}}}
}
27、統計
GET /demo_person/_search
{"aggs": {"all_result": {"terms": { "field": "interests" }}}
}
28、根據條件統計
GET /demo_person/_search
{"query": {"match": {"last_name": "smith"}},"aggs": {"all_interests": {"terms": {"field": "interests"}}}
}
29、獲取平均數
GET /demo_person/_search
{"aggs" : {"all_interests" : {"terms" : { "field" : "interests" },"aggs" : {"avg_age" : {"avg" : { "field" : "age" }}}}}
}
30、多個字段匹配查詢
GET /demo_person/_search
{"query": {"multi_match": {"query": "Smith","fields": ["last_name","about"]}}
}
31、范圍查詢
GET demo_person/_search
{ "query": {"range": {"age": {"gte": 30,"lt": 35}}}
}
32、排序
GET /demo_person/_search
{"query": {"bool": {"must": [{"term": { "last_name.keyword": {"value": "Smith"}}},{"term": {"about": {"value": "climbing"}}}]}},"sort": [{"last_name": {"order": "desc"}}]
}
33、刪除一條數據
DELETE /demo_person/_doc/TC4cJ4ABPbcGgBnacj_w
34、批量刪除
POST /_bulk
{"delete":{"_index":"demo_person","_id":"1"}}
{"delete":{"_index":"demo_person","_id":"12"}}
35、分頁查詢
GET /demo_person/_search?from=1&size=10
或者
GET /demo_person/_search
{"from": 1,"size": 10
}
Size:顯示應該返回的結果數量,默認是?10
From:顯示應該跳過的初始結果數量,默認是?0
36、游標查詢
GET /demo_person/_search?scroll=5m
{"query": {"match_all": {}},"sort": [{"_doc": {"order": "desc"}}],"size": 1
}
然后獲取scroll_id繼續查詢,如下
GET _search/scroll
{"scroll":"5m",
"scroll_id":"FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoBRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FgWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FkWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FoWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FsWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FwWV0QzQ0pVQm9URnVBVFpmOGpDSC05QQ=="
}
37、字段存在查詢
GET /demo_person/_search
{"query": {"exists": {"field": "aa"}}
}
38、復雜查詢demo
Demo1
{"bool": {"must": { "match": { "tweet": "elasticsearch" }},"must_not": { "match": { "name": "mary" }},"should": { "match": { "tweet": "full text" }},"filter": { "range": { "age" : { "gt" : 30 }} }}
}
demo2
{"bool": { "must": { "match": { "email": "business opportunity" }},"should": [{ "match": { "starred": true }},{ "bool": {"must": { "match": { "folder": "inbox" }},"must_not": { "match": { "spam": true }}}}],"minimum_should_match": 1}
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
Demo3
GET /my_store/products/_search
{"query" : {"filtered" : {"filter" : {"bool" : {"should" : [{ "term" : {"productID" : "KDKE-B-9947-#kL5"}}, { "bool" : { "must" : [{ "term" : {"productID" : "JODL-X-1937-#pV7"}}, { "term" : {"price" : 30}} ]}}]}}}}
}
39、拷貝索引
POST _reindex
{"source": {"index": "twitter"},"dest": {"index": "new_twitter"}
}
40、索引別名
PUT /my_index_v1 –創建索引
PUT /my_index_v1/_alias/my_index? --創建my_index_v1別名為my_index
GET /*/_alias/my_index –-查看別名指向哪個索引
GET /my_index_v1/_alias/*? --查看哪些別名指向此索引
41、查詢集群健康
GET /_cluster/health
status?字段指示著當前集群在總體上是否工作正常。它的三種顏色含義如下:
green:所有的主分片和副本分片都正常運行。
Yellow:所有的主分片都正常運行,但不是所有的副本分片都正常運行。
Red:有主分片沒能正常運行。
總結
- 上一篇: 使用docker-compose的Spr
- 下一篇: Spring整合基础