postgresql - 简单模式下的 to_tsvector 在某些设置中丢弃非英语

标签 postgresql full-text-search

在某些 pg 安装中,我注意到发生了以下情况

sam=# select '你好 世界'::tsvector;
   tsvector    
---------------
 '世界' '你好'
(1 row)

sam=# select to_tsvector('simple', '你好 世界');
 to_tsvector 
-------------

(1 row)

即使我的数据库是这样配置的:

MBA:bin sam$ ./psql -l
                              List of databases
   Name    | Owner | Encoding |   Collate   |    Ctype    | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
 postgres  | sam   | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 |
 sam       | sam   | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 |
 template0 | sam   | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | =c/sam           +
           |       |          |             |             | sam=CTc/sam
 template1 | sam   | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | =c/sam           +
           |       |          |             |             | sam=CTc/sam
(4 rows)

在其他类似设置中,我看到 select to_tsvector('simple', ' Hello World '); 正确返回标记。

我如何诊断简单的分词器以弄清楚为什么它会丢弃这些字母?

最简单的重现似乎是通过 postgres 应用程序安装 postgres。在设置了语言环境的 ubuntu 上安装 postgres 时不会发生。

最佳答案

不幸的是,文本搜索使用的默认解析器高度依赖于数据库初始化,尤其是 lc_collat​​e 和当前数据库对象编码。

这是由于默认文本解析器的一些内部工作造成的。含糊不清documented :

Note: The parser's notion of a "letter" is determined by the database's locale setting, specifically lc_ctype. Words containing only the basic ASCII letters are reported as a separate token type, since it is sometimes useful to distinguish them.

重要的部分是 PostgreSQL 中的这些注释 source code :

/* [...]
 * Notes:
 *  - with multibyte encoding and C-locale isw* function may fail
 *    or give wrong result.
 *  - multibyte encoding and C-locale often are used for
 *    Asian languages.
 *  - if locale is C then we use pgwstr instead of wstr.
 */

及以下:

/*
 * any non-ascii symbol with multibyte encoding with C-locale is
 * an alpha character
 */

因此,如果您想对中文使用默认解析器,请确保您的数据库已使用 C 语言环境进行初始化并且您具有多字节编码,因此 U+007F 以上的所有字符都将被视为字母(包括空格,例如表意空间 U+3000 !)。通常,以下 initdb 调用将执行您期望的操作:

initdb --locale=C -E UTF-8

否则,汉字将被跳过并被视为空白。

您可以使用调试函数 ts_debug 进行检查。使用 lc_collat​​e=en_US.UTF-8 初始化的数据库或标记化失败的任何其他配置,您将获得:

SELECT * FROM ts_debug('simple', '你好 世界');
 alias |  description  |   token   | dictionaries | dictionary | lexemes 
-------+---------------+-----------+--------------+------------+---------
 blank | Space symbols | 你好 世界 | {}            |            | 

相反,使用 lc_collat​​e=C 和 UTF-8 数据库(如上初始化),您将得到正确的结果:

SELECT * FROM ts_debug('simple', '你好 世界');
 alias |    description    | token | dictionaries | dictionary | lexemes
-------+-------------------+-------+--------------+------------+---------
 word  | Word, all letters | 你好  | {simple}     | simple     | {你好}
 blank | Space symbols     |       | {}           |            | 
 word  | Word, all letters | 世界  | {simple}     | simple     | {世界}

但是,您的意思似乎是对已经由常规空格分隔的中文文本进行标记化,即标记化/分段不会在 PostgreSQL 中发生。对于此用例,我强烈建议使用自定义解析器。如果您不使用 PostgreSQL 简单解析器的其他功能,例如标记化 URL,则尤其如此。

对空格字符进行分词的解析器很容易实现。事实上,在contrib/test_parser ,有一个示例代码就是这样做的。该解析器将在任何语言环境下工作。此解析器中存在缓冲区溢出错误,该错误已于 2012 年修复,请确保您使用的是最新版本。

关于postgresql - 简单模式下的 to_tsvector 在某些设置中丢弃非英语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24461059/

相关文章:

postgresql - 当子句是模时,postgres可以使用索引吗

sql - 对于 PostgreSQL 中的 boolean 数据类型,输出 "yes/no"而不是 "t/f"

sql - 已应用 GROUP BY 后多列的不同值

java - Solr - 多个同时写入的 LockObtainFailedException

sql - 在查询中使用过滤器和 CONTAINSTABLE 时执行计划不佳

bash - 错误 : more than one row returned by a subquery used as an expression

node.js - 如何使用应用程序用户连接到数据库?

mongodb 全文搜索和 json 对象

在 WHERE 中使用 "OR"时 MySQL 不使用索引

Android 记事本教程 - 实现搜索功能