Elasticsearch嵌套查询
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
一、背景
最近在做基于宴會(huì)廳檔期的商戶搜索推薦時(shí),如果用傳統(tǒng)平鋪式的mapping結(jié)構(gòu),無法滿足需求場(chǎng)景,于是用到了Elasticsearch支持的Nested(嵌套)查詢。
二、普通對(duì)象與嵌套對(duì)象的索引異同
如果一個(gè)對(duì)象不是嵌套類型,那么以如下原數(shù)據(jù)為例:
PUT /my_index/blogpost/1 { "title":"Nest eggs", "body": "Making your money work...", "tags": [ "cash", "shares" ], "comments":[ { "name": "John Smith", "comment": "Great article", "age": 28, "stars": 4, "date": "2014-09-01" }, { "name": "Alice White", "comment": "More like this please", "age": 31, "stars": 5, "date": "2014-10-22" } ] }由于是json格式的結(jié)構(gòu)化文檔,es會(huì)平整成索引內(nèi)的一個(gè)簡(jiǎn)單鍵值格式,如下:
{ "title": [ eggs, nest ], "body": [ making, money, work, your ], "tags": [ cash, shares ], "comments.name": [ alice, john, smith, white ], "comments.comment": [ article, great, like, more, please, this ], "comments.age": [ 28, 31 ], "comments.stars": [ 4, 5 ], "comments.date": [ 2014-09-01, 2014-10-22 ] }這樣的話,像這種john/28,Alice/31間的關(guān)聯(lián)性就丟失了,Nested Object就是為了解決這個(gè)問題。
將comments指定為Nested類型,如下mapping:
curl -XPUT 'localhost:9200/my_index' -d ' { "mappings":{ "blogpost":{ "properties":{ "comments":{ "type":"nested", //聲明為nested類型"properties":{ "name": {"type":"string"}, "comment": { "type": "string"}, "age": { "type": "short"}, "stars": { "type": "short"}, "date": { "type": "date"} } } } } } }這樣,每一個(gè)nested對(duì)象將會(huì)作為一個(gè)隱藏的單獨(dú)文本建立索引,進(jìn)而保持了nested對(duì)象的內(nèi)在關(guān)聯(lián)關(guān)系,如下:
{ ① "comments.name": [ john, smith ], "comments.comment": [ article, great ], "comments.age": [ 28 ], "comments.stars": [ 4 ], "comments.date": [ 2014-09-01 ] } { "comments.name": [ alice, white ], "comments.comment": [ like,more,please,this], "comments.age": [ 31 ],"comments.stars": [ 5 ], "comments.date": [ 2014-10-22 ] } { "title": [ eggs, nest ], "body": [ making, money, work, your ], "tags": [ cash, shares ] } ①nested object三、嵌套對(duì)象的查詢
命令查詢(輸出結(jié)果1):
curl -XGET localhost:9200/yzsshopv1/shop/_search?pretty -d '{"query" : {"bool" : {"filter" : {"nested" : {"path":"hallList","query":{"bool":{"filter":{"term":{"hallList.capacityMin" : "11"}}}}}}}}}' {"took" : 3,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.0,"hits" : [ {"_index" : "yzsshopv1","_type" : "shop","_id" : "89999988","_score" : 0.0,"_source" : {"cityId" : "1","shopName" : "xxxx婚宴(yyyy店)","shopId" : "89999988","categoryId" : [ "55", "165", "2738" ],"hallList" : [ {"hallId" : "20625","schedule" : ["2017-11-10", "2017-11-09"],"capacityMax" : 16,"capacityMin" : 12}, {"hallId" : "21080","schedule" : [ "2017-12-10", "2017-09-09", "2017-02-25"],"capacityMax" : 20,"capacityMin" : 11} ],"wedHotelTagValue" : [ "12087", "9601", "9603", "9602" ],"regionId" : [ "9", "824" ]}} ]} }java api查詢封裝:
BoolQueryBuilder boolBuilder = new BoolQueryBuilder(); NestedQueryBuilder nestedQuery = new NestedQueryBuilder("hallList", new TermQueryBuilder("hallList.capacityMin","11")); //注意:除path之外,fieldName也要帶上path (hallList)boolBuilder.filter(nestedQuery); searchRequest.setQuery(boolBuilder); //設(shè)置查詢條件java api輸出字段封裝:
searchRequest.addField("shopId"); searchRequest.addField("hallList. schedule"); searchRequest.addField("hallList.capacityMin"); searchRequest.addField("hallList.capacityMax");如果輸出的outputField為searchRequest.addField("hallList"),則會(huì)報(bào)錯(cuò):illegal_argument_exception,reason:field [hallList] isn't a leaf field;
如果輸出的outputField為searchRequest.addField("capacityMin"),則不報(bào)錯(cuò),但沒有capacityMin字段的值;
正確調(diào)用search后的輸出結(jié)果(輸出結(jié)果2):
{"took" : 8,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.0,"hits" : [{"_index" : "yzsshopv1","_type" : "shop","_id" : "89999988","_score" : 0.0,"fields" : {"shopId" : [ "89999988" ],"hallList.hallId" : [ "20625", "21080"],"hallList.capacityMin" : [12, 11 ],"hallList.capacityMax" : [16, 20 ],"hallList.schedule" : [ "2017-11-10", "2017-11-09", "2017-12-10", "2017-09-09", "2017-02-25"]}}]} }對(duì)比輸出結(jié)果1和2發(fā)現(xiàn),命令輸出嵌套對(duì)象結(jié)果1沒問題,但通過java api輸出結(jié)果2時(shí),嵌套對(duì)象內(nèi)部的關(guān)系也會(huì)打亂,比如hallList.schedule字段,無法區(qū)分到底哪些值屬于hallList.hallId-20625,哪些屬于21080。
//============以下更新20170331===========
經(jīng)過后續(xù)調(diào)試,發(fā)現(xiàn)要讓java api輸出正確結(jié)果的嵌套對(duì)象,不能通過searchRequest.addField的方式,因?yàn)?strong>嵌套對(duì)象并不是葉子節(jié)點(diǎn),需要通過以下的方式添加輸出字段:
searchRequest.setFetchSource(new String[]{"shopId","hallList"},new String[]{});還有一個(gè)不足點(diǎn)是: 嵌套查詢請(qǐng)求返回的是整個(gè)文本,而不僅是匹配的nested文本。
四、參考文檔
轉(zhuǎn)載于:https://my.oschina.net/weiweiblog/blog/1572727
總結(jié)
以上是生活随笔為你收集整理的Elasticsearch嵌套查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++面试题目(五)
- 下一篇: NVIDIA控制面板打不开