PostgreSQL 全文搜索问题 (to_tsquery)
PostgreSQL full text search issue (to_tsquery)
我在数据库中搜索包含停用词的游戏名称时遇到问题。我只是在寻找一般的完全匹配,我希望我的所有搜索都尽可能少 "fuzzy" 匹配,最好是零。
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');
我可以在这里做些什么来获得期望的结果,即只返回匹配项,即短语匹配项?我在想也许我可以将其作为停用词删除,不知道我该怎么做,也不确定考虑到可能还搜索 "World of Warcraft" 和具有停用词的类似标题的解决方案有多好(而且我通常只需要完全匹配)。
想法?
要删除部分或全部停用词,请在 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%');
我在数据库中搜索包含停用词的游戏名称时遇到问题。我只是在寻找一般的完全匹配,我希望我的所有搜索都尽可能少 "fuzzy" 匹配,最好是零。
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');
我可以在这里做些什么来获得期望的结果,即只返回匹配项,即短语匹配项?我在想也许我可以将其作为停用词删除,不知道我该怎么做,也不确定考虑到可能还搜索 "World of Warcraft" 和具有停用词的类似标题的解决方案有多好(而且我通常只需要完全匹配)。
想法?
要删除部分或全部停用词,请在 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%');