[英]Find the count of nested.nested Elastic Search documents
Is it possible using the Elastic Search _count
API and having the following abbreviated ES template to find the count of sponsorships
for all the campaigns by brandId
?是否有可能使用弹性搜索_count
API并具有以下简称ES模板找到的计数sponsorships
全部由活动brandId
?
sponsorshipSets
and sponsorships
are optional so it can be null
. sponsorshipSets
和sponsorships
是可选的,所以它可以是null
。
{
"index_patterns": "campaigns*",
"order": 4,
"version": 4,
"aliases": {
"campaigns": {
}
},
"settings": {
"number_of_shards": 5
},
"mappings": {
"dynamic": "false",
"properties": {
"brandId": {
"type": "keyword"
},
"sponsorshipSets": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
},
"sponsorships": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
}
}
}
}
}
}
filter aggregation can be used to fetch docs with certain brand Id.过滤器聚合可用于获取具有特定品牌 ID 的文档。 Two Nested aggregations to point to sponsorship and value_count aggregation to get the count.两个嵌套聚合指向赞助和 value_count 聚合以获取计数。
Query询问
{
"aggs": {
"selected_brand": {
"filter": {
"term": {
"brandId": "1"
}
}
},
"sponsorshipSets": {
"nested": {
"path": "sponsorshipSets"
},
"aggs": {
"sponsorships": {
"nested": {
"path": "sponsorshipSets.sponsorships"
},
"aggs": {
"count": {
"value_count": {
"field": "sponsorshipSets.sponsorships.id"
}
}
}
}
}
}
}
}
I found a solution without using Aggregations, it seems more accurate from the above and I can use the _count
API.我找到了一个不使用聚合的解决方案,从上面看起来更准确,我可以使用_count
API。
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "sponsorshipSets.sponsorships",
"query": {
"bool": {
"filter": {
"exists": {
"field": "sponsorshipSets.sponsorships"
}
}
}
}
}
},
{
"term": {
"brandId": "b1d28821-3730-4266-8f55-eb69596004fb"
}
}
]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.