开发者

How can I get postgresql text searching to use search term order in rank

开发者 https://www.devze.com 2023-02-13 03:05 出处:网络
The following PostgreSQL text search select ID, DISPLAY_NAME, ts_rank_cd(to_tsvector(\'english\', display_name), query) as RANK

The following PostgreSQL text search

select 
    ID, DISPLAY_NAME, 
    ts_rank_cd(to_tsvector('english', display_name), query) as RANK
from 
    my_table, 
    to_tsquery('english', 'John:*&Bernard:*') as query
where 
    to_tsvector('english', display_name) @@ query
    order by RANK DESC

produces

ID    DISPLAY_NAME         RANK   
========开发者_运维问答=============================
82683 "BERNARD JOHN SMBZh" 0.05
63815 "BERNARD JOHN []zkP" 0.05
68204 "BERNARD JOHN uPmYB" 0.05
29666 "John Bernard iECx"  0.05
44256 "John Bernard DpIff" 0.05
52601 "BERNARD JOHN ivRTX" 0.05
80250 "BERNARD JOHN b'nVp" 0.0430677

but what I really would like is for the "John Bernard*" records to have a higher rank because the terms in the "document" appear in the same order as the query. Is this possible?

e.g. A result like this:

ID    DISPLAY_NAME         RANK   
=====================================
29666 "John Bernard iECx"  0.10
44256 "John Bernard DpIff" 0.10
82683 "BERNARD JOHN SMBZh" 0.05
63815 "BERNARD JOHN []zkP" 0.05
68204 "BERNARD JOHN uPmYB" 0.05
52601 "BERNARD JOHN ivRTX" 0.05
80250 "BERNARD JOHN b'nVp" 0.0430677

Cheers Craig


I think you will have to consider a solution involving another ranking mechanism alongside tsearch as it does not handle phrases.

How about something like:

create table my_table(id serial primary key, display_name text);
insert into my_table(display_name) values ('John Bernard iECx'), 
                                          ('John Bernard DpIff'), 
                                          ('BERNARD JOHN SMBZh'), 
                                          ('BERNARD JOHN b''nVp');
select
    ID, DISPLAY_NAME,
    ts_rank_cd(to_tsvector('english', display_name), query)
      *case when display_name~*'.*john bernard.*' then 2 else 1 end as RANK
from
    my_table,
    to_tsquery('english', 'John:*&Bernard:*') as query
where
    to_tsvector('english', display_name) @@ query
    order by RANK DESC;

producing:

 id |    display_name    |       rank
----+--------------------+-------------------
  1 | John Bernard iECx  | 0.200000002980232
  2 | John Bernard DpIff | 0.200000002980232
  3 | BERNARD JOHN SMBZh | 0.100000001490116
  4 | BERNARD JOHN b'nVp | 0.100000001490116
0

精彩评论

暂无评论...
验证码 换一张
取 消