我在数据库中搜索包含停用词的游戏名称时遇到问题。我只是在寻找一般的精确匹配,我希望所有搜索的“模糊”匹配尽可能少,最好为零。
E.g. content that produced false positives directly, contains sentences like; "the war in Afghanistan" + reference to "win*" another place; or "Lifeseed to win the war"; or "win the war that is taking over the galaxy" and so on.
这当然不起作用并报错:
SELECT id, title, content FROM my_table
WHERE tsvector_combined@@ to_tsquery('win that war');
我曾希望“phraseto_tsquery”能解决我的一些其他搜索(PostgreSQL 9.6),但由于这个搜索中的停用词,它没有:
SELECT id, title, content FROM my_table
WHERE tsvector_combined@@ phraseto_tsquery('win that war');
我也尝试过使用领带战斗机,<1> | <-> 但通常它会得到误报:
SELECT id, title, content FROM my_table
WHERE tsvector_combined@@ to_tsquery('win <-> that <-> war');
我可以在这里做些什么来获得期望的结果,即只返回匹配项,即短语匹配项?我在想也许我可以把它作为停用词删除,不知道我是怎么做到的,也不确定这个解决方案有多好,也许还可以搜索“魔兽世界”和类似的有停用词的标题(而且我通常只需要完全匹配)。
想法?
最佳答案
要删除部分或全部停用词,请在 PostgreSQL 软件目录的 share/tsearch_data
子目录中创建一个简化的或空的停用词文件。然后你可以创建一个新的雪球文本搜索字典
CREATE TEXT SEARCH DICTIONARY newdict (
TEMPLATE = pg_catalog.snowball,
language = '...',
stopwords = '...'
);
使用新的停用词文件并基于该文件创建新的文本搜索配置。这当然会使您的索引更大。
从您引用的示例中,我宁愿选择一种不同的方法并使用全文搜索,以便能够使用索引来减少您的候选人并使用第二个条件进一步过滤他们:
SELECT id, title, content FROM my_table
WHERE tsvector_combined @@ to_tsquery('win that war')
AND (title LIKE '%win that war%' OR content LIKE '%win that war%');
关于PostgreSQL 全文搜索问题 (to_tsquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39281258/