ES中nest Join

nested 类型是一个特殊object数据类型,允许数组的object的字段可以被独立的查询出来。

数据类型是如何被封装的

在lucene中没有嵌套object的概念,所以ES的用一个简单的数据数据列表来表示一个复杂的层次数据实体,例如一个博客和评论的实体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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类型会被转化成一个数组的形式如下:

1
2
3
4
5
6
{
...
"commet.commet_user":["tom","john","lily"],
...
"commet.commet_location":["beijing","shanghai","shenzhen"],
}

而当执行查询的时候:

1
2
3
4
5
6
7
8
9
10
11
12
GET nesttest/_search
{
"query": {
"bool": {
"must": [
{ "match": { "commet.commet_user": "john" }},
{ "match": { "commet.commet_location": "shenzhen" }}
]
}
}
}

发现查询结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
...

"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类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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层