应用多个过滤器,使用elasticsearch聚合到单个度量标准中

Ian*_*Ian 3 elasticsearch

有没有办法(可能聚合聚合桶)在聚合中应用多个过滤器并让它返回单个度量值?

例如,这是一个聚合查询,其中我得到了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"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这给了我两个独立的价值观:

{
    "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
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法将两个过滤器一起应用到一个指标/桶中?

bit*_*kar 5

就在这里.您需要使用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"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这将为您提供单个指标price_min和单个指标price_max,同时应用这两个指标.