Elasticsearch - 获取聚合键排序为数字

Elasticsearch - Get aggregation key sort as number

我做的查询结果聚合了一些数据,它的聚合键是number。我试图按键对聚合结果进行排序。 elasticsearch 将键视为字符串。

由于当前结果桶数量较多,客户端无法修改。知道这个吗?

这是我的查询。

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

和当前结果桶。

    "buckets": [
       {
          "key": "0",
          "doc_count": 68
       },
       {
          "key": "1",
          "doc_count": 21
       },
       {
          "key": "10",
          "doc_count": 6
       },
       {
          "key": "11",
          "doc_count": 16
       },

这是我的预期结果。

"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
   },

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

示例:

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