Postgres干掉了比赛

xua*_*nji 10 postgresql full-text-search

查询

SELECT to_tsvector('recreation') @@ to_tsquery('recreatio:*');

即使'recreati'是'娱乐'的前缀,也会返回false.这似乎发生了,因为"娱乐"被存储为其词干,"后退".例如,如果我们通过运行故意破坏词干算法

SELECT to_tsvector('recreation1') @@ to_tsquery('recreatio:*');

查询返回true.

有没有办法让第一个查询匹配?

ove*_*awn 1

考虑到问题的年龄,不确定这个答案是否有用,但是:

关于词干

看来你是对的:

select ts_lexize('english_stem','recreation');
Run Code Online (Sandbox Code Playgroud)

输出

 ts_lexize
-----------
 {recreat}
(1 row)
Run Code Online (Sandbox Code Playgroud)

文档说

此外,* 可以附加到词位以指定前缀匹配:

SELECT to_tsquery('supern:*A & star:A*B');

这样的词位将匹配 tsvector 中以给定字符串开头的任何单词。

所以看来没有办法使原始查询匹配。

基于部分匹配的解决方案

人们可以回退到寻找词干和查询的部分匹配,例如使用pg_trgm扩展:

SELECT (to_tsvector('recreation creation') @@ to_tsquery('recreatio:*')) or 
  'recreatio:*' % any (
    select trim(both '''' from regexp_split_to_table(strip(to_tsvector('recreation creation'))::text, ' '))
  );
Run Code Online (Sandbox Code Playgroud)

(也许可以用更优雅的方式形成茎阵列。)