摘要:上一章節(jié)主要介紹了的一些重要概念及簡(jiǎn)單的,本章內(nèi)容將重點(diǎn)介紹的多種查詢方式。的多種搜索方式到此介紹完畢,每種搜索方式中還包含其他的,文中沒(méi)有全部介紹,如果有需要可以自行翻閱官方文檔。
上一章節(jié)主要介紹了ES的一些重要概念及簡(jiǎn)單的CRUD,本章內(nèi)容將重點(diǎn)介紹ES的多種查詢方式。ES在使用過(guò)程中,查詢是最重要的應(yīng)用場(chǎng)景。一、Query String Search(‘Query String’方式的搜索)
1.搜索全部商品
GET /shop_index/productInfo/_search
返回結(jié)果:
{ "took": 8, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "2", "_score": 1, "_source": { "test": "test" } }, { "_index": "shop_index", "_type": "productInfo", "_id": "zyWpRGkB8mgaHjxk0Hfo", "_score": 1, "_source": { "name": "HuaWei P20", "desc": "Expen but easy to use", "price": 5300, "producer": "HuaWei Producer", "tags": [ "Expen", "Fast" ] } }, { "_index": "shop_index", "_type": "productInfo", "_id": "1", "_score": 1, "_source": { "name": "HuaWei Mate8", "desc": "Cheap and easy to use", "price": 2500, "producer": "HuaWei Producer", "tags": [ "Cheap", "Fast" ] } } ] } }
字段解釋:
took:耗費(fèi)了幾毫秒 timed_out:是否超時(shí),這里是沒(méi)有 _shards:數(shù)據(jù)被拆到了5個(gè)分片上,搜索時(shí)使用了5個(gè)分片,5個(gè)分片都成功地返回了數(shù)據(jù),失敗了0個(gè),跳過(guò)了0個(gè) hits.total:查詢結(jié)果的數(shù)量,3個(gè)document max_score:就是document對(duì)于一個(gè)search的相關(guān)度的匹配分?jǐn)?shù),越相關(guān),就越匹配,分?jǐn)?shù)也越高 hits.hits:包含了匹配搜索的document的詳細(xì)數(shù)據(jù)
2.搜索商品名稱中包含HuaWei的商品,而且按照售價(jià)降序排序:
下面這種方法也是"Query String Search"的由來(lái),因?yàn)閟earch參數(shù)都是以http請(qǐng)求的query string來(lái)附帶的.
GET /shop_index/productInfo/_search?q=name:HuaWei&sort=price:desc
返回結(jié)果:
{ "took": 23, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": null, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "zyWpRGkB8mgaHjxk0Hfo", "_score": null, "_source": { "name": "HuaWei P20", "desc": "Expen but easy to use", "price": 5300, "producer": "HuaWei Producer", "tags": [ "Expen", "Fast" ] }, "sort": [ 5300 ] }, { "_index": "shop_index", "_type": "productInfo", "_id": "1", "_score": null, "_source": { "name": "HuaWei Mate8", "desc": "Cheap and easy to use", "price": 2500, "producer": "HuaWei Producer", "tags": [ "Cheap", "Fast" ] }, "sort": [ 2500 ] } ] } }二、Query DSL(DSL: Domain Specified Language,特定領(lǐng)域的語(yǔ)言)
這種方法是通過(guò)一個(gè)json格式的http request body請(qǐng)求體作為條件,可以完成多種復(fù)雜的查詢需求,比query string的功能更加強(qiáng)大
1.搜索所有商品
GET /shop_index/productInfo/_search { "query": { "match_all": {} } }
返回結(jié)果省略...
2.查詢名稱中包含HuaWei的商品,并且按照價(jià)格降序排列
GET /shop_index/productInfo/_search { "query": { "match": { "name": "HuaWei" } }, "sort": [ { "price": { "order": "desc" } } ] }
返回結(jié)果省略...
3.分頁(yè)查詢第二頁(yè),每頁(yè)1條記錄
GET /shop_index/productInfo/_search { "query": { "match_all": {} }, "from": 1, "size": 1 }
返回結(jié)果:
{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "zyWpRGkB8mgaHjxk0Hfo", "_score": 1, "_source": { "name": "HuaWei P20", "desc": "Expen but easy to use", "price": 5300, "producer": "HuaWei Producer", "tags": [ "Expen", "Fast" ] } } ] } }
注意:
(1)在實(shí)際項(xiàng)目中,如果有條件查詢之后再需要分頁(yè),不需要多帶帶查詢總條數(shù),ES會(huì)返回滿足條件的總條數(shù),可以直接使用;
(2)ES的分頁(yè)默認(rèn)from是從0開始的;
4.只查詢特定字段,比如:name,desc和price字段,其他字段不需要返回
GET /shop_index/productInfo/_search { "query": { "match": { "name": "HuaWei" } }, "_source": ["name","desc","price"] }
返回結(jié)果:
{ "took": 27, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "zyWpRGkB8mgaHjxk0Hfo", "_score": 0.2876821, "_source": { "price": 5300, "name": "HuaWei P20", "desc": "Expen but easy to use" } }, { "_index": "shop_index", "_type": "productInfo", "_id": "1", "_score": 0.2876821, "_source": { "price": 2500, "name": "HuaWei Mate8", "desc": "Cheap and easy to use" } } ] } }三.Query Filter(對(duì)查詢結(jié)果進(jìn)行過(guò)濾)
比如:查詢名稱中包含HuaWei,并且價(jià)格大于4000的商品記錄:
GET /shop_index/productInfo/_search { "query": { "bool": { "must": [ { "match": { "name": "HuaWei" } } ], "filter": { "range": { "price": { "gt": 4000 } } } } } }
返回結(jié)果:
{ "took": 195, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "zyWpRGkB8mgaHjxk0Hfo", "_score": 0.2876821, "_source": { "name": "HuaWei P20", "desc": "Expen but easy to use", "price": 5300, "producer": "HuaWei Producer", "tags": [ "Expen", "Fast" ] } } ] } }四、全文索引(Full-Text Search)
搜索生產(chǎn)廠商字段中包含"HuaWei MateProducer"的商品記錄:
GET /shop_index/productInfo/_search { "query": { "match": { "producer": "HuaWei MateProducer" } } }
返回結(jié)果:
{ "took": 8, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0.5753642, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "SiUBRWkB8mgaHjxkJHyS", "_score": 0.5753642, "_source": { "name": "HuaWei Mate10", "desc": "Cheap and Beauti", "price": 2300, "producer": "HuaWei MateProducer", "tags": [ "Cheap", "Beauti" ] } }, { "_index": "shop_index", "_type": "productInfo", "_id": "1", "_score": 0.2876821, "_source": { "name": "HuaWei Mate8", "desc": "Cheap and easy to use", "price": 2500, "producer": "HuaWei Producer", "tags": [ "Cheap", "Fast" ] } }, { "_index": "shop_index", "_type": "productInfo", "_id": "zyWpRGkB8mgaHjxk0Hfo", "_score": 0.18232156, "_source": { "name": "HuaWei P20", "desc": "Expen but easy to use", "price": 5300, "producer": "HuaWei Producer", "tags": [ "Expen", "Fast" ] } }, { "_index": "shop_index", "_type": "productInfo", "_id": "CSX8RGkB8mgaHjxkV3w1", "_score": 0.18232156, "_source": { "name": "HuaWei nova 4e", "desc": "cheap and look nice", "price": 1999, "producer": "HuaWei Producer", "tags": [ "Cheap", "Nice" ] } } ] } }
從以上結(jié)果中可以看到:
id為"SiUBRWkB8mgaHjxkJHyS"的記錄score分?jǐn)?shù)最高,表示匹配度最高;
原因:
producer分完詞之后包括的詞語(yǔ)有:
(1).HuaWei:
匹配到改詞的記錄ID:"SiUBRWkB8mgaHjxkJHyS","1","CSX8RGkB8mgaHjxkV3w1","zyWpRGkB8mgaHjxk0Hfo"
(2).MateProducer:
匹配到該詞的記錄ID:"SiUBRWkB8mgaHjxkJHyS"
由于"HuaWei MateProducer"兩次匹配到ID為"SiUBRWkB8mgaHjxkJHyS"的記錄,所以該記錄的score分?jǐn)?shù)最高。
短語(yǔ)索引和全文索引的區(qū)別:
(1)全文匹配:將要搜索的內(nèi)容分詞,然后挨個(gè)單詞去倒排索引中匹配,只要匹配到任意一個(gè)單詞,就算是匹配到記錄;
(2)短語(yǔ)索引:輸入的搜索串,必須在指定的字段內(nèi)容中,完全包含一模一樣的,才可以算匹配,才能作為結(jié)果返回;
例如:搜索name中包含"HuaWei MateProducer"短語(yǔ)的商品信息:
GET /shop_index/productInfo/_search { "query": { "match_phrase": { "producer": "HuaWei MateProducer" } } }
返回結(jié)果:
{ "took": 158, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "SiUBRWkB8mgaHjxkJHyS", "_score": 0.5753642, "_source": { "name": "HuaWei Mate10", "desc": "Cheap and Beauti", "price": 2300, "producer": "HuaWei MateProducer", "tags": [ "Cheap", "Beauti" ] } } ] } }
可以看到只有包含"HuaWei MateProducer"的記錄才被返回。
六、Highlight Search(搜索高亮顯示)高亮搜索指的是搜索的結(jié)果中,將某些特別需要強(qiáng)調(diào)的詞使用特定的樣式展示出來(lái)。
例如:搜索商品名稱中包含"Xiao"Mi"的商品,并將搜索的關(guān)鍵詞高亮顯示:
GET /shop_index/productInfo/_search { "query": { "match": { "name": "Xiao"Mi" } }, "highlight": { "fields": { "name": {} } } }
返回結(jié)果:
{ "took": 348, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "HiX9RGkB8mgaHjxk4nxC", "_score": 0.2876821, "_source": { "name": "Xiao"Mi 9", "desc": "Expen but nice and Beauti", "price": 3500, "producer": "XiaoMi Producer", "tags": [ "Expen", "Beauti" ] }, "highlight": { "name": [ "Xiao"Mi 9" ] } } ] } }
可以看到,"Xiao"Mi"使用了標(biāo)簽返回了,可以在HTML中直接以斜體展示。
如果想使用自定義高亮樣式,可以使用pre_tags和post_tags進(jìn)行自定義,比如:想使用紅色展示,如下所示:
GET /shop_index/productInfo/_search { "query": { "match": { "name": "Xiao"Mi" } }, "highlight": { "fields": { "name": {} }, "pre_tags": [ "" ], "post_tags": [ "" ] } }
返回結(jié)果:
{ "took": 10, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "shop_index", "_type": "productInfo", "_id": "HiX9RGkB8mgaHjxk4nxC", "_score": 0.2876821, "_source": { "name": "Xiao"Mi 9", "desc": "Expen but nice and Beauti", "price": 3500, "producer": "XiaoMi Producer", "tags": [ "Expen", "Beauti" ] }, "highlight": { "name": [ "Xiao"Mi 9" ] } } ] } }
返回結(jié)果中的搜索關(guān)鍵字使用表示紅色的css樣式展示出來(lái)。
ES的多種搜索方式到此介紹完畢,每種搜索方式中還包含其他的API,文中沒(méi)有全部介紹,如果有需要可以自行翻閱官方文檔。歡迎評(píng)論轉(zhuǎn)發(fā)!
后續(xù)更多文章將更新在個(gè)人小站上,歡迎查看。
另外提供一些優(yōu)秀的IT視頻資料,可免費(fèi)下載!如需要請(qǐng)查看https://www.592xuexi.com
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/73903.html
閱讀 1641·2019-08-30 15:44
閱讀 2566·2019-08-30 11:19
閱讀 393·2019-08-30 11:06
閱讀 1556·2019-08-29 15:27
閱讀 3077·2019-08-29 13:44
閱讀 1621·2019-08-28 18:28
閱讀 2352·2019-08-28 18:17
閱讀 1978·2019-08-26 10:41