您好,登錄后才能下訂單哦!
本篇內容介紹了“Elasticsearch中查詢的多種方式”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
query DSL:Domain Specified Language,特定領域的語言
語法
GET /{index}/_search { "query": {"match_all": {}} }
示例
輸入: GET /staffs/_search { "query": {"match_all": {}} }
輸出 { "took" : 1, 消耗的時間 "timed_out" : false, 是否超時 "_shards" : { "total" : 1, 一共請求了幾個shared "successful" : 1, 成功了幾個 "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, 查詢的結果的總數量 "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "staffs", 索引的名稱 "_type" : "_doc", "_id" : "1a", 數據對應的主鍵 "_score" : 1.0,就是document對于一個search的相關度的匹配分數,越相關,就越匹配,分數也高 "_source" : { 數據的詳情 "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 1.0, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "lixiansheng", "age" : 25, "phone" : "15733333333", "posittion" : "android", "hobby" : [ "paobu", "zuqiu", "tubu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "name" : "maxiaoshuan", "age" : 23, "phone" : "15744444444", "posittion" : "ios", "hobby" : [ "paobu", "yumaoqiu", "lanqiu" ] } } ] } }
語法
GET {index}/_search { "query": { "match": { "FIELD": "TEXT" FIELD:字段的名稱 TEXT:條件 } }, "sort": [ { "FIELD": { FIELD: 字段的名稱 "order": "desc" } } ] }
示例
GET staffs/_search { "query": { "match": { "name": "xiao" } }, "sort": [ { "age": { "order": "desc" } } ] }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "3", "_score" : null, "_source" : { "name" : "ma xiao shuai", "age" : 23, "phone" : "15744444444", "posittion" : "ios", "hobby" : [ "paobu", "yumaoqiu", "lanqiu" ] }, "sort" : [ 23 ] }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : null, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] }, "sort" : [ 21 ] } ] } }
語法
GET {index}/_search { "query": {"match_all": {}}, "from": 1, 從第幾頁開始查詢,0:代表第一頁,1:代表第二頁 "size": 1 每頁顯示的條數 }
示例
GET staffs/_search { "query": {"match_all": {}}, "from": 1, "size": 1 }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 1.0, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] } } ] } }
語法
GET {index}}/_search { "query": { "bool": { "must": [ {"match": { "FIELD": "TEXT" }} ] } }, "post_filter": { "range": { "FIELD": { "gte": 10 } } } }
示例
GET staffs/_search { "query": { "bool": { 可以拼接多個條件的查詢 "must": [ {"match": { "name": "xian sheng" }} ] } }, "post_filter": { "range": { "age": { "gt": 25 } } } }
其實有兩條 但是有一個年齡為25 所以這里只顯示一條 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.3862944, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 1.3862944, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } } ] } }
示例
GET staffs/_search { "query": {"match": { "posittion": "java kaifa" }} }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.6694658, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 1.6694658, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 0.60996956, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] } } ] } }
和全文檢索相反,全文檢索會將輸入的搜索串拆解開來,去倒排索引里面意義匹配,只要能匹配上任意一個拆解后的單詞,就可以作為結果返回
短語搜索,要求輸入的搜索串,必須在制定的字段文本中,完全包含一模一樣的,擦可以算匹配,才能作為結果返回
語法
GET staffs/_search { "query": { "match_phrase": { "FIELD": "PHRASE" } } }
示例
GET staffs/_search { "query": { "match_phrase": { "posittion": "java kaifa" } } }
{ "took" : 30, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.6694657, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 1.6694657, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } } ] } }
語法
GET {index}/_search { "query": { "match": { "FIELD": "TEXT" } }, "highlight": { "fields": { "FIELD": {} } } }
示例
GET staffs/_search { "query": { "match": { "posittion": "kaifa" } }, "highlight": { "fields": { "posittion": {} } } }
{ "took" : 54, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.60996956, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 0.60996956, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] }, "highlight" : { "posittion" : [ "java <em>kaifa</em>" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 0.60996956, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] }, "highlight" : { "posittion" : [ "web <em>kaifa</em>" ] } } ] } }
其實保存數據的時候,其查詢索引就已經創建了,使用的倒排索引
例如:posittion字段,先被拆解,然后創建倒排索引
拆解是根據選擇的分詞器構成的
拆解的單詞 | 對應的數據主鍵 |
---|---|
java | 1a |
kaifa | 1a,Nq_96G0Bs8sg-pU7kn0S |
web | 1a,Nq_96G0Bs8sg-pU7kn0S |
ios | 3 |
android | 4 |
GET _cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana_task_manager_1 Qyl1MousQLq5FyMOCBO4nw 1 0 2 0 30.5kb 30.5kb green open .apm-agent-configuration qPsz40bsQxW_Zcd_4pkJJg 1 0 0 0 283b 283b green open .kibana_1 yYdsQgxWQ0utXN-Ulhm5ew 1 0 9 0 35.4kb 35.4kb yellow open staffs uoo38LYwRB2PzxupYjJ66Q 1 1 4 0 17.9kb 17.9kb
“Elasticsearch中查詢的多種方式”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。