Postgres全文搜索,在一列中包含多个单词

问题描述 投票:0回答:1

我想在postgres数据库上创建全文选择查询。例如,我想查找所有标题为“ Harry Potter”的书。如果我只是通过全文搜索查找Harry,它将运行很快。例如

SELECT * FROM books WHERE to_tsvector('simple', cast(title AS text)) @@ plainto_tsquery('simple',cast('harry' AS text))

但是,如果我尝试合并像Harry Potter这样的标题,则我的查询用完了时间(例如一分钟)

SELECT * FROM books WHERE to_tsvector('simple', cast(title AS text)) @@ plainto_tsquery('simple',cast('harry' AS text)) AND to_tsvector('simple', cast(title AS text)) @@ plainto_tsquery('simple',cast('potter' AS text))

我的错是什么?我还需要全文搜索,也需要一个或多个单词的组合。

[如果有比在一个查询中合并两个单词更好的解决方案,例如

 SELECT * FROM books WHERE to_tsvector('simple', cast(title AS text)) @@ plainto_tsquery('simple',cast('harry potter' AS text))
postgresql select tsvector
1个回答
0
投票

您应该使用词组搜索:

WHERE to_tsvector('simple', title) @@ to_tsquery('simple', 'harry <-> potter')
© www.soinside.com 2019 - 2024. All rights reserved.