Sort

Sort and pagination

/ Sort, / Pagination, / Search

Sort and pagination #

Control which documents come back and in what order — sort, from/size, search_after/search_before, and total-hit accounting.

POST /my-collection/_search

How it works #

sort is an array of sort clauses evaluated in order; the first clause that produces a difference decides the order. Sorting on an indexed field lets the collector skip documents that cannot make the cut (early termination), so field sorts — and _doc above all — are cheaper than score sorts, not just differently ordered.

...

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.

...