elasticsearch - Elastic Search按另一个字段中定义的字段排序

标签 elasticsearch elasticsearch-5

我有两个字段:count(对象)和countyby(文本)。计数对象具有几个类型为number的嵌套属性:

"count" : {
    "properties" : 
     "value1" : {
         "type" : "long"
     },
     "value2" : {
         "type" : "long"
     },
     "value3" : {
         "type" : "long"
     },
     "value4" : {
         "type" : "long"
     },
     "value5" : {
         "type" : "long"
     }
 },
 "countBy": {
     type: "keyword"
 }

我需要进行搜索,以根据countBy属性中指定的count的嵌套属性的值对文档进行排序

最佳答案

您可以使用script-based sorting实现此目的:

"sort" : {
    "_script" : {
        "type" : "number",
        "script" : {
            "lang": "painless",
            "source": "doc['countBy'].value != null ? doc['count.'+doc['countBy'].value].value : 0"
        },
        "order" : "desc"
    }
}

关于elasticsearch - Elastic Search按另一个字段中定义的字段排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54191142/

相关文章:

java - ElasticSearch Java API AggregationBuilder 过滤器/全局 "name"参数导致 JSON 格式错误

elasticsearch - has_parent查询如何与Elasticsearch中的join数据类型一起使用?

elasticsearch - 过渡时Elasticsearch索引

java - ES Rest High Level Client 空闲一段时间后抛出 SocketTimeoutException

c# - Elasticsearch 查询第一次慢

javascript - 如何追踪elasticsearch批量导入失败的原因?

java - 哪个客户端可以在 Java 1.6 中运行并从 Elasticsearch 5.4 进行查询?

python - 修复GEOJson自交点

elasticsearch - 如果我想让Elasticsearch更快,是否要向集群添加更多节点?

apache-spark - 从spark写入elasticsearch非常慢