PostgreSQL全文搜索找不到“ andy”

时间:2018-10-02 10:46:56

标签: postgresql search full-text-search

我有这个PostgreSQL查询:

SELECT d.user_id, display_name, avatar_url
FROM user_directory_search
WHERE
user_id like '@and%';

我得到这些结果:

                    user_id             | display_name | avatar_url
----------------------------------------+--------------+------------
 @andy.huang:synapse.siliconmotion.com  |              |
 @andy.zhao:synapse.siliconmotion.com   | Andy.zhao    |
 @andy.yao:synapse.siliconmotion.com    |              |
 @andy.zou:synapse.siliconmotion.com    |              |
 @andy.xie:synapse.siliconmotion.com    |              |
 @andy.chang:synapse.siliconmotion.com  | andy.chang   |
 @andy.chuang:synapse.siliconmotion.com | andy.chuang  |
 @andy.hsiao:synapse.siliconmotion.com  |              |
(8 rows)

但是当我使用命令时:

SELECT d.user_id, display_name, avatar_url
FROM user_directory_search
WHERE
vector @@ to_tsquery('english', '(andy:* | andy)');

我什么也没有:

 user_id | display_name | avatar_url
---------+--------------+------------
(0 rows)

有人知道原因吗?

1 个答案:

答案 0 :(得分:0)

问题是全文解析器将这些字符串解析为主机名:

SELECT alias, description, token, lexemes
FROM ts_debug('english', '@andy.huang:synapse.siliconmotion.com')
WHERE alias <> 'blank';

 alias | description |           token           |           lexemes           
-------+-------------+---------------------------+-----------------------------
 host  | Host        | andy.huang                | {andy.huang}
 host  | Host        | synapse.siliconmotion.com | {synapse.siliconmotion.com}
(2 rows)

在索引期间,您可以用空格替换有问题的期限:

SELECT alias, description, token, lexemes
FROM ts_debug('english',
              translate('@andy.huang:synapse.siliconmotion.com', '.', ' '))
WHERE alias <> 'blank';

   alias   |   description   |     token     |   lexemes    
-----------+-----------------+---------------+--------------
 asciiword | Word, all ASCII | andy          | {andi}
 asciiword | Word, all ASCII | huang         | {huang}
 asciiword | Word, all ASCII | synapse       | {synaps}
 asciiword | Word, all ASCII | siliconmotion | {siliconmot}
 asciiword | Word, all ASCII | com           | {com}
(5 rows)

但是,如果我是您,我将使用simple全文搜索配置。还是您想要词干(比较上面的“令牌”和“词汇”)?