是否可以使用弹性搜索_count API并使用以下简短的ES模板来为brandId的所有活动查找sponsorships计数
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"
}
}
}
}
}
}发布于 2020-03-10 21:16:10
我找到了一种不用聚集的解决方案,从上面看,它似乎更准确,而且我可以使用_count API。
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "sponsorshipSets.sponsorships",
"query": {
"bool": {
"filter": {
"exists": {
"field": "sponsorshipSets.sponsorships"
}
}
}
}
}
},
{
"term": {
"brandId": "b1d28821-3730-4266-8f55-eb69596004fb"
}
}
]
}
}
}发布于 2020-03-10 02:06:18
过滤器聚合可以用于获取具有特定品牌Id的文档。两个嵌套聚合指向赞助和value_count聚合以获得计数。
查询
{
"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"
}
}
}
}
}
}
}
}https://stackoverflow.com/questions/60610328
复制相似问题