ES的多种搜索机制:query string search,query DSL,query filter,full-text search,phrase search,highlight search
1、query string search
搜索全部商品:
GET /ecommerce/product/_searchtook:耗費了幾毫秒
timed_out:是否超時,這里是沒有
_shards:數據拆成了5個分片,所以對于搜索請求,會打到所有的primary shard(或者是它的某個replica shard也可以)
hits.total:查詢結果的數量,3個document
hits.max_score:score的含義,就是document對于一個search的相關度的匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜索的document的詳細數據
query string search的由來,因為search參數都是以http請求的query string來附帶的
搜索商品名稱中包含yagao的商品,而且按照售價降序排序:
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc結果是:
{"took": 5,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": null,"hits": [{"_index": "ecommerce","_type": "product","_id": "3","_score": null,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]},"sort": [40]},{"_index": "ecommerce","_type": "product","_id": "1","_score": null,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]},"sort": [30]},{"_index": "ecommerce","_type": "product","_id": "2","_score": null,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]},"sort": [25]}]} }適用于臨時的在命令行使用一些工具,比如curl,快速的發出請求,來檢索想要的信息;但是如果查詢請求很復雜,是很難去構建的
在生產環境中,幾乎很少使用query string search
2、query DSL
DSL:Domain Specified Language,特定領域的語言
http request body:請求體,可以用json的格式來構建查詢語法,比較方便,可以構建各種復雜的語法,比query string search肯定強大多了
查詢所有的商品
GET /ecommerce/product/_search {"query": { "match_all": {} } }結果是:
{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "ecommerce","_type": "product","_id": "2","_score": 1,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "1","_score": 1,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 1,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]}}]} }查詢名稱包含yagao的商品,同時按照價格降序排序
GET /ecommerce/product/_search {"query" : {"match" : {"name" : "yagao"}},"sort": [{ "price": "desc" }] }結果是:
{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 1,"max_score": null,"hits": [{"_index": "ecommerce","_type": "product","_id": "3","_score": null,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]},"sort": [40]}]} }如果在查詢的時候報如下錯誤:
{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [price] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}],"type": "search_phase_execution_exception","reason": "all shards failed","phase": "query","grouped": true,"failed_shards": [{"shard": 0,"index": "ecommerce","node": "DEtgiaiTSaWWvmOfvlvc6Q","reason": {"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [price] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}]},"status": 400 }解決辦法是(其中語法是:/type/_mapping/index),然后添加fielddata屬性:
PUT /ecommerce/_mapping/product {"properties": {"price":{"type": "text","fielddata": true}} }執行完成之后:
{"acknowledged": true }分頁查詢商品,總共3條商品,假設每頁就顯示1條商品,現在顯示第2頁,所以就查出來第2個商品
GET /ecommerce/product/_search {"query": { "match_all": {} },"from": 1,"size": 1 }結果是:
{"took": 4,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "ecommerce","_type": "product","_id": "1","_score": 1,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}}]} }指定要查詢出來商品的名稱和價格就可以
GET /ecommerce/product/_search {"query": { "match_all": {} },"_source": ["name", "price"] }結果是:
{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "ecommerce","_type": "product","_id": "2","_score": 1,"_source": {"price": 25,"name": "jiajieshi yagao"}},{"_index": "ecommerce","_type": "product","_id": "1","_score": 1,"_source": {"price": 30,"name": "jiaqiangban gaoluejie yagao"}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 1,"_source": {"price": 40,"name": "zhonghua yagao"}}]} }更加適合生產環境的使用,可以構建復雜的查詢
3、query filter
搜索商品名稱包含yagao,而且售價大于25元的商品
GET /ecommerce/product/_search {"query" : {"bool" : {"must" : {"match" : {"name" : "yagao" }},"filter" : {"range" : {"price" : { "gt" : 25 } }}}} }結果是:
{"took": 68,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 2,"max_score": 0.25811607,"hits": [{"_index": "ecommerce","_type": "product","_id": "3","_score": 0.25811607,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]}},{"_index": "ecommerce","_type": "product","_id": "1","_score": 0.25316024,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}}]} }4、full-text search(全文檢索)
先添加一條記錄:
put /ecommerce/product/4 {"name":"special yagao","desc":"special meibai","price":50,"producer":"special yagao producer","tags":["meibai"] }進行查詢
GET /ecommerce/product/_search {"query" : {"match" : {"producer" : "yagao producer"}} }上面的producer這個字段,會先被拆解,建立倒排索引
special 4
yagao 4
producer 1,2,3,4
gaolujie 1
zhognhua 3
jiajieshi 2
也就是說yagao producer 會被拆解成: yagao和producer,最后查詢出來的結果是:
{"took": 4,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0.25811607,"hits": [{"_index": "ecommerce","_type": "product","_id": "1","_score": 0.25811607,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 0.25811607,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]}},{"_index": "ecommerce","_type": "product","_id": "2","_score": 0.1805489,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "4","_score": 0.14638957,"_source": {"name": "special yagao","desc": "special meibai","price": 50,"producer": "special yagao producer","tags": ["meibai"]}}]} }5、phrase search(短語搜索)
跟全文檢索相對應,相反,全文檢索會將輸入的搜索串拆解開來,去倒排索引里面去一一匹配,只要能匹配上任意一個拆解后的單詞,就可以作為結果返回
phrase search,要求輸入的搜索串,必須在指定的字段文本中,完全包含一模一樣的,才可以算匹配,才能作為結果返回
搜索到的結果是:
{"took": 11,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 1,"max_score": 0.70293105,"hits": [{"_index": "ecommerce","_type": "product","_id": "4","_score": 0.70293105,"_source": {"name": "special yagao","desc": "special meibai","price": 50,"producer": "special yagao producer","tags": ["meibai"]}}]} }6、highlight search(高亮搜索結果)
GET /ecommerce/product/_search {"query" : {"match" : {"producer" : "producer"}},"highlight": {"fields" : {"producer" : {}}} }查詢出來的結果是:
{"took": 50,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0.25811607,"hits": [{"_index": "ecommerce","_type": "product","_id": "1","_score": 0.25811607,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]},"highlight": {"producer": ["gaolujie <em>producer</em>"]}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 0.25811607,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]},"highlight": {"producer": ["zhonghua <em>producer</em>"]}},{"_index": "ecommerce","_type": "product","_id": "2","_score": 0.1805489,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]},"highlight": {"producer": ["jiajieshi <em>producer</em>"]}},{"_index": "ecommerce","_type": "product","_id": "4","_score": 0.14638957,"_source": {"name": "special yagao","desc": "special meibai","price": 50,"producer": "special yagao producer","tags": ["meibai"]},"highlight": {"producer": ["special yagao <em>producer</em>"]}}]} }總結
以上是生活随笔為你收集整理的ES的多种搜索机制:query string search,query DSL,query filter,full-text search,phrase search,highlight search的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 证券发行的公开原则
- 下一篇: 钱站提前还款能减免吗