Elasticsearch -将聚合键排序为数字

oknwwptz  于 2023-05-22  发布在  ElasticSearch
关注(0)|答案(2)|浏览(128)

我做了一个聚合了一些数据的查询结果,它的聚合键是number。我尝试按键对聚合结果进行排序。elasticsearch把key当作字符串。
由于当前结果桶数量较大,客户端无法修改。你知道这个吗?
这是我的疑问。

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

bsxbgnwa1#

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

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

a0zr77ik2#

这是一个多组按方案,其中数据按键降序排序。

{
    "size": 0,
    "aggs": {
        "categories": {
            "filter": {
                "exists": {
                    "field": "organization_industries"
                }
            },
            "aggs": {
                "names": {
                    "terms": {
                        "field": "organization_revenue_in_thousands_int.keyword",
                        "size": 200,
                        "order": {
                            "_key": "desc"
                        }
                    },
                    "aggs": {
                        "industry_stats": {
                            "terms": {
                                "field": "organization_industries.keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

输出

"aggregations": {
    "categories": {
        "doc_count": 195161605,
        "names": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 19226983,
            "buckets": [
                {
                    "key": "99900",
                    "doc_count": 1742,
                    "industry_stats": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "internet",
                                "doc_count": 1605
                            },
                            {
                                "key": "investment management",
                                "doc_count": 81
                            },
                            {
                                "key": "biotechnology",
                                "doc_count": 54
                            },
                            {
                                "key": "computer & network security",
                                "doc_count": 2
                            }
                        ]
                    }
                },                
                {
                    "key": "998000",
                    "doc_count": 71,
                    "industry_stats": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "finance",
                                "doc_count": 48
                            },
                            {
                                "key": "information technology & services",
                                "doc_count": 23
                            }
                        ]
                    }
                }
                
                }
            ]
        }
    }

相关问题