日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ES的多种搜索机制:query string search,query DSL,query filter,full-text search,phrase search,highlight search

發布時間:2024/9/27 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES的多种搜索机制:query string search,query DSL,query filter,full-text search,phrase search,highlight search 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

搜索全部商品:

GET /ecommerce/product/_search

took:耗費了幾毫秒
timed_out:是否超時,這里是沒有
_shards:數據拆成了5個分片,所以對于搜索請求,會打到所有的primary shard(或者是它的某個replica shard也可以)
hits.total:查詢結果的數量,3個document
hits.max_score:score的含義,就是document對于一個search的相關度的匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜索的document的詳細數據

{"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": "gaolujie 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"]}}]} }

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,要求輸入的搜索串,必須在指定的字段文本中,完全包含一模一樣的,才可以算匹配,才能作為結果返回

GET /ecommerce/product/_search {"query" : {"match_phrase" : {"producer" : "yagao producer"}} }

搜索到的結果是:

{"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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 香蕉视频一区二区三区 | 超碰麻豆 | 美女毛毛片 | 美女91网站| 乱视频在线观看 | 看片网址国产福利av中文字幕 | 大陆一级片 | 亚洲av日韩av在线观看 | 欧洲亚洲一区二区三区 | 午夜手机福利 | 欧美大喷水吹潮合集在线观看 | 丁香色欲久久久久久综合网 | 黄瓜视频91 | 色啊色 | 起碰在线 | 日本一二三区在线 | 欧美精品人妻一区二区 | 成品短视频泡芙 | 中文字幕综合网 | 免费一级特黄3大片视频 | 精品国产AV色欲天媒传媒 | 日韩在线观看免费全 | 国产粉嫩在线观看 | 日本乱轮视频 | 国产成人精品亚洲男人的天堂 | 中国色老太hd | 91国内揄拍国内精品对白 | 尤物av在线 | 91精品国产综合久久久久久久 | 黑人乱码一区二区三区av | 欧美片17c07.com | 亚洲免费在线观看av | 国产模特av私拍大尺度 | 亚洲成人av一区 | 天天干天天操天天舔 | 在线观看日韩av电影 | 免费a在线观看 | 2018自拍偷拍 | 日韩中文字幕一区二区 | 黑人巨大精品人妻一区二区 | 亚洲在线观看一区 | 又粗又大又硬毛片免费看 | 激情欧美日韩 | 亚洲7777| 拔擦8x成人一区二区三区 | 国产精品久久久久久久久久小说 | 日本国产网站 | 日本三不卡 | 91成人免费网站 | 国产孕妇一区二区三区 | 国产成人亚洲一区二区 | 999福利视频 | 少妇一区二区三区四区 | 国产69久久精品成人看 | 国产jizz18女人高潮 | 亚洲AV无码精品国产 | 成人精品久久久午夜福利 | aaaa毛片| 欧美成人精品二区三区99精品 | 毛片资源 | 久久久综合精品 | 久久视 | 国产一区二区精品在线观看 | 少妇特黄a一区二区三区88av | 性欧美在线视频观看 | 捆绑调教sm束缚网站 | 91在线中文字幕 | 91久久久国产精品 | 麻豆私人影院 | 女教师痴汉调教hd中字 | 手机在线看片福利 | 亚洲美女久久 | 福利电影一区二区 | 99精品热| free性丰满69性欧美 | 亚洲欧美另类综合 | 中文字幕3区 | a视频在线观看免费 | 国产又黄又猛又爽 | 亚洲私拍| 野战少妇38p| 青春草久久 | 天天爽夜夜爽一区二区三区 | 成年人视频网站 | 国产精品国产av | 777国产成人入口 | 黄视频在线免费看 | 亚洲久久在线 | 三级网站视频 | 中文字幕在线视频日韩 | 三级全黄的视频 | 色91视频 | 123毛片| 中文在线免费 | av色播 | 香蕉尹人 | 亚洲综合久久av一区二区三区 | 国产天堂精品 | 人妻久久久一区二区三区 |