有没有办法(可能聚合聚合桶)在聚合中应用多个过滤器并让它返回单个指标值?
例如,这是一个聚合查询,我在其中获取 option_id = 16 和 category.id = 870 的所有产品的最低/最高价格。
{
"aggregations": {
"prices": {
"filters": {
"filters": {
"category": {
"terms": {
"category.id": [ 870 ]
}
},
"option": {
"terms": {
"option_id": [ 16 ]
}
}
}
},
"aggregations": {
"price_min": {
"min": {
"field": "variant.price_total"
}
},
"price_max": {
"max": {
"field": "variant.price_total"
}
}
}
}
}
}
不幸的是,这给了我两个不同的值:
{
"doc_count": 1450674,
"prices": {
"buckets": {
"category": {
"doc_count": 42979,
"price_min": {
"value": 0.49
},
"price_max": {
"value": 39998.99
}
},
"store": {
"doc_count": 100961,
"price_min": {
"value": 0.06
},
"price_max": {
"value": 644000
}
}
}
}
}
有没有办法将两个过滤器一起应用到一个指标/桶中?
最佳答案
是的。您需要使用 filter
聚合而不是 filters
聚合。查询如下所示:
{
"aggregations": {
"prices": {
"filter": {
"bool": {
"must": [
{
"terms": {
"category.id": [
870
]
}
},
{
"terms": {
"option_id": [
16
]
}
}
]
}
},
"aggregations": {
"price_min": {
"min": {
"field": "variant.price_total"
}
},
"price_max": {
"max": {
"field": "variant.price_total"
}
}
}
}
}
}
这将为您提供 price_min
的单个指标和 price_max
的单个指标,同时应用了两个过滤器。
关于elasticsearch - 应用多个过滤器,使用 elasticsearch 聚合到单个指标中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096607/