# ElasticSearch Aggregation + Sorting in 5.3

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregation-sorting-in-5-3/222054>\
**Category:** Elasticsearch\
**Created:** [March 4, 2020, 11:06am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sorting-in-5-3/222054 "2020-03-04T11:06:37Z")\
**Posts on this page:** 5\
**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:** [March 4, 2020, 11:06am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sorting-in-5-3/222054/1 "2020-03-04T11:06:37Z")

</div>

Hi Team,

I wanted to aggregate the data on a different field and also wanted to get the aggregated data on sorted fashion based on the name.

My data is :

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

```

My query :

```
{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "name:emp*^1000.0"
                    }
                }
            ]
        }
    },
    "aggs": {
        "groupbyid": {
            "terms": {
                "field": "groupbyid.raw",
                "order": {
                    "top_hit_agg": "desc"
                },
                "size": 10
            },
            "aggs": {
                "top_hit_agg": {
                    "avg": {
                        "script": "_score"
                    }
                }
            }
        }
    }
}

```

My mapping is :

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

```

I am getting data based on the average of the relevance of grouped records. Now, what I wanted is the first club the records based on the groupid and then in each bucket sort the data based on the name field.

I wanted grouping on one field and after that grouped bucket, I want to sort on another field. This is sample data.

There are other fields like created\_on, updated\_on. I also wanted to get sorted data based on that field. also get the data by alphabetically grouped.

Thanks,  
Het

---

<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:** [March 4, 2020, 11:09am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sorting-in-5-3/222054/2 "2020-03-04T11:09:14Z")

</div>

@elasticsearch -- Please help me for this problem.

---

<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:** [March 4, 2020, 12:27pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sorting-in-5-3/222054/3 "2020-03-04T12:27:27Z")

</div>

Read [this](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and specifically the "Also be patient" part.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an 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:** [March 4, 2020, 12:32pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sorting-in-5-3/222054/4 "2020-03-04T12:32:57Z")

</div>

> [@dadoonet](#):
>
> It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

Sure. Will take care of that

---

<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:** [April 1, 2020, 12:32pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sorting-in-5-3/222054/5 "2020-04-01T12:32:59Z")

</div>

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