我想用一些附加的逻辑来做一个聚合请求,但是我不确定是否可能以及如何做。当我有以下文档时,如何请求数组中没有定义“类型”的位置?我尝试在类型上发送一个子聚合筛选器,但也尝试在该聚合上发送位置“20”get doc\u count 1。
在这种情况下,如何对匹配的聚合项执行一些逻辑操作?
文件:
//document1
{
"locations": [{
"code": "20",
"names": [{
"languageCode": "en-GB",
"value": "Amsterdam"
}
]
}, {
"type": {
"id": 25,
"names": [{
"languageCode": "en-GB",
"value": "area"
}
]
},
"code": "21",
"names": [{
"languageCode": "en-GB",
"value": "Amsterdam-South"
}
]
}
]
}
//Document 2
{
"locations": [{
"code": "22",
"names": [{
"languageCode": "en-GB",
"value": "DenHague"
}, {
"languageCode": "nl-NL",
"value": "DenHaag"
}
]
}
]
}
请求:
{
"aggs": {
"Filter_Location": {
"aggs": {
"SubType": {
"filter": {
"exists": {
"field": "locations.type"
}
}
} },
"terms": {
"field": "locations.code.keyword"
}
}
},
"size": 0
}
结果:
{
"aggregations": {
"Filter_Location": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "20",
"doc_count": 1,
"SubType": {
"doc_count": 1
},
"groupByAccoId": {
"value": 1
}
}, {
"key": "21",
"doc_count": 1,
"SubType": {
"doc_count": 1
},
"groupByAccoId": {
"value": 1
}
}, {
"key": "22",
"doc_count": 1,
"SubType": {
"doc_count": 0
},
"groupByAccoId": {
"value": 1
}
}
]
}
}
}
预期结果:
{
"aggregations": {
"Filter_Location": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "20",
"doc_count": 1,
"SubType": {
"doc_count": 0
}
}, {
"key": "21",
"doc_count": 1,
"SubType": {
"doc_count": 1
}
}, {
"key": "22",
"doc_count": 1,
"SubType": {
"doc_count": 0
}
}
]
}
}
}
1条答案
按热度按时间dddzy1tm1#
为了防止
locations
数组展平您需要将索引Map设置为nested
:接收文档后,此查询将获取所需的结果: