This section covers sorting by a field. To sort by BM25 relevance score, see
BM25 Scoring.
ORDER BY...LIMIT:
- All
ORDER BYfields must be indexed. If they are text fields, they must use the literal tokenizer. - At least one ParadeDB text search operator must be present at the same level as the
ORDER BY...LIMIT. - The query must have a
LIMIT. - With the exception of
lower, ordering by expressions is not supported — only the raw fields themselves.
Custom Scan with a TopKScanExecState in the EXPLAIN output:
Expected Response
Expected Response
TopKScanExecState in the EXPLAIN output.
Tiebreaker Sorting
To guarantee stable sorting in the event of a tie, additional columns can be provided toORDER BY.
This is particularly important when sorting by pdb.score(...), as equal scores can result in non-deterministic orderings. As noted above, all tiebreaker columns must be indexed to receive the Top K optimization.
ParadeDB is currently able to handle 3
ORDER BY columns. If there are more
than 3 columns, the ORDER BY will not be efficiently executed by ParadeDB.Sorting by Text
If a text field is present in theORDER BY clause, it must be indexed with the literal or
literal normalized tokenizer.
Sorting by lowercase text using lower(<text_field>) is also supported. To enable this, the expression lower(<text_field>) must be indexed
with either the literal or literal normalized tokenizer. See indexing expressions for more information.
Sorting by Range
Postgres range columns (int4range, int8range, numrange, daterange, tsrange, tstzrange) can receive the Top K optimization when they appear as the leading ORDER BY key. ParadeDB compares range values using the same rules as Postgres range_cmp (empty ranges, unbounded bounds, and inclusive/exclusive endpoints).
Range columns are always indexed as fast fields, so no extra configuration is needed. If a range column appears in a later ORDER BY position, the whole ORDER BY falls back to a regular Postgres sort. Tiebreaker columns after a leading range key are fine.
Sorting by JSON
Ordering by a JSON subfield directly is not yet supported. For example, this query will not receive an optimized Top K scan:pdb.alias.
For example, to order by a numeric weight field within a JSON column: