liveBook · Manning

Leaf query

As explained in Elasticsearch in Action, Second Edition

A leaf query is an individual Elasticsearch query that evaluates document fields or values and can serve as a component of a compound query.

A leaf query is a simple Elasticsearch Query DSL query that applies one search criterion to document fields or values. Match, term-level, range, and phrase queries are examples. section 8.7.4 section 8.9

Use a leaf query when the search requirement is singular:

Match a phrase in movie synopses

GET movies/_search { "query": { "match_phrase": { "synopsis": "A meek hobbit from the shire and eight companions" } } } The match_phrase object names the leaf-query type, while synopsis identifies the field searched. The query expresses one criterion. section 8.7.4

A leaf query does not combine multiple clauses. For requirements such as matching a title, excluding a rating, preferring an actor, and filtering another field, wrap several leaf queries in a compound query such as bool. section 8.7.4 section 11.3

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
            }
          }
        }
      ]
    }
  }
}

Compare three leaf queries—a product match, a resolution term, and a colour terms query—combined with a bool must clause. section 11.3.3

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines simple criteria and shows JSON examples, but defers advanced logical combination to compound queries.
Examples
Uses movie synopsis phrases, movie ratings and years, book authors, and product types, prices, resolutions, and colours.
What it assumes
Assumes a search request and query object, while distinguishing basic leaf criteria from compound query logic.
What you can do after
Apply it via Elasticsearch Query DSL to write a focused search against a document field or value.
How it connects
Connects full-text queries, term-level queries, compound queries, bool clauses, Query DSL, and search requests.

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.