之前我问过this https://stackoverflow.com/questions/31841542/elasticsearch-metric-aggregation-number-of-elements-in-array/31846457?noredirect=1#comment51642542_31846457问题。
示例文档是一个简化的文档。这对我了解非嵌套类型与嵌套类型的聚合差异很有帮助。然而,简化隐藏了进一步的复杂性,所以我必须在这里扩展这个问题。
所以我的实际文件更接近以下内容:
"_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
, and item_property_1
,但隐藏了许多其他使情况变得复杂的事情。首先,请注意,与引用的问题相比,有很多额外的嵌套:在根和“items”之间,以及“items”和“item_property_1”之间。此外,还请注意属性response
and results
都是具有单个元素的数组。这很奇怪,但事实就是这样:-)
现在,这个问题与上面引用的问题不同的原因是我尝试了接受的答案(它确实适用于那里的示例),但在这里不起作用。也就是说,如果我使用以下映射:
"items": {
"type":"nested",
"properties": {
"prop": {
"properties": {
"item_property_1": {
"type": "string",
"index": "not_analyzed"
},
}
}
}
}
那么聚合不起作用。它返回零命中。
我稍后将进行编辑并提供一个随时可用的示例批量插入。
编辑:
好吧,下面我展示了三个查询,分别是:映射、批量插入和聚合(零命中)
映射(与"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"
}
}
}
}