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层