Ranking and dictionaries
Overview
Ranking orders matching documents by a relevance score, not by proof of truth or quality. Dictionaries transform tokens, for example by stemming or removing stop words. Different analyzers change which documents match; relevance scores are not generally comparable between separate queries.
First example
SELECT rowid FROM sr_stem WHERE sr_stem MATCH 'index' ORDER BY rowid;Observed SQLite result (rows in JSON):
[ [ 1 ], [ 2 ], [ 3 ]]How it works
FTS5 bm25() is ordered ascending (better matches have numerically smaller scores); use rowid as a tie-breaker. Its Porter tokenizer stems English, unlike unicode61 alone. PostgreSQL ts_rank and ts_rank_cd normally sort descending, and English configuration uses dictionaries for stemming and stop words. Do not infer ranking performance from the tiny fixture. Render highlighted text as escaped text, not trusted HTML.
Practical example
SELECT rowid FROM sr_doc WHERE sr_doc MATCH 'index' ORDER BY bm25(sr_doc),rowid;Observed SQLite result (rows in JSON):
[ [ 2 ], [ 1 ]]Practice and mistakes
Practice: compare index against sr_doc and sr_stem. Hint 1: both have the same English documents. Hint 2: Porter reduces indexing to index. Expect 1,2 versus 1,2,3. Then inspect BM25 order; do not treat a negative score as an error.
PostgreSQL 17 example — NOT RUN
SELECT to_tsvector('english','indexing') @@ plainto_tsquery('english','index');Expected result, not executed: [[true]]