Elasticsearch-获取聚合密钥按数字排序

J.D*_*one 3 aggregation elasticsearch

我取得了汇总一些数据的查询结果,其汇总键为数字。我试图按键对聚合结果进行排序。elasticsearch将键视为字符串。

由于当前结果存储区的数量非常大,因此无法在客户端进行修改。有这个想法吗?

这是我的查询。

"aggregations" : {
                "startcount" : {
                    "terms" : {
                        "script" : "round(doc['startat'].value/1000)",
                        "size" : 1000,
                        "order" : { "_term" : "asc" }
                    }
                }
             }
Run Code Online (Sandbox Code Playgroud)

和当前结果存储区。

    "buckets": [
       {
          "key": "0",
          "doc_count": 68
       },
       {
          "key": "1",
          "doc_count": 21
       },
       {
          "key": "10",
          "doc_count": 6
       },
       {
          "key": "11",
          "doc_count": 16
       },
Run Code Online (Sandbox Code Playgroud)

这是我的预期结果。

"buckets": [
   {
      "key": "0",
      "doc_count": 68
   },
   {
      "key": "1",
      "doc_count": 21
   },
   {
      "key": "2", // not '10'
      "doc_count": 6
   },
   {
      "key": "3", // not '11'
      "doc_count": 16
   },
Run Code Online (Sandbox Code Playgroud)

kee*_*ety 6

使用value_script方法应该可以解决字母排序问题:

例:

 {
   "size": 0,
   "aggregations": {
      "startcount": {
         "terms": {
            "field": "startat",
            "script": "round(_value/1000)",
            "size": 1000,
            "order": {
               "_term": "asc"
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

  • #!弃用:已弃用聚合顺序键 [_term],已替换为 [_key]。 (2认同)