我们正在尝试使用文本查询清除特定列上的重复项。
以下是一个示例查询:
SELECT
distinct on (title) title",
ts_rank_cd(to_tsvector(title), query, 1) AS rank
FROM
products,
to_tsquery('english',''english','Ribeye | Starter | Cheese | Assortment'') query
WHERE
query @@ to_tsvector(title) AND
book_id = '37' as result
ORDER BY
rank DESC limit 10 offset 0
出于某种原因,我们在使用TEXT搜索时仍然偶尔会收到重复的标题。
结果:
"Ribeye Starter Cheese Assortment"
"Filet Mignon Starter Cheese Assortment"
"Ambassadors Assortment"
"Ambassadors Assortment"
"Strip Steak Starter Cheese Assortment"
"After Dinner Cheese Assortment"
"Ambassadors Assortment"
"Chairmans Assortment"
使用不带TSVectors的distinct可以正常工作但引入文本搜索会忽略PostGres不同的关键字。
有什么想法吗?
答案 0 :(得分:1)
我没有在我面前测试psql ......但是文档中的这一行引起了我的注意:
DISTINCT ON表达式必须与最左边的ORDER BY表达式匹配。 ORDER BY子句通常包含其他表达式,用于确定每个DISTINCT ON组中行的所需优先级。
所以只需将“标题”添加到ORDER
:
... ORDER BY title,rank DESC ...