Question

I'm using Elasticsearch and Nest to create a query for documents within a specific time range as well as doing some filter facets. The query looks like this:

{
  "facets": {
    "notfound": {
      "query": {
        "term": {
          "statusCode": {
            "value": 404
          }
        }
      }
    }
  },
  "filter": {
    "bool": {
      "must": [
        {
          "range": {
            "time": {
              "from": "2014-04-05T05:25:37",
              "to": "2014-04-07T05:25:37"
            }
          }
        }
      ]
    }
  }
}

In the specific case, the total hits of the search is 21 documents, which fits the documents within that time range in Elasticsearch. But the "notfound" facet returns 38, which fits the total number of ErrorDocuments with a StatusCode value of 404.

As I understand the documentation, facets collects data from withing the search. In this case, the "notfound" facet should never be able to return a count higher that 21.

What am I doing wrong here?

Was it helpful?

Solution

There's a distinct difference between filter/query/filtered_query/facet filter which is good to know.

Top level filter

{
    filter: {}
}

This acts as a post-filter, meaning it will filter the results after the query phase has ended. Since facets are part of the query phase filters do not influence the documents that are facetted over. Filters do not alter score and are therefor very cacheable.

Top level query

{
    query: {}
}

Queries influence the score of a document and are therefor less cacheable than filters. Queries run in the query phase and thus also influence the documents that are facetted over.

Filtered query

{
    query: {
        filtered: {
           filter: {}
           query: {}
        }
    }
}

This allows you to run filters in the query phase taking advantage of their better cacheability and have them influence the documents that are facetted over.

Facet filter

"facets" : {
    "<FACET NAME>" : {
        "<FACET TYPE>" : {
            ...
        },
        "facet_filter" : {
            "term" : { "user" : "kimchy"}
        }
    }
}

this allows you to apply a filter to the documents that the facet is run over. Remember that the it'll be a combination of the queryphase/facetfilter unless you also specify global:true on the facet as well.

Query Facet/Filter Facet

{
    "facets" : {
        "wow_facet" : {
            "query" : {
                "term" : { "tag" : "wow" }
            }
        }
    }
}

Which is the one that @thomasardal is using in this case which is perfectly fine, it's a facet type which returns a single value: the query hit count.

The fact that your Query Facet returns 38 and not 21 is because you use a filter for your time range.

You can fix this by either doing the filter in a filtered_query in the query phase or apply a facet filter(not a filter_facet) to your query_facet although because filters are cached better you better use facet filter inside you filter facet.

Confusingly Filter Facets are specified using .FacetFilter() on the search object. I will change this in 1.0 to avoid future confusion.

Sadly: .FacetFilter() and .FacetQuery() in NEST do not allow you to specify a facet filter like you can with other facets:

var results = typedClient.Search<object>(s => s
    .FacetTerm(ft=>ft
        .OnField("myfield")
        .FacetFilter(f=>f.Term("filter_facet_on_this_field", "value"))
    )
);

OTHER TIPS

You issue here is that you are performing a Filter Facet and not a normal facet on your query (which will follow the restrictions applied via the query filter). In the JSON, the issue is because of the "query" between the facet name "notfound" and the "terms" entry. This is telling Elasticsearch to run this as a separate query and facet on the results of this separate query and not your main query with the date range filter. So your JSON should look like the following:

 {
  "facets": {
    "notfound": {
      "term": {
        "statusCode": {
         "value": 404
        }
      }
    }
  },
  "filter": {
    "bool": {
      "must": [
        {
          "range": {
            "time": {
              "from": "2014-04-05T05:25:37",
              "to": "2014-04-07T05:25:37"
            }
          }
        }
      ]
    }
  }
}

Since I see you have this tagged with NEST as well, in your call using NEST, you are probably using FacetFilter on your search request, switch this to just Facet to get the desired result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top