liveBook · Manning

Filter context

As explained in Elasticsearch in Action, Second Edition

A filter context is an Elasticsearch query context in which matching determines inclusion without calculating relevance scores.

A filter context answers only whether a document matches. Elasticsearch returns matching documents without calculating relevance scores, so their _score is 0.0. A query determines the execution context: match queries generally use query context, while filter clauses use filter context. section 8.4.2

Filtering movie matches

To find movies with “Godfather” in the title without ranking them, place the match query inside a bool filter clause: GET movies/_search { "query": { "bool": { "filter": [ { "match": { "title": "Godfather" } } ] } } } The two matching movies remain included, but both receive a score of 0.0. Use this form when matching is sufficient and relevance ordering is unnecessary. section 8.4.2

Filter context also appears in compound queries:

  • The must_not clause excludes matching documents and does not alter scores produced by other clauses. section 11.3.5
  • Filter execution avoids the relevance-scoring algorithm and is therefore performant for yes-or-no criteria. section 11.3.6
  • Wrap a filter in constant_score when every matching document needs a chosen nonzero score, such as a boost for black TVs. section 11.4
Listing 8.4 A bool query with no score
GET movies/_search
{
  "query": {
    "bool": {
      "filter": [{
          "match": {
            "title": "Godfather"
          }
        }
      ]
    }
  }
}

Inspect the runnable bool query that places a movie match in filter context and demonstrates zero scores. section 8.4.2

Listing 11.17 A constant_score query that produces a static score
GET products/_search
{
  "query": {
    "constant_score": {     #1
      "filter": {           #2
          "range": {
            "user_ratings": {
              "gte": 4,
              "lte": 5
            }
          }
        },
      "boost": 5.0          #3
    }
  }
}

Inspect the constant_score form, which preserves filter-style matching while assigning a static boosted score. section 11.4

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains query and filter contexts through bool examples, stopping short of internal execution details.
Examples
Demonstrates Godfather movie matching, television exclusions, rating ranges, and boosted black-TV results.
What it assumes
Builds from basic match queries and introduces execution context before comparing scoring and non-scoring searches.
What you can do after
Apply it via Elasticsearch bool and constant_score queries to filter results, exclude matches, and assign optional static boosts.
How it connects
Connects filter context to query context, bool queries, must_not clauses, relevance scoring, and constant_score queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.