2016-02-14 40 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', ' ' , '|'));