Applying multiple filters aggregated into a single metric with elasticsearch

Is there a way (perhaps aggregating aggregate buckets) to apply multiple filters in an aggregate and return one measure value?

For example, here's an aggregation query where I get the min / max prices for all products with option_id = 16 and 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"
                    }
                }
            }
        }
    }
}

      

This unfortunately gives me two separate values:

{
    "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
                }
            }
        }
    }
}

      

Is there a way for both filters to be combined into a single metric / bucket?

+3


source to share


1 answer


Yes there is. You should use filter

aggregation instead of aggregation filters

. The request will look something like this:

{
    "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"
                    }
                }
            }
        }
    }
}

      



This will give you one metric for price_min

and one metric for price_max

with both filters applied.

+5


source







All Articles