小白學ES 07 - Elasticsearch的多種查詢方式
目錄
- 1 Query String Search
- 2 Query DSL
- 3 Query Filter
- 4 Full Text Search(全文檢索)
- 5 Phrase Search(短語檢索)
- 6 Highlight Search(高亮顯示檢索結果)
1 Query String Search
這種方法通過HTTP請求的Query String攜帶查詢引數, 因此得名.
適用於臨時性的查詢請求, 比如在終端檢查基礎資訊:
# 檢索name中包含Java的文件, 並按價格降序排序: curl -XGET 'http://localhost:9200/shop/it_book/_search?q=name:Java&sort=price:desc'
生產環境中很少使用, 因為請求引數都封裝到Query String中, 難以構建複雜的查詢.
查詢全部商品:
GET /shop/it_book/_search
響應結果:
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 1, "_source": { "name": "深入理解Java虛擬機器:JVM高階特性與最佳實踐", "author": "周志明", "desc": "Java圖書領域公認的經典著作", "price": 79, "date": "2013-06", "publisher": "機械工業出版社", "tags": [ "Java", "虛擬機器", "最佳實踐" ] } }, // 省略其餘2條記錄 ] } }
響應結果中各引數說明:
- took: 此次檢索耗費的時間, 單位: 毫秒.
- timed_out: 是否超出規定的檢索時間, 這裡沒有設定, 後續會講解此引數.
- _shards: 被查詢的index被分解成5個分片, 所以搜尋請求會分發到所有的primary shard(或primary shard對應的某個replica shard)上.
- hits.total: 符合查詢請求的文件數, 即hit(命中)數.
- hits.max_score: Lucene底層對檢索到的文件的相關度的評分, 相關度越高, 說明越匹配, score的值也就越高.
- hits.hits: 命中的所有document的詳細資料.
2 Query DSL
DSL: Domain Specified Language, 特定領域的語言.
這種查詢方式將查詢引數構建為JSON格式的資料, 並封裝到HTTP請求的Request Body(請求體)中, 可以構建各類複雜的查詢語法, 比Query String Search強大很多.
查詢全部商品:
GET /shop/it_book/_search { "query": { "match_all": {} } }
查詢name中包含Java的商品, 並按price降序排序
GET /shop/it_book/_search { "query": { "match": { "name": "Java" } }, "sort": [ { "price": "desc" } ] }
分頁查詢商品 - 每頁顯示1條, 顯示第3頁:
GET /shop/it_book/_search { "query": { "match_all": {} }, "from": 2, "size": 1 } GET /ecommerce/product/_search { "query": { "match_all": {} }, "_source": ["name", "price"] }
只查詢商品的名稱和價格:
GET /shop/it_book/_search { "query": {"match_all": {}}, "_source": ["name", "price"] }
3 Query Filter
過濾查詢, 如: 查詢name包含Java, 且price大於80元的商品:
GET /shop/it_book/_search
{
"query": {
"bool": {
"must": {
"match": {"name": "Java"}
},
"filter": {
"range": { "price": {"gt": 80} }
}
}
}
}
4 Full Text Search(全文檢索)
查詢desc中包含"Java圖書"的文件, 只顯示name和desc的值:
GET /shop/it_book/_search { "query": { "match": {"desc": "Java圖書"} }, "_source": ["name", "desc"] }
全文檢索過程說明:
ES會對欄位"desc"的內容進行分詞, 並建立倒排索引.
這裡將 "Java圖書" 分詞為 "Java"、"圖"、"書" 3個, 檢索時將匹配desc中含有 "Java"、"圖"、"書" 中任意一個分詞的文件.
—— 對於中文分詞, 後續會使用IK分詞器, 彼時會將 "Java圖書" 分詞為 "Java"、"圖書" 2個.
響應結果:
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.7594807, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 0.7594807, "_source": { "name": "深入理解Java虛擬機器:JVM高階特性與最佳實踐", "desc": "Java圖書領域公認的經典著作" } }, { "_index": "shop", "_type": "it_book", "_id": "1", "_score": 0.25316024, "_source": { "name": "Java程式設計思想", "desc": "Java學習必讀經典,殿堂級著作!" } } ] } }
5 Phrase Search(短語檢索)
Full Text Search會將檢索文字作分詞處理, 然後從倒排索引中作匹配查詢, 如果某個文件的對應field中存在任意一個分解後的詞, 該文件就算匹配檢索條件.
Phrase Search不會對檢索串進行分詞處理, 只有一個文件的對應field中包含與檢索文字完全一致的內容, 該文件才算匹配檢索條件, 也才能作為結果返回.
精確查詢desc中包含"Java圖書"的文件:
GET /shop/it_book/_search { "query": { "match_phrase": { "desc": "Java圖書" } }, "_source": ["name", "desc"] }
響應結果:
{ "took": 13, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.7594808, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 0.7594808, "_source": { "name": "深入理解Java虛擬機器:JVM高階特性與最佳實踐", "desc": "Java圖書領域公認的經典著作" } } ] } }
6 Highlight Search(高亮顯示檢索結果)
分頁查詢desc中包含"Java圖書"的文件, 頁大小為1, 顯示第3頁, 並對搜尋條件高亮處理:
GET /shop/it_book/_search { "query": { "match": {"desc": "Java圖書"} }, "from": 0, "size": 1, "highlight": { "fields": {"desc": {}} }, "_source": ["name", "desc"] }
響應結果:
{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.7594807, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 0.7594807, "_source": { "name": "深入理解Java虛擬機器:JVM高階特性與最佳實踐", "desc": "Java圖書領域公認的經典著作" }, "highlight": { "desc": [ "<em>Java</em><em>圖</em><em>書</em>領域公認的經典著作" // 高亮顯示的內容: 預設新增<em>標籤 ] } } ] } }
版權宣告
作者: ma_shoufeng(馬瘦風)
出處: 部落格園 馬瘦風的部落格
您的支援是對博主的極大鼓勵, 感謝您的閱讀.
本文版權歸博主所有, 歡迎轉載, 但未經博主同意必須保留此段宣告, 且在文章頁面明顯位置給出原文連結, 否則博主保留追究相關人員法律責任的權利.