Range

Lesson 3 — Structure and numbers

/ Tutorial, / Sort, / Range

Lesson 3 — Structure and numbers #

Ranges, sorts, and cursor pagination over the sales collection (120 documents: type, price, qty, amount, merchant, status, date).

The use case #

Most queries are not about words — they are filters and ordering over the structured half of the document: prices, quantities, timestamps. And real interfaces page through result sets, sometimes deeply. The questions this lesson answers:

  • “Sales priced $45 to $60” / “everything from 2024” — range over a typed field.
  • “Top 5 by amount” — the canonical sort clause.
  • “Page 3000 of a big result set, without re-ranking the world” — cursor pagination (search_after), where from+size breaks down.
  • “Why did my filter miss the row with 45?” — the field-type system: 45 the number and "45" the keyword are different values.

The types behind the data #

Numbers are indexed as their real types (float for price, integers for qty), dates as epoch millis. That is what makes the queries below precise — see the field types reference for the full catalog.

...

Range aggregation

/ Range, / Aggregation

Range aggregation #

A multi-bucket aggregation over numeric fields where each bucket is a user-defined range. It answers “how many documents fall in each band?” — the bands are yours to name and place, unlike the regular grid of histogram.

How it works #

Each range is a half-open window: from is inclusive, to is exclusive, and either side may be omitted for unbounded. A document whose value v satisfies from <= v < to counts in that range — and because the test is per range, overlapping definitions put one document in several buckets at once.

...

Range query

/ Range, / Query

Range query #

Returns documents that contain terms within a provided range.

Use it for intervals: prices between two numbers, events inside a time window, names inside an alphabetical span — anything expressible as “from / to” bounds rather than a list of exact values.

Inside a nested query: supported — the bounds apply per array element.

How it works #

The engine picks the index by field type:

...