liveBook · Manning

Range query

As explained in Elasticsearch in Action, Second Edition

A query that retrieves documents whose field values fall within specified lower and upper bounds.

A range query retrieves documents whose field values fall between lower and upper bounds. It works with numbers, dates, ratings, prices, and other structured attributes. section 2.4.2 section 9.6

Use these operators to define the range:

  • `gt` means greater than; `gte` means greater than or equal to. section 9.6
  • `lt` means less than; `lte` means less than or equal to. section 9.6
  • Combine bounds to create inclusive or exclusive intervals, such as ratings from 9.0 through 9.5 or earnings greater than 350 and less than 400. section 9.6 section 5.5.3
  • Apply ranges to dates, sort the results, or use date math such as `now-1y` and an anchor date followed by `||-2d`. section 9.6
Listing 9.13 Fetching movies with a specified range of ratings
GET movies/_search
{
  "query": {
    "range": {
      "rating": {
        "gte": 9.0,
        "lte": 9.5
      }
    }
  }
}

A focused JSON example retrieves movies with ratings from 9.0 to 9.5 using inclusive lower and upper bounds. section 9.6

Combine a range with other criteria

A Boolean query can require both a product match and a price range. For example, matching `TV` and requiring a price from 700 through 800 narrows the results to qualifying televisions. A range can also appear inside `must_not` to exclude values below a threshold, or inside `_delete_by_query` to remove documents in a range. section 11.3.3 section 2.5.3 section 5.5.3

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains bounds, operators, sorting, date math, Boolean composition, and deletion, stopping short of deeper query internals.
Examples
Demonstrates book ratings, movie ratings and release dates, TV prices, flight times, and movie earnings.
What it assumes
Assumes structured fields and basic Elasticsearch queries while teaching range operators and date expressions through concrete examples.
What you can do after
Apply it via Elasticsearch query DSL to search, combine, sort, exclude, or delete documents matching value ranges.
How it connects
Connects range queries with term-level search, Boolean queries, date math, sorting, full-text search, and delete-by-query.

Also explained in

Compare how 3 books explain range query →

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.