tsvector的Postgres词组搜索

时间:2018-07-16 21:31:14

标签: postgresql search

以下代码可用于词组搜索:

TextInput

有没有办法查看50%以上的单词是否匹配,或者有更好的方法呢?

例如:

SELECT *
FROM companies
WHERE name_tokens @@ to_tsquery(replace('Apple Corporation', ' ', ':* |'))

没有返回名为“ TIAA-CREF小型混合指数基金顾问”的公司,而是将&更改为|会产生太多无关的结果。

1 个答案:

答案 0 :(得分:1)

pg_trgm extension比全文搜索可能更适合您的需求,例如:

create extension if not exists pg_trgm;

with companies(name) as (
values 
    ('Small Companies Cooperation'),
    ('TIAA Access Small-Cap Blend Index Fund T1'),
    ('Access Business Corporation'),
    ('Index One Fundation')
)

select name, similarity(name, 'Access Small-Cap Blend Index Fund')
from companies
order by 2 desc

                   name                    | similarity 
-------------------------------------------+------------
 TIAA Access Small-Cap Blend Index Fund T1 |      0.825
 Index One Fundation                       |   0.232558
 Access Business Corporation               |       0.18
 Small Companies Cooperation               |   0.134615
(4 rows)

select name
from companies
where name % 'Access Small-Cap Blend Index Fund'

                   name                    
-------------------------------------------
 TIAA Access Small-Cap Blend Index Fund T1
(1 row)