to_tsvector() 是一个用于将文本或 JSON 文档转换为 tsvector 的系统函数。
to_tsvector() 在 PostgreSQL 8.3 中被添加。
用法
to_tsvector ( [configregconfig, ]documenttext) →tsvector
to_tsvector ( [configregconfig, ]documentjson) →tsvector
to_tsvector ( [configregconfig, ]documentjsonb) →tsvector
单词会根据指定的或 默认的全文搜索配置 进行规范化。结果中会包含位置信息。
变更历史
- PostgreSQL 10
- 增加了对 JSON 数据类型的支持(提交 e306df7f)
- PostgreSQL 8.3
- 添加(提交 140d4ebc)
示例
to_tsvector() 的基本用法示例
postgres=# SELECT to_tsvector('The Quick Brown Fox');
to_tsvector
-----------------------------
'brown':3 'fox':4 'quick':2
(1 row)
请注意,直接将文本值转换为 tsvector 会跳过规范化步骤,这意味着停用词等将包含在生成的 tsvector 中,但不会包含位置信息。
postgres=# SELECT 'The Quick Brown Fox'::tsvector;
tsvector
-----------------------------
'Brown' 'Fox' 'Quick' 'The'
(1 row)
将 to_tsvector() 与 tsquery 结合使用
postgres=# SELECT plainto_tsquery('foxes which are quick and brown') @@ to_tsvector('The Quick Brown Fox'); ?column? ---------- t (1 row)
参考资料
- PostgreSQL 文档: 文本搜索函数
反馈
请在此处 提交任何关于“to_tsvector()”的评论、建议或更正。