ES常用查詢命令
一、基礎(chǔ)配置查看
GET /_cluster/health
{ "cluster_name" : "es-cluster", "status" : "green", #集群狀態(tài),分為green、yellow和red "timed_out" : false, "number_of_nodes" : 2, #集群的節(jié)點數(shù) "number_of_data_nodes" : 2, #數(shù)據(jù)節(jié)點數(shù) "active_primary_shards" : 88, #集群中所有活躍的主分片數(shù)。 "active_shards" : 116, #集群中所有活躍的分片數(shù)。 "relocating_shards" : 0, #當(dāng)前節(jié)點遷往其他節(jié)點的分片數(shù)量,通常為0,當(dāng)有節(jié)點加入或者退出時該值會增加。 "initializing_shards" : 0, #正在初始化的分片。 "unassigned_shards" : 0, #未分配的分片數(shù),通常為0,當(dāng)有某個節(jié)點的副本分片丟失該值就會增加。 "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, #是指主節(jié)點創(chuàng)建索引并分配shards等任務(wù),如果該指標(biāo)數(shù)值一直未減小代表集群存在不穩(wěn)定因素 "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 #集群分片健康度,活躍分片數(shù)占總分片數(shù)比例。 }
二、常用數(shù)據(jù)操作
一、查看所有索引
GET _cat/indices
二、match查詢
match關(guān)鍵字,相當(dāng)于MySQL數(shù)據(jù)庫中的like查詢,match查詢的字段如果是text類型,那么text會被分詞,match就會匹配分詞,查詢所有包含分詞的doc文檔,
如果不是text類型的,那么就是精確查詢。
- match條件查詢
GET 索引名稱/_search { "query": { "match": { "查詢字段": "查詢值" } } }
- match_all查詢所有數(shù)據(jù),默認(rèn)是10條,因為ES默認(rèn)分頁是10條數(shù)據(jù),這個關(guān)鍵字不能寫查詢條件。
GET 索引名稱/_search { "query": { "match_all": {} } }
- match_phrase:匹配短語,match是會查詢所有包含分詞的doc文檔,而match_phrase則是匹配整個短語,才會返回對應(yīng)的doc文檔。
- match_phrase_prefix:匹配短語的前綴部分,這個只能使用在text類型字段。
三、過濾字段
第一種:_source=false,表示所有字段都不返回。
第二種:_source: ["username", "age"],只返回指定的字段。
GET 索引名稱/_search { "query": { "match": { "查詢字段": "查詢值" } } , "_source": ["查詢字段1","查詢字段2","查詢字段3"] }
四、must布爾查詢
條件查詢中的【must】和mysql數(shù)據(jù)庫中的【and】是相同作用,【must】接收一個數(shù)組,數(shù)組中的所有條件都成立,這個時候才會查詢對應(yīng)數(shù)據(jù)。
GET 索引名稱/_search { "query": { "bool": { "must": [ { "terms": { "查詢字段": ["查詢值1","查詢值2"] } } ] } }, "_source": ["查詢字段1","查詢字段2"] }
GET 索引名稱/_search { "query": { "bool": { "must": [ { "match": { "查詢字段": "查詢值" }, "match": { "查詢字段": "查詢值" } } ] } }, "_source": ["查詢字段1","查詢字段2"] }
五、should布爾查詢
既然有and運算,那當(dāng)然少不了or運算,ES中使用【should】表示或運算,相當(dāng)于mysql數(shù)據(jù)庫中的【or】連接符。
GET 索引名稱/_search { "query": { "bool": { "should": [ { "match": { "查詢字段": "查詢值" }, "match": { "查詢字段": "查詢值" } } ] } }, "_source": ["查詢字段1","查詢字段2"] }
六、must_not布爾查詢
must_not是非運算,相當(dāng)于mysql數(shù)據(jù)庫中的【!】非運算,即:【not】。
GET 索引名稱/_search { "query": { "bool": { "must_not": [ { "match": { "查詢字段": "查詢值" } } ] } }, "_source": ["查詢字段1","查詢字段2"] }
七、filter過濾查詢
filter是用于過濾查詢的關(guān)鍵字,在filter里面可以使用多種查詢條件,例如:range、term、terms、exists、ids幾種常見的查詢,
- range范圍查詢,范圍查詢首先需要指定范圍,下面是幾個常見的范圍關(guān)鍵字。
- gt:大于。
- lt:小于。
- gte:大于等于。
- lte:小于等于。
- eq:等于。
- ne:不等于。
# 10<=查詢字段<=20
GET 索引名稱/_search { "query": { "bool": { "filter": [ { "range": { "查詢字段": { "gte": 10, "lte": 20 } } } ] } } }
2.exists是否存在
GET 索引名稱/_search { "query": { "bool": { "filter": [ { "exists": { "查詢字段":"字段值" } } ] } } }
3.ids過濾查詢
ids查詢,這就相當(dāng)于mysql數(shù)據(jù)庫中的【in】條件查詢,多個條件值查詢,但是這里的只能夠?qū)?/p>
GET 索引名稱/_search { "query": { "bool": { "filter": [ { "ids": { "values":["字段值1","字段值2"] } } ] } } }
4.term關(guān)鍵詞查詢
term表示關(guān)鍵字查詢,即:判斷doc文檔里面是否包含給定的term關(guān)鍵字,如果包含,則滿足條件,否則不滿足。
GET 索引名稱/_search { "query": { "bool": { "filter": [ { "term": { "查詢字段":"字段值" } } ] } } }
5.terms多關(guān)鍵詞查詢
term每次只能夠匹配一個關(guān)鍵字,而terms則允許多個關(guān)鍵字匹配。
GET 索引名稱/_search { "query": { "bool": { "filter": [ { "terms": { "查詢字段":["字段值1","字段值2"] } } ] } } }
本文來自博客園,作者:曾經(jīng)已是追憶,轉(zhuǎn)載請注明原文鏈接:http://www.rzrgm.cn/qingdufengyun/p/18323131

浙公網(wǎng)安備 33010602011771號