liveBook · Manning

Query context

As explained in Elasticsearch in Action, Second Edition

A query context is an Elasticsearch execution mode that evaluates whether documents match and how well they match, producing relevance scores.

A query context evaluates whether documents match and how well they match. Elasticsearch selects the context from the query: full-text match queries run in query context and produce relevance scores. section 8.4.2

Finding Godfather movies

GET movies/_search { "query": { "match": { "title": "Godfather" } } } Both matching movies receive an _score. “The Godfather” scores higher than “The Godfather: Part II” because the matching term appears in the shorter title. section 8.4.2

Use query context when relevance affects the result order:

  • A match query searches analyzed field values and scores documents according to how well they match. section 8.4.2
  • In a bool query, must and should clauses run in query context; should clauses can increase a matching document’s score. section 11.3.6
  • A filter clause answers only whether documents match, returning a score of 0.0 instead of calculating relevance. section 8.4.2
Listing 8.3 Match query executed in a query context
GET movies/_search
{
  "query": {
    "match": {
      "title": "Godfather"
    }
  }
}

Open the complete REST request for the Godfather match query and its query-context execution. section 8.4.2

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains automatic context selection, relevance scores, and bool-clause behavior, while deferring the scoring algorithm’s internals.
Examples
Demonstrates Godfather movie matching and TV searches using bool must and should clauses.
What it assumes
Assumes basic Elasticsearch search requests and introduces execution contexts through match, filter, and bool-query examples.
What you can do after
Evaluate relevance scores and write match or bool queries that rank documents according to matching strength.
How it connects
Connects match queries, bool queries, relevance scoring, filter context, and Query DSL.

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.