# Alphabetically sorting + aggregation ElasticSearch

**URL:** <https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414>\
**Category:** Elasticsearch\
**Created:** [October 8, 2020, 10:27am UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414 "2020-10-08T10:27:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![het](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@het](https://discuss.elastic.co/u/het)\
**Post date:** [October 8, 2020, 10:27am UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/1 "2020-10-08T10:27:42Z")

</div>

Hi team,

I am using aggregation to get grouped records. I am not able to sort the buckets.

Dummy Data :

```
{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp001_local000000000000001",
    "_score": 10.0,
    "_source": {
        "name": [
            "Person 01"
        ],
        "groupbyid": [
            "group0001"
        ],
        "ranking": [
             "2.0"
        ]
    }
},
{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp002_local000000000000001",
    "_score": 85146.375,
    "_source": {
        "name": [
            "Person 02"
        ],
        "groupbyid": [
            "group0001"
        ],
        "ranking": [
             "10.0"
        ]
    }
},
{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp003_local000000000000001",
    "_score": 20.0,
    "_source": {
        "name": [
            "Person 03"
        ],
        "groupbyid": [
            "group0002"
        ],        
        "ranking": [
             "-1.0"
        ]
    }
},
{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp004_local000000000000001",
    "_score": 5.0,
    "_source": {
        "name": [
            "Person 04"
        ],
        "groupbyid": [
            "group0002"
        ],
        "ranking": [
             "2.0"
        ]
    }
}

```

Mapping:

```auto
{
    "name": {
        "type": "text",
        "fielddata": true,
        "fields": {
            "lower_case_sort": {
                "type": "text",
                "fielddata": true,
                "analyzer": "case_insensitive_sort"
            }
        }
    },
   "ranking": {
         "type": "float"
    },
    "groupbyid": {
        "type": "text",
        "fielddata": true,
        "index": "analyzed",
        "fields": {
            "raw": {
                "type": "keyword",
                "index": "not_analyzed"
            }
        }
    }
}

```

I wanted to sort by the name field. So if the Sort order is DESC, then I want the data like,

```
group0002
    emp003_local000000000000001
    emp004_local000000000000001

group0001
    emp001_local000000000000001
    emp002_local000000000000001

```

Explanation: group0002, will come first because the name Person 04 will came first by DESC order.

I have tried a couple of things, but I can not able to achieve for the String field. I am able to achieve for the integer field.

Thanks in Advance.

---

<div class="post-metadata">

**Author:** ![het](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@het](https://discuss.elastic.co/u/het)\
**Post date:** [October 15, 2020, 12:40pm UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/2 "2020-10-15T12:40:49Z")

</div>

@elasticsearch -- Can anyone help on this?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 15, 2020, 1:04pm UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/3 "2020-10-15T13:04:36Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![het](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@het](https://discuss.elastic.co/u/het)\
**Post date:** [October 16, 2020, 6:24am UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/4 "2020-10-16T06:24:10Z")

</div>

Hi @dadoonet,

Thanks for the reply and apologies for not providing the query/script. Please find the query which I am hitting on elasticsearch.

```
{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "name:XYZ"
          }
        }
      ]
    }
  },
  "aggregations": {
    "groupbyid": {
      "terms": {
        "field": "groupbyid.raw",
        "size": 100
      },
      "aggs": {
        "top_hit_agg": {
          "top_hits": {
            "size": 100
          }
        },
        "ranking_agg": {
            "min": {
                "field": "ranking"
            }
        },
        "test_bucket_sort": {
          "bucket_sort": {
            "sort": [
              {
                "ranking_agg": {
                  "order": "desc"
                }
              }
            ],
            "size": 100,
            "from": 0
          }
        }
      }
    }
  }
}

```

I am able to sort for the ranking field. Was there any way I can group on groupbyid field and sort on the name field?

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 16, 2020, 6:37am UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/5 "2020-10-16T06:37:00Z")

</div>

Could you share a full script we can just copy, paste and execute in Kibana console?

---

<div class="post-metadata">

**Author:** ![het](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@het](https://discuss.elastic.co/u/het)\
**Post date:** [October 16, 2020, 6:47am UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/6 "2020-10-16T06:47:14Z")

</div>

Hi @dadoonet,

I am using this query only to get the records. Just the query and data are not real. I am using the GET API of Elasticsearch via postman for now as a part of testing.

I have shared the request body. Do you need anything more as I already have shared mapping, dummy data, and a script?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 16, 2020, 12:07pm UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/7 "2020-10-16T12:07:42Z")

</div>

A script that we can just copy and paste in kibana would help to reproduce the exact problem.

---

<div class="post-metadata">

**Author:** ![het](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@het](https://discuss.elastic.co/u/het)\
**Post date:** [October 16, 2020, 1:46pm UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/8 "2020-10-16T13:46:33Z")

</div>

I could not share the exact data so have provided the dummy data and elasticsearch exact query which I am hitting.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 16, 2020, 4:34pm UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/9 "2020-10-16T16:34:13Z")

</div>

Could you share a script with dummy data that we can just paste in Kibana dev console and then click on the play button to execute it?

---

<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:** [November 13, 2020, 4:34pm UTC](https://discuss.elastic.co/t/alphabetically-sorting-aggregation-elasticsearch/251414/10 "2020-11-13T16:34:19Z")

</div>

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