深层嵌套类型的 Elasticsearch 聚合
Elasticsearch aggregation of deep nested type
之前我问过问题。
那里的示例文档是一个简化的文档。这对我理解非嵌套类型与嵌套类型的聚合差异很有帮助。然而,简化隐藏了更多的复杂性,所以我必须在这里扩展这个问题。
所以我的实际文档更接近于以下内容:
"_source": {
"keyword": "my keyword",
"response": [
{
"results": [
{
"items": [
{
"prop": [
{
"item_property_1": ["A"],
}
]
( ... other properties )
},
{
"prop": [
{
"item_property_1": ["B"],
}
]
( ... other properties )
},
( ... other items )
]
}
],
( ... other properties )
}
]
}
所以我保留了关键属性 keyword
、items
和 item_property_1
,但隐藏了许多其他使情况复杂化的东西。首先,请注意,与引用的问题相比,有很多额外的嵌套:在根和 "items" 之间,以及在 "items" 和 "item_property_1" 之间。此外,还要注意属性 response
和 results
都是具有单个元素的数组。这很奇怪,但就是这样:-)
现在,这个问题与上面引用的问题不同的原因是我尝试了接受的答案(它确实适用于那里的例子),但它在这里不起作用。也就是说,如果我使用映射:
"items": {
"type":"nested",
"properties": {
"prop": {
"properties": {
"item_property_1": {
"type": "string",
"index": "not_analyzed"
},
}
}
}
}
那么聚合不起作用。它 returns 零命中。
我稍后会编辑并提供一个随时可用的样本批量插入。
编辑:
好吧,下面我展示了三个查询,分别是:映射,批量插入和聚合(零命中)
映射("type":"nested"
如前一个回答的问题所示)
PUT /test2/_mapping/test3
{
"test3": {
"properties": {
"keyword": {
"type": "string",
"index": "not_analyzed"
},
"response": {
"properties": {
"results": {
"properties": {
"items": {
"type": "nested",
"properties": {
"prop": {
"properties": {
"item_property_1": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
}
}
}
批量投放:
PUT /test2/test3/_bulk
{ "index": {}}
{ "keyword": "my keyword", "response": [ { "results": [ { "items": [ { "prop": [ {"item_property_1": ["A"]} ] }, { "prop": [ {"item_property_1": ["B"]} ] }, { "prop": [ {"item_property_1": ["A"]} ] } ] } ] } ]}
{ "index": {}}
{ "keyword": "different keyword", "response": [ { "results": [ { "items": [ { "prop": [ {"item_property_1": ["A"]} ] }, { "prop": [ {"item_property_1": ["C"]} ] } ] } ] } ]}
聚合(零命中):
POST /test2/test3/_search
{
"size":0,
"aggregations": {
"item_property_1_count": {
"terms":{
"field":"item_property_1"
}
}
}
}
这与之前的答案并没有什么不同。您只需要稍微修改字段名称以考虑额外的嵌套。除此之外,映射中无需更改任何内容。请注意,此查询在没有映射更改的情况下工作只是因为 response
和 results
都是具有单个元素的数组,如果不是这种情况,它会更加复杂并且需要映射更改和不同的询问。
查询现在看起来像这样:
{
"size": 0,
"aggregations": {
"by_keyword": {
"terms": {
"field": "keyword"
},
"aggs": {
"prop_1_count": {
"nested": {
"path": "response.results.items"
},
"aggs": {
"prop_1": {
"terms": {
"field": "response.results.items.prop.item_property_1"
}
}
}
}
}
}
}
}
结果:
{
...
"aggregations" : {
"by_keyword" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "different keyword",
"doc_count" : 1,
"prop_1_count" : {
"doc_count" : 2,
"prop_1" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "A",
"doc_count" : 1
}, {
"key" : "C",
"doc_count" : 1
} ]
}
}
}, {
"key" : "my keyword",
"doc_count" : 1,
"prop_1_count" : {
"doc_count" : 3,
"prop_1" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "A",
"doc_count" : 2
}, {
"key" : "B",
"doc_count" : 1
} ]
}
}
} ]
}
}
}
之前我问过
那里的示例文档是一个简化的文档。这对我理解非嵌套类型与嵌套类型的聚合差异很有帮助。然而,简化隐藏了更多的复杂性,所以我必须在这里扩展这个问题。
所以我的实际文档更接近于以下内容:
"_source": {
"keyword": "my keyword",
"response": [
{
"results": [
{
"items": [
{
"prop": [
{
"item_property_1": ["A"],
}
]
( ... other properties )
},
{
"prop": [
{
"item_property_1": ["B"],
}
]
( ... other properties )
},
( ... other items )
]
}
],
( ... other properties )
}
]
}
所以我保留了关键属性 keyword
、items
和 item_property_1
,但隐藏了许多其他使情况复杂化的东西。首先,请注意,与引用的问题相比,有很多额外的嵌套:在根和 "items" 之间,以及在 "items" 和 "item_property_1" 之间。此外,还要注意属性 response
和 results
都是具有单个元素的数组。这很奇怪,但就是这样:-)
现在,这个问题与上面引用的问题不同的原因是我尝试了接受的答案(它确实适用于那里的例子),但它在这里不起作用。也就是说,如果我使用映射:
"items": {
"type":"nested",
"properties": {
"prop": {
"properties": {
"item_property_1": {
"type": "string",
"index": "not_analyzed"
},
}
}
}
}
那么聚合不起作用。它 returns 零命中。
我稍后会编辑并提供一个随时可用的样本批量插入。
编辑: 好吧,下面我展示了三个查询,分别是:映射,批量插入和聚合(零命中)
映射("type":"nested"
如前一个回答的问题所示)
PUT /test2/_mapping/test3
{
"test3": {
"properties": {
"keyword": {
"type": "string",
"index": "not_analyzed"
},
"response": {
"properties": {
"results": {
"properties": {
"items": {
"type": "nested",
"properties": {
"prop": {
"properties": {
"item_property_1": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
}
}
}
批量投放:
PUT /test2/test3/_bulk
{ "index": {}}
{ "keyword": "my keyword", "response": [ { "results": [ { "items": [ { "prop": [ {"item_property_1": ["A"]} ] }, { "prop": [ {"item_property_1": ["B"]} ] }, { "prop": [ {"item_property_1": ["A"]} ] } ] } ] } ]}
{ "index": {}}
{ "keyword": "different keyword", "response": [ { "results": [ { "items": [ { "prop": [ {"item_property_1": ["A"]} ] }, { "prop": [ {"item_property_1": ["C"]} ] } ] } ] } ]}
聚合(零命中):
POST /test2/test3/_search
{
"size":0,
"aggregations": {
"item_property_1_count": {
"terms":{
"field":"item_property_1"
}
}
}
}
这与之前的答案并没有什么不同。您只需要稍微修改字段名称以考虑额外的嵌套。除此之外,映射中无需更改任何内容。请注意,此查询在没有映射更改的情况下工作只是因为 response
和 results
都是具有单个元素的数组,如果不是这种情况,它会更加复杂并且需要映射更改和不同的询问。
查询现在看起来像这样:
{
"size": 0,
"aggregations": {
"by_keyword": {
"terms": {
"field": "keyword"
},
"aggs": {
"prop_1_count": {
"nested": {
"path": "response.results.items"
},
"aggs": {
"prop_1": {
"terms": {
"field": "response.results.items.prop.item_property_1"
}
}
}
}
}
}
}
}
结果:
{
...
"aggregations" : {
"by_keyword" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "different keyword",
"doc_count" : 1,
"prop_1_count" : {
"doc_count" : 2,
"prop_1" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "A",
"doc_count" : 1
}, {
"key" : "C",
"doc_count" : 1
} ]
}
}
}, {
"key" : "my keyword",
"doc_count" : 1,
"prop_1_count" : {
"doc_count" : 3,
"prop_1" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "A",
"doc_count" : 2
}, {
"key" : "B",
"doc_count" : 1
} ]
}
}
} ]
}
}
}