postgresql 按 2 个字段的相似性(三元组)排序
我在表中的 2 个字段上使用了三元组,并希望在请求期间比较两个字段时按最大相似度进行排序。(Postgresql 11.x)
我很难做到这一点:
- 我无法按顺序使用别名
- 如果我替换完整代码, 通过相似性(...)我有这个错误:
查询 1 错误:错误:对于 SELECT DISTINCT,ORDER BY 表达式必须 出现在选择列表中
我当前的别名在 godd 的 ORDER BY 中不起作用的请求是:
SELECT DISTINCT
similarity (msr_references.searchable, 'my simple test') AS similarity,
similarity (msr_references.simplified_searchable, 'my simple test') AS similarity_simplified,
msr_references.name, msr_references.searchable, msr_references.simplified_name, msr_references.simplified_searchable
FROM
"msr_references"
WHERE
similarity(msr_references.searchable, 'my simple test') >= 0.3
OR
similarity(msr_references.simplified_searchable, 'my simple test') >= 0.3
ORDER BY
CASE WHEN similarity >= similarity_simplified THEN similarity END DESC,
CASE WHEN similarity_simplified >= similarity THEN similarity_simplified END DESC
LIMIT 10;
请求的目标是按 2 个字段之间的最大相似度进行排序,但针对所有记录。
良好排序的示例:
similarity | similarity_simplified
0.3 0.55
0.51 0.2
0.46 0.31
0.32 0.43
0.39 0.36
这在技术上可行吗? 谢谢,
I used trigrams on 2 fields in my table and want to sort by MAX similarity comparing both fields during the request.(Postgresql 11.x)
I have difficulties to do this:
- I can't use aliases in order by
- If I replace full code by similarity(...) I have this error:
Query 1 ERROR: ERROR: for SELECT DISTINCT, ORDER BY expressions must
appear in select list
My current request with aliases not working in ORDER BY of godd is:
SELECT DISTINCT
similarity (msr_references.searchable, 'my simple test') AS similarity,
similarity (msr_references.simplified_searchable, 'my simple test') AS similarity_simplified,
msr_references.name, msr_references.searchable, msr_references.simplified_name, msr_references.simplified_searchable
FROM
"msr_references"
WHERE
similarity(msr_references.searchable, 'my simple test') >= 0.3
OR
similarity(msr_references.simplified_searchable, 'my simple test') >= 0.3
ORDER BY
CASE WHEN similarity >= similarity_simplified THEN similarity END DESC,
CASE WHEN similarity_simplified >= similarity THEN similarity_simplified END DESC
LIMIT 10;
The goal of the request is to sort by MAX similarity between the 2 fields, but for all records.
Example of good sorting:
similarity | similarity_simplified
0.3 0.55
0.51 0.2
0.46 0.31
0.32 0.43
0.39 0.36
Is this possible technically ?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论