Sort on multiple Buckets value


(garg) #1

Hi,

I have an Index named "requirement" which has a child named as "application_activity".

In this case i need to sort the multiple bucket based on their value. I am able to prepare the buckets as I want using multi level aggregation. But I have to sort the result bucket based on any status.

I am using the below aggregation:

  "aggs": {
    "EMPLOYERID": {
      "terms": {
        "field": "EMPLOYERID",
        "size": 2
      },
      "aggs": {
        "APPLICATION_ACTIVITY": {
          "children": {
            "type": "application_activity"
          },
          "aggs": {
            "PROCESSED_TYPE": {
              "terms": {
                "field": "PROCESSED_TYPE"
              },
              "aggs": {
                "UNIQUE_COUNT": {
                  "cardinality": {
                    "field": "APPLICATIONID"
                  }
                }
              }
            }
          }
        }
      }
    }
  }

And I am getting the below resut.

{
    "aggregations": {
        "EMPLOYERID": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 4,
            "buckets": [
                {
                    "key": 2787,
                    "doc_count": 37,
                    "APPLICATION_ACTIVITY": {
                        "doc_count": 133,
                        "PROCESSED_TYPE": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": 111,
                                    "doc_count": 44,
                                    "unique_count": {
                                        "value": 11
                                    }
                                },
                                {
                                    "key": 222,
                                    "doc_count": 39,
                                    "unique_count": {
                                        "value": 3
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "key": 0,
                    "doc_count": 30,
                    "APPLICATION_ACTIVITY": {
                        "doc_count": 112,
                        "PROCESSED_TYPE": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": 111,
                                    "doc_count": 36,
                                    "unique_count": {
                                        "value": 13
                                    }
                                },
                                {
                                    "key": 222,
                                    "doc_count": 18,
                                    "unique_count": {
                                        "value": 5
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

No Suppose If we have 2 Buckets as above which is fine, But i want to sort these bucket per PROCESSED_TYPE. Like I want to sort the bucket in the basis of status (111 or 222) which is having the larger or smaller value.

If I wanna sort on 222 in desc order then bucket no. 2 should come first because it has the value 5 which is greater than 3.

Please help me out If anyone have any idea to sort such kind of result.


(Glen Smith) #2

Hello,

Have a look at the documentation for how to order terms aggs [1].

[1] https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order


(garg) #3

Thanks for your response, I have checked this solution. But this is for inside the bucekt not over the parallel buckets.


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.