Search code examples
elasticsearchtire

Facet counts aren't displaying correct numbers after filtering on a different facet


If you have two facets, let's say category and tag, initially the counts are correct, but they don't go down after filtering on one of the facets. The idea being that if there are no records within category X with tag Y, and we're filtering by category X, tag Y should have a facet count of 0.

Of course, other facet counts in the same term (other categories) facet counts don't go down because they would be an OR rather than an AND.


Solution

  • How are you filtering your facets? Like this?

    GET /_search
    {
      "query": {.....},
      "filter": { "term": { "some_field": "some_value"}},
      "facets: {.....}
    }
    

    If so, then it's not surprising. The top level filter parameter is for filtering your search results AFTER having calculated facets. In fact, in 1.0 it has been renamed to post_filter precisely because of this confusion.

    You want to use a filtered query instead:

    GET /_search
    {
      "query": {
        "filtered": {
          "query": {.....},
          "filter": { "term": { "some_field": "some_value"}}
        }
      },
      "facets: {.....}
    }