postgresql 全文接近度 (`<->` ) 搜索 json 文档

标签 postgresql full-text-search

我在尝试对 json(b) 文档使用全文搜索时遇到某些单词组合的意外行为。也许我对文本搜索的理解不正确,或者这是一个已知错误。

这是一个简单的例子:

-- searching on text, expected true, query returned true
select to_tsvector('the quick brown fox jumped over the lazy dog')             
       @@ to_tsquery('fox <-> jump');
 ?column?
----------
 t
(1 row)

-- searching json, expected true, but query returns false
select to_tsvector('{"example": ["the quick brown fox", "jumped over the lazy dog"]}'::json) 
       @@ to_tsquery('fox <-> jump');
 ?column?
----------
 f
(1 row)

意外行为似乎只发生在特定单词的文本元素中,例如,搜索 fox <-> over在以下示例中工作。

-- expected this to be true, and I get true back
select to_tsvector('["jumped the quick brown fox", "over the lazy dog"]'::json) 
       @@ to_tsquery('fox <-> over');
 ?column?
----------
 t
(1 row)

同样,如果fox jumped位于同一文本元素中,则查询按预期返回 true。

select to_tsvector('["the quick brown fox jumped", "over the lazy dog"]'::json) 
       @@ to_tsquery('fox <-> jump');
 ?column?
----------
 t
(1 row)

postgresql/系统版本测试:

select version();
                                                    version
---------------------------------------------------------------------------------------------------------------
 PostgreSQL 10.1 on x86_64-apple-darwin15.6.0, compiled by Apple LLVM version 8.0.0 (clang-800.0.42.1), 64-bit
(1 row)

最佳答案

我不知道为什么,但 PostgreSQL 计算数组元素之间的一个标记:

SELECT to_tsvector('english', jsonb '{"example": ["the quick brown fox", "jumped over the lazy dog"]}');

                      to_tsvector                       
--------------------------------------------------------
 'brown':3 'dog':10 'fox':4 'jump':6 'lazi':9 'quick':2
(1 row)

你看到 'fox' 在位置 4,而 'jump' 在位置 6。

这可能是一个 PostgreSQL 错误;我懒得调试它。

关于postgresql 全文接近度 (`<->` ) 搜索 json 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57763985/

相关文章:

sql-server - 从 SQL Server 全文搜索获取词频

postgresql - 尝试创建 Postgres 函数

sql - 替代 SQL 计数子查询

Postgresql 中的 C 库函数

sql - PostgreSQL 错误 : function to_tsvector(character varying, 未知)不存在

django - 在 Django/PostgreSQL 搜索结果页面上突出显示搜索词

postgresql - 如何在 Heroku 上为 Postgres 命令启用安静模式

sql - postgresql 在 where 子句中使用 json 子元素

search - 关于多方面搜索软件堆栈的建议

elasticsearch - 在 ElasticSearch 7+ 中,如何搜索所有文本字段?