3

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.

Nameless One
  • 1,615
  • 2
  • 23
  • 39
MoShe
  • 6,197
  • 17
  • 51
  • 77

2 Answers2

3

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.

Community
  • 1
  • 1
Pavel Vasilev
  • 1,002
  • 1
  • 10
  • 11
3

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

user3775217
  • 4,675
  • 1
  • 22
  • 33
  • 1
    But what if I want to apply other aggregations to the bool query specified in the post_filter? It should not be possible then, right? – Theo Wittkovskiy Aug 23 '18 at 14:27