gpt4 book ai didi

sorting - Elasticsearch:按条件匹配的字段对某些项目进行排序,其他按条件进行排序

转载 作者:行者123 更新时间:2023-12-03 02:02:46 24 4
gpt4 key购买 nike

假设我有一个这样映射的文档:

{
"corp" : {
"mappings" : {
"staff" : {
"properties" : {
"created" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"name" : {
"type" : "string"
},
"public" : {
"type" : "boolean"
},
"salary" : {
"type" : "long"
}
}
}
}
}
}

像这样的数据集:
[{"name": "John", "salary": 100000, "public": true, "created": "<datestamp>"},
{"name": "Bob", "salary": 80000, "public": false, "created": "<datestamp>"},
{"name": "Wendy", "salary": 110000, "public": true, "created": "<datestamp>"},
{"name": "Joan", "salary": 210000, "public": false, "created": "<datestamp>"}]

我想构造一个排序,使我们仅在 public=true处按薪水排序,然后通过 created字段对其余项目进行排序。最终结果应为:

went
  • 约翰
  • Bob(取决于created日期)​​
  • 琼(取决于created日期)​​

  • 这可能吗?

    最佳答案

    应该这样做:

    GET /corp/staff/_search
    {
    "sort": [
    {
    "public": {
    "order": "desc"
    }
    },
    {
    "salary": {
    "order": "asc"
    }
    },
    {
    "created": {
    "order": "asc"
    }
    }
    ]
    }

    关于sorting - Elasticsearch:按条件匹配的字段对某些项目进行排序,其他按条件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362582/

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