【问题标题】:Conditional sorting in Elastic search弹性搜索中的条件排序
【发布时间】:2020-10-19 06:58:44
【问题描述】:

我正在对 es 进行排序。 我有两列“分数”和“范围”。所以我想对记录进行排序,首先我会得到分数为 0 的所有记录,然后按范围排序。我无法在 ES 上找到它的任何文档。

就像我有记录
分数为 0 且范围为 7 的 A
B,得分 1,范围 2
分数为 0 且范围为 7 的 C
D,得分 2,范围 1

所以我想要 A、C、D、B 这样的结果 谢谢

【问题讨论】:

    标签: node.js elasticsearch elasticsearch-aggregation


    【解决方案1】:

    我建议使用script-based sorting。该脚本的想法是首先返回带有Score = 0 的文档,如果Score > 0 脚本使用Range 值进行排序:

    GET index/_search
    {
        "sort" : {
            "_script" : {
                "type" : "number",
                "script" : {
                    "lang": "painless",
                    "source": "doc['Score'].value == 0 ? 0 : doc['Range'].value"
                },
                "order" : "asc"
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答,但我想要所有得分为 0 的记录,然后按范围排序。
    • 您能提供一些示例文档吗?
    猜你喜欢
    • 2019-01-26
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多