Elasticsearch 订单查询排序不正确

Elasticsearch order query does not order correctly

我正在使用 python 访问 elasticsearch 索引。我有一组数据,我想按它们的键值排序。当我订购它们时,结果似乎不正确。

这是我发送到索引的查询:

            "aggs": {
                "Group1": {
                    "terms": {
                        "field": "method.keyword",
                        "include": ".*POST.*",
                    },
                    "aggs": {
                        "Group2": {
                            "terms": {
                                "field": "keyword.keyword",
                                "size": 11593,
                            },
                            "aggs": {
                                "Group3": {
                                    "terms": {
                                        "field": "amount.keyword",
                                        "size": 100,
                                        "order": {"_key": "desc"}

                                    }
                                }

                            },
                        }
                    },



                }

            }

这是查询返回的内容:

"aggregations": {
    "Group1": {
      "buckets": [
        {
          "Group2": {
            "buckets": [
              {
                "Group3": {
                  "buckets": [
                    {
                      "doc_count": 2, 
                      "key": "9"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "89"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "88"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "87"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "86"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "85"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "84"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "83"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "82"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "81"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "80"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "8"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "79"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "78"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "77"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "76"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "75"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "74"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "73"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "72"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "71"
                    }, 
                    {
                      "doc_count": 1, 
                      "key": "70"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "7"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "69"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "68"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "67"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "66"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "65"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "64"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "63"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "62"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "61"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "60"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "6"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "59"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "58"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "57"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "56"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "55"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "54"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "53"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "52"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "51"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "50"
                    }, 
                    {
                      "doc_count": 3, 
                      "key": "5"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "49"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "48"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "47"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "46"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "45"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "44"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "43"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "42"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "41"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "40"
                    }, 
                    {
                      "doc_count": 3, 
                      "key": "4"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "39"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "38"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "37"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "36"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "35"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "34"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "33"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "32"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "31"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "30"
                    }, 
                    {
                      "doc_count": 3, 
                      "key": "3"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "29"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "28"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "27"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "26"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "25"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "24"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "23"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "22"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "21"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "20"
                    }, 
                    {
                      "doc_count": 3, 
                      "key": "2"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "19"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "18"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "17"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "16"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "15"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "14"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "13"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "12"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "11"
                    }, 
                    {
                      "doc_count": 2, 
                      "key": "10"
                    }, 
                    {
                      "doc_count": 6, 
                      "key": "1"
                    }
                  ], 
                  "doc_count_error_upper_bound": 0, 
                  "sum_other_doc_count": 0
                }, 
                "doc_count": 167, 
                "key": "y&price_max"
              }, 

如您所见,键值的顺序不正确。如何订购?

由于amount.keyword是一个字符串值,所以它按字母顺序对它们进行了排序。如果您想要数字排序,则需要使用数字字段。例如,如果 amount(没有关键字部分)是数字,则使用它。