Pg_search: how to prioritize an exact match?

The bottom line: When searching for texts with multiple occurrences of words similar to a search query, they get a higher rank than texts with one exact match.

Example: Let's say that a search query is “productivity”, then “organic production” gets a higher rank than “labor productivity” just because it contains two similar words, although the exact match of words is not fulfilled.

Question: What is the best way to prioritize records that have exact matches?

Here is my pg_scope:

pg_search_scope :search, :against => { :title => 'A', :search_string => 'B' }, :using => { :tsearch => { :dictionary => 'english', :any_word => 'true' } } 

thanks.

+6
source share
1 answer

If you can manipulate a search query, review the coverage density rating ( ts_rank_cd() instead of ts_rank() ) and its normalization parameter .

 SELECT v, ts_rank(to_tsvector('english', v), to_tsquery('english', 'productivity')) rank, ts_rank_cd(to_tsvector('english', v), to_tsquery('english', 'productivity')) rankcd, ts_rank_cd(to_tsvector('english', v), to_tsquery('english', 'productivity'), 4) rankcd4, ts_rank_cd(to_tsvector('english', v), to_tsquery('english', 'productivity'), 6) rankcd6 FROM ( VALUES ('production of organic products'::TEXT), ('labour productivity'), ('labour productivity with more unrelated words'), ('labour productivity with more unrelated words and again production'), ('production of productivity'), ('product production') ) d(v) 

SQLFiddle

However, I'm not sure how to configure pg_search_scope with normalization .

Alternatively, you may be interested in trigram search , they offer more accurate (character by character) results.

0
source

Source: https://habr.com/ru/post/970147/


All Articles