Tomer
Tomer

Reputation: 2448

Filtered query - retrieve the number of filtered documents per filter

When executing a filtered query with 3 filters, is there a way to know which filter, filtered how many documents?

I have like 20 more filters, and sometimes only one of the filter is problematic filtering all the documents, I would like to get indication for this.

Does this make sense?

Upvotes: 0

Views: 56

Answers (1)

Val
Val

Reputation: 217504

Yes, you can do it with a filters aggregation. Put each of your filters in the inner filters hash and in the response you'll see the doc_count matching each filter.

{
  "size": 0,
  "aggs": {
    "messages": {
      "filters": {
        "filters": {
          "filter1": {
            "term": {
              "field1": "sample1"
            }
          },
          "filter2": {
            "term": {
              "field2": "sample2"
            }
          }
        }
      }
    }
  }
}

Then when you have that information, it's a good idea to rework your query and order your filters starting with the ones selecting the fewest documents, so that the next filters down the chain work on as few document as possible.

Upvotes: 1

Related Questions