仅匹配 Postgres 全文搜索中的某些单词

Matching only some words in a Postgres full text search

我有兴趣使用postgres 作为我正在开发的网站的搜索引擎,但似乎postgres 在匹配ts_query 和ts_vector 类型时非常严格。如果 ts_vector 不包含查询中的所有项目,则匹配被拒绝。

例如,我希望查询 'Stack Overflow code' 与 Stack Overflow 站点摘要相匹配,但事实并非如此,因为单词 'code' 不存在,即使 'Stack' 'Overflow' 是。

SELECT 
to_tsvector('Stack Overflow is a question and answer site for professional and enthusiast programmers. It''s built and run by you as part of the Stack Exchange network of Q&A sites. With your help, we''re working together to build a library of detailed answers to every question about programming.')
@@ 
plainto_tsquery('english', 'Stack Overflow code')

Returns:

false

在我的用例中,我对完全匹配不感兴趣,因为用户将使用它来搜索网站。

当文档中只有部分查询时,有没有办法将某些内容计为匹配项?

那是因为 plainto_tsquery 将字符串分成单独的词素,并在它们之间放置 & (AND) 运算符。这意味着它匹配所有单词。

如果你想要| (OR)运算符,需要自己写"parser"。例如,您可以将所有出现的 ' ' 替换为 '|'

SELECT 
to_tsvector('Stack Overflow is a question and answer site for professional and enthusiast programmers. It''s built and run by you as part of the Stack Exchange network of Q&A sites. With your help, we''re working together to build a library of detailed answers to every question about programming.')
@@ 
to_tsquery('english', replace('Stack Overflow Code', ' ' , '|'));