# Sort on multiple Buckets value

**URL:** <https://discuss.elastic.co/t/sort-on-multiple-buckets-value/130645>\
**Category:** Elasticsearch\
**Created:** [May 4, 2018, 2:29pm UTC](https://discuss.elastic.co/t/sort-on-multiple-buckets-value/130645 "2018-05-04T14:29:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gargnaukri](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gargnaukri](https://discuss.elastic.co/u/gargnaukri)\
**Post date:** [May 4, 2018, 2:29pm UTC](https://discuss.elastic.co/t/sort-on-multiple-buckets-value/130645/1 "2018-05-04T14:29:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [May 5, 2018, 4:07am UTC](https://discuss.elastic.co/t/sort-on-multiple-buckets-value/130645/2 "2018-05-05T04:07:25Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order)

---

<div class="post-metadata">

**Author:** ![gargnaukri](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gargnaukri](https://discuss.elastic.co/u/gargnaukri)\
**Post date:** [May 5, 2018, 9:32am UTC](https://discuss.elastic.co/t/sort-on-multiple-buckets-value/130645/3 "2018-05-05T09:32:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 2, 2018, 9:32am UTC](https://discuss.elastic.co/t/sort-on-multiple-buckets-value/130645/4 "2018-06-02T09:32:40Z")

</div>

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