ES中nest join
- 2020 年 1 月 21 日
- 筆記
nested
類型是一個特殊object
數據類型,允許數組的object
的欄位可以被獨立的查詢出來。
數據類型是如何被封裝的
在lucene中沒有嵌套object
的概念,所以ES的用一個簡單的數據數據列表來表示一個複雜的層次數據實體,例如一個部落格和評論的實體:
PUT nesttest/_doc { "blog_title": "開篇-es的nest的使用", "blog_content": "從本篇文章開始,我會分享給大家canvas繪製的各種基礎圖形和酷炫的圖形", "tags": [ "Java", "es" ], "hit_count": 5, "commet": [ { "commet_user": "tom", "commet_content": "good Job", "commet_time": "2019-02-26", "commet_location": "beijing" }, { "commet_user": "john", "commet_content": "clearly,tks", "commet_time": "2019-02-23" , "commet_location": "shanghai" }, { "commet_user": "lily", "commet_content": "it's too hard ", "commet_time": "2019-02-22" , "commet_location": "shenzhen" } ], "create_time": "2019-02-26" }
其中commet
類型會被轉化成一個數組的形式如下:
{ ... "commet.commet_user":["tom","john","lily"], ... "commet.commet_location":["beijing","shanghai","shenzhen"], }
而當執行查詢的時候:
GET nesttest/_search { "query": { "bool": { "must": [ { "match": { "commet.commet_user": "john" }}, { "match": { "commet.commet_location": "shenzhen" }} ] } } }
發現查詢結果如下:
{ ... "hits" : [ { "_index" : "nest", "_type" : "_doc", "_id" : "pe0iKWkBulkJdQfMSgyV", "_score" : 0.5753642, "_source" : { "blog_title" : "2019-01-05", "blog_content" : "從本篇文章開始,我會分享給大家canvas繪製的各種基礎圖形和酷炫的圖形", "tags" : [ "Java", "es" ], "hit_count" : 5, "commet" : [ { "commet_user" : "tom", "commet_content" : "good Job", "commet_time" : "2019-02-26", "commet_location" : "beijing" }, { "commet_user" : "john", "commet_content" : "clearly,tks", "commet_time" : "2019-02-23", "commet_location" : "shanghai" }, { "commet_user" : "lily", "commet_content" : "it's too hard ", "commet_time" : "2019-02-22", "commet_location" : "shenzhen" } ], "create_time" : "2019-02-26" } } ] } }
上面把所有的結果都列出來了, 但是 john
的不在shenzhen
啊?所以需要把commet
定義為nested
類型。
定義nested類型
PUT nest_new/_mapping/_doc { "properties": { "blog_title": { "type": "text" }, "blog_content": { "type": "text" }, "tags": { "type": "keyword" }, "hit_count": { "type": "keyword" }, "commet": { "type": "nested",//這裡 "properties": { "commet_user": { "type": "text" }, "commet_content": { "type": "text" }, "commet_time": { "type": "date" }, "commet_location": { "type": "keyword" } } }, "create_time": { "type": "date" }, "datachange_lasttime": { "format": "strict_date_optional_time||epoch_millis", "type": "date" } } }
ES嵌套深度被限制在50層