postgresql 按 2 个字段的相似性(三元组)排序

发布于 2025-01-13 02:17:40 字数 1215 浏览 1 评论 0原文

我在表中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文