我在尝试对 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/