J.Done
J.Done

Reputation: 3053

Elasticsearch - Get aggregation key sort as number

I made query result that aggregate some data, and its aggregation key is number. I tried to sort result of aggregation by key. elasticsearch treated key as string.

Since the number of current result bucket is pretty large, it's unable to modify on client side. Any idea of this?

Here is my query.

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

and current result bucket.

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

It's my expect result.

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

Upvotes: 4

Views: 9919

Answers (2)

Subhamay
Subhamay

Reputation: 325

This is a multiple group by scenario where data are being sorted by the key descending order.

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

Output

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

Upvotes: 2

keety
keety

Reputation: 17461

Using the value_script approach should fix the alphabetical sort issue:

Example:

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

Upvotes: 12

Related Questions