Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - exclude filter from aggregations

I query the following:

{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "agent_id": [
            "58becc297513311ad81577eb"
          ]
        }
      }
    }
  },
  "aggs": {
    "agent_id": {
      "terms": {
        "field": "agent_id"
      }
    }
  }
}

I would like the aggregation to be excluded from the filter. In solr there is an option to tag a filter and use this tag to exclude this filter from the fact query.

How can I do the same in Elasticsearch.

like image 854
MoShe Avatar asked Jan 19 '26 02:01

MoShe


2 Answers

One way to approach this problem is to use post_filter as described here.

It might be performance concern, so if it doesn't fit your SLA there is alternative approach using global bucket and described here.

like image 86
Pavel Vasilev Avatar answered Jan 21 '26 06:01

Pavel Vasilev


You can use post_filter for elasticsearch. Post filter excludes the scope of the filters from the aggregations and is perfect to build an eCommerce search for drilled down aggregations count on filters

you can build a query like the following

{
    "aggs": {
        "agent_id": {
            "terms": {
                "field": "agent_id",
                "size": 10
            }
        }
    },
    "post_filter": {
        "bool": {
            "terms": {
                "agent_id": [
                    "58becc297513311ad81577eb"
                ]
            }
        }
    }
}

Thanks

like image 41
user3775217 Avatar answered Jan 21 '26 06:01

user3775217



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!