# How to sort the composite aggregation results by score?

**URL:** <https://discuss.elastic.co/t/how-to-sort-the-composite-aggregation-results-by-score/148952>\
**Category:** Elasticsearch\
**Created:** [September 18, 2018, 9:51am UTC](https://discuss.elastic.co/t/how-to-sort-the-composite-aggregation-results-by-score/148952 "2018-09-18T09:51:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![betty](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@betty](https://discuss.elastic.co/u/betty)\
**Post date:** [September 18, 2018, 9:51am UTC](https://discuss.elastic.co/t/how-to-sort-the-composite-aggregation-results-by-score/148952/1 "2018-09-18T09:51:04Z")

</div>

Elasticsearch ver: 6.2.4

I have now got the results of composite aggregation by:

```auto
GET index/_search
{
  "aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "name": {
              "terms": {
                "field": "name.keyword"
                }
            }
          }
        ]
      }
    }
  },
  "query":{

   }
}

```

To simplify the question, the query part is omitted here.

I can get the results like:

```auto
"aggregations": {
    "my_buckets": {
      "buckets": [
        {
          "key": {
            "name": "Bill Gates"
          },
          "doc_count": 1
        },
        {
          "key": {
            "name": "Steven Paul Jobs"
          },
          "doc_count": 1
        },
        {
          "key": {
            "name": "Gates Godar"
          },
          "doc_count": 1
        }
        ]
       }
      }

```

I noticed that the order of the search results is not sorted by the score. If I use the following DSL:

```auto
{
  "_source": "name",
  "query":{

   }
}

```

The results are ordered by score. That's what I want. But I need to use aggregations to group several attributes in the index and get the value.

How can we achieve to sort the composite aggregation results by the score? Is there a way to combine the "aggregations" with the "\_source" part? It confuses me for several days, any idea is appreciated!

Thanks.

---

<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:** [October 16, 2018, 9:51am UTC](https://discuss.elastic.co/t/how-to-sort-the-composite-aggregation-results-by-score/148952/2 "2018-10-16T09:51:06Z")

</div>

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