I'm using postgres FTS for prefix searching.
When a match occurs I want to favor an exact match over a prefix match.
A query of work:*
should order work
first and then workday
.
This query will give the same rank for both matches, so this won't work
SELECT ts_rank(to_tsvector('simple', 'work'), to_tsquery('simple', 'work:*'))
UNION ALL
SELECT ts_rank(to_tsvector('simple', 'workday'), to_tsquery('simple', 'work:*'));
-- result:
0.06079271
0.06079271
When I add an or clause for work
( trough | work
) , the rank for workday
suddenly becomes 0.
I'm not understanding why the or clause reduces the rank to 0, since there is still a matching term (work:*
) in the query
SELECT ts_rank(to_tsvector('simple', 'work'), to_tsquery('simple', 'work:* | work'))
UNION ALL
SELECT ts_rank(to_tsvector('simple', 'workday'), to_tsquery('simple', ' work:* | work'));
-- result:
0.06079271
0
When I switch the two or clauses positionally, the result is the same as not adding the clause at all:
SELECT ts_rank(to_tsvector('simple', 'work'), to_tsquery('simple', 'work | work:*'))
UNION ALL
SELECT ts_rank(to_tsvector('simple', 'workday'), to_tsquery('simple', 'work | work:*'));
-- result:
0.06079271
0.06079271
SQL fiddle: http://sqlfiddle.com/#!17/9eecb/89476
My goal is to better understand ts_rank
and why the ranking gets impacted in such a dramatic way, potentially reporting any bugs too postgres.
I can not find any mention of the order of the query terms them self to have any impact on ranking.
https://www.postgresql.org/docs/13/textsearch-controls.html#TEXTSEARCH-RANKING
note: I am using ranking to order the results because my query can contain an arbitrary amount of keywords.