liveBook · Manning

Must clause

As explained in Elasticsearch in Action, Second Edition

A Boolean query clause in Elasticsearch that requires documents to match its inner query.

A must clause is a Boolean query clause that requires documents to match its inner queries. A positive match contributes to relevance scoring. section 2.5.2 section 2.5.1

Find TVs priced from $700 to $800

GET products/_search { "query": { "bool": { "must": [ { "match": { "product": "TV" } }, { "range": { "price": { "gte": 700, "lte": 800 } } } ] } } } Both leaf queries must match: the product must be a TV and its price must fall within the range. section 11.3.3

Use the clause to build increasingly specific search criteria:

  • Place one leaf query inside must for a focused requirement, such as matching books authored by Joshua Bloch. section 2.5.2
  • Add multiple queries as array entries to express AND logic across full-text, term-level, and range criteria. section 11.3.3 section 11.3
  • Combine must with must_not, should, and filter when requirements include exclusions, preferences, or non-scoring constraints. section 11.3.8
Listing 11.3 Finding TVs within a price range
GET products/_search
{
  "query": {
    "bool": {
      "must": [              #1
        {
          "match": {         #2
            "product": "TV"
          }
        },
        {
          "range": {         #3
            "price": {
              "gte": 700,
              "lte": 800
            }
          }
        }
      ]
    }
  }
}

Shows a runnable Elasticsearch request combining product matching with an inclusive numeric price range. section 11.3.3

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains single and multiple leaf queries with examples, then stops short of the broader scoring and advanced-query mechanics.
Examples
Uses Joshua Bloch books, TVs within a price range, and products matching resolution and color conditions.
What it assumes
Assumes basic Elasticsearch search requests and leaf queries while introducing bool-query structure through incremental examples.
What you can do after
Apply it via Elasticsearch bool-query JSON to require several search conditions and shape relevance-ranked results.
How it connects
Connects must clauses to bool queries, match queries, match_phrase queries, range queries, term queries, filter clauses, and relevance scoring.

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.