Dav*_*ave 5 postgresql full-text-search internationalization full-text-indexing
我正在用巴西葡萄牙语开发一个简单的文章网站.搜索功能基于全文搜索,但未返回预期结果.
我在postgresql上做了这个.这是简化表:
Artigos
-id
-title -- article title
-intro -- article introduction
-content -- article body
-publishdate -- date of launch
-artigosts -- this will work as our fts index.
Run Code Online (Sandbox Code Playgroud)
创建表后,我跑了:
UPDATE artigos SET artigosts =
setweight(to_tsvector('pg_catalog.portuguese', coalesce(title,'')), 'A') ||
setweight(to_tsvector('pg_catalog.portuguese', coalesce(intro,'')), 'B') ||
setweight(to_tsvector('pg_catalog.portuguese', coalesce(content,'')), 'C');
CREATE INDEX artigosts_idx ON artigos USING gist (artigosts);
CREATE TRIGGER artigosts_tg
BEFORE INSERT OR UPDATE ON artigos
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('artigosts', 'pg_catalog.portuguese', 'title', 'intro', 'content');
Run Code Online (Sandbox Code Playgroud)
是的,我打算在搜索中使用简单的加权.做了一个索引加速,一个触发器,所以我可以插入和更新,而无需担心重制索引等.
嗯,根据我的理解,一切都很好.但结果不是.一个简单的例子.
假设我有"...... banco de dados ......没有银行......"作为一篇文章的内容.当我做:
SELECT title, intro, content FROM artigos WHERE plainto_tsquery('banco de dados') @@ artigosts;
Run Code Online (Sandbox Code Playgroud)
它返回一个空集.我查看了ts_vector列,看到了谓词'banc'和'dad'.但我仍然无法理解为什么它不返回包含上述文章的行.
有人能为这个问题带来光明吗?
原因可能是因为您的默认字典设置为英语.请尝试以下查询以确定是否确实如此.
SELECT * FROM ts_debug('banco de dados');
Run Code Online (Sandbox Code Playgroud)
此查询将显示字典如何解析您的搜索短语.它应该提供lexemes"banco","de"和"dado".因此,您实际搜索的内容将不会存在于索引中,您将收到0结果.
现在试试这个:
SELECT * FROM ts_debug('portuguese', 'banco de dados');
Run Code Online (Sandbox Code Playgroud)
它应该返回索引中存在的词法"banc"和"dad".如果是这种情况,那么您只需更改搜索查询即可获得适当的结果.
SELECT title, intro, content FROM artigos WHERE plainto_tsquery('portuguese', 'banco de dados') @@ artigosts;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
887 次 |
| 最近记录: |