Ian
Ian

Reputation: 25366

Applying multiple filters, aggregated into a single metric with elasticsearch

Is there a way (possible aggregating the aggregate buckets) to apply multiple filters in an aggregate and have it return a single metric value?

Eg, here's an aggregation query where I'm getting the min/max prices for all products that have 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 to get both filters applied together into a single metric/bucket?

Upvotes: 3

Views: 2639

Answers (1)

bittusarkar
bittusarkar

Reputation: 6357

Yes there is. You need to use filter aggregation instead of filters aggregation. Query would be something like below:

{
    "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 a single metric for price_min and a single metric for price_max with both the filters applied.

Upvotes: 6

Related Questions