gpt4 book ai didi

postgresql - 了解全文搜索查询中子句顺序的影响

转载 作者:行者123 更新时间:2023-12-05 08:10:40 24 4
gpt4 key购买 nike

我正在使用 postgres FTS 进行前缀搜索。
当匹配发生时,我希望完全匹配而不是前缀匹配。

work:* 的查询应该先排序work,然后是workday。此查询将为两个匹配项提供相同的排名,因此这将不起作用

SELECT ts_rank(to_tsvector('simple', 'work'), to_tsquery('simple', 'work:*'))
UNION ALL
SELECT ts_rank(to_tsvector('simple', 'workday'), to_tsquery('simple', 'work:*'));
-- result:
0.06079271
0.06079271

当我为 work 添加 or 子句时(通过 | work ),workday 的排名突然变为 0。我不明白为什么 or 子句将排名降低到 0,因为查询中仍然有一个匹配项 (work:*)

SELECT ts_rank(to_tsvector('simple', 'work'), to_tsquery('simple', 'work:* | work'))
UNION ALL
SELECT ts_rank(to_tsvector('simple', 'workday'), to_tsquery('simple', ' work:* | work'));
-- result:
0.06079271
0

当我在位置上切换两个 or 子句时,结果与根本不添加子句相同:

SELECT ts_rank(to_tsvector('simple', 'work'), to_tsquery('simple', 'work | work:*'))
UNION ALL
SELECT ts_rank(to_tsvector('simple', 'workday'), to_tsquery('simple', 'work | work:*'));
-- result:
0.06079271
0.06079271

SQL fiddle : http://sqlfiddle.com/#!17/9eecb/89476

我的目标是更好地理解 ts_rank 以及为什么排名会以如此显着的方式受到影响,并可能报告任何 postgres 错误。

我找不到任何提及查询词本身的顺序对排名有任何影响。
https://www.postgresql.org/docs/13/textsearch-controls.html#TEXTSEARCH-RANKING

注意:我使用排名对结果进行排序,因为我的查询可以包含任意数量的关键字。

最佳答案

在前缀匹配之前明确对完全匹配进行排名的查询怎么样?

SELECT doc
FROM tab
WHERE to_tsvector('simple', doc) @@ to_tsquery('simple', 'work:*')
ORDER BY NOT to_tsvector('simple', doc) @@ to_tsquery('simple', 'work'),
ts_rank(to_tsvector('simple', doc), to_tsquery('simple', 'work:*'));

那依赖于FALSE < TRUE .

关于postgresql - 了解全文搜索查询中子句顺序的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71212423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com