Lesson 3 — Structure and numbers

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.

Ranges #

A range query takes any combination of gt/gte/lt/lte:

SQL spells a range with the operators you already know (BETWEEN for inclusive bounds):

Dates work the same way — 92 sales in 2024, 28 in 2025:

Sorting #

The canonical sort clause names the field, direction, and where missing values land ( sort reference):

The SQL spelling is ORDER BY … DESC LIMIT n — same order, same page size:

Add a second clause to break ties, and "_doc" or "_score" when you want index order or relevance.

Cursor pagination #

from+size re-ranks everything before the offset — fine for page 3, wrong for page 3000. Cursors (search_after) jump straight to the position after a sort-key tuple. Take sort values from the last hit of the page above and feed them back:

search_before pages backwards the same way. For a page-stable view across requests, open a point-in-time snapshot first — see API conventions.

Exact sets and existence #

terms matches a set of values; exists checks presence:

50 — the 30 hats plus the 20 mugs you will meet again in the aggregation lesson.

IN is the SQL set; IS NULL / IS NOT NULL cover exists:

What you learned #

  • Ranges on numbers and dates, with explicit epoch millis.
  • Canonical sort clauses and the cheap pseudo-fields.
  • search_after/search_before cursors beat deep from.
  • terms for sets, exists for presence.
  • The SQL lane mirrors all of it: comparison operators for ranges, ORDER BY … LIMIT for sorts, IN/IS NULL for sets and presence — see the SQL syntax page.

Next: Lesson 4 — aggregations.

Calendar October 6, 2026
Edit Edit this page