2016-02-14 43 views
1

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

例如,我期望查询'堆栈溢出代码'匹配堆栈溢出网站摘要,但它并不是因为单词'代码'不存在,即使'堆栈'和'溢出'是。

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') 

返回:

false 

在我的使用情况下,我不感兴趣的精确匹配,因为这将被用户使用搜索网站。

当只有部分查询在文档中时,有什么方法可以将某些内容计算为匹配吗?

+0

也看到了smlar延长,同一作者的文本搜索替换写的' '所有出现 –

回答

3

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

如果您想要| (OR)运算符,则需要编写自己的“解析器”。例如,您可以用'|'

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', ' ' , '|'));