# How order aggregations using elasticsearch 7.15

**URL:** https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645
**Category:** Elasticsearch
**Created:** [October 13, 2021, 3:40pm UTC](https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645 "2021-10-13T15:40:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![acantepie](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@acantepie](https://discuss.elastic.co/u/acantepie)
#### Post date: [October 13, 2021, 3:40pm UTC](https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645/1 "2021-10-13T15:40:21Z")

</div>

Hello,

I have the following mapping :

```auto
"COLOR": {
      "properties": {
        "order": {
          "type": "long"
        },
        "value": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    },

```

```auto
"COLOR": {
    "value": "Rose",
    "order": 6
  },

```

How can i order value by value of `order` field on my search request ?

```auto
{
    query: ...,
   aggs: {
       terms: { field: COLOR.value.keyword, order: ???? )
   }

```

Thx for helping

PS: post edited 🙂

---

<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 13, 2021, 3:54pm UTC](https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645/2 "2021-10-13T15:54:01Z")

</div>

You want to order by value (alphabetical sort on `Rose`...) or by count (10, 9, 9) or by the value of the `order` field?

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: ![acantepie](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@acantepie](https://discuss.elastic.co/u/acantepie)
#### Post date: [October 13, 2021, 4:02pm UTC](https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645/3 "2021-10-13T16:02:25Z")

</div>

Thx for answer, i want to order by the value of the `order` field.  
I will try to post a script toworrow, i am currently not using Kibana to manage ES.

---

<div class="post-metadata">

### Author: ![acantepie](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@acantepie](https://discuss.elastic.co/u/acantepie)
#### Post date: [October 14, 2021, 8:11am UTC](https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645/4 "2021-10-14T08:11:25Z")

</div>

Kibana script

```auto
POST test1/_doc/1
{
  "COLOR": {
    "value": "Rose",
    "order": 2
  }
}

POST test1/_doc/2
{
  "COLOR": {
    "value": "Jaune",
    "order": 1
  }
}

POST test1/_doc/3
{
  "COLOR": {
    "value": "Bleu",
    "order": 3
  }
}

GET test1/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_color": {
      "terms": {
        "field": "COLOR.value.keyword"
      } 
    }
  }
}

```

Result :

```auto
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "COLOR" : {
            "value" : "Rose",
            "order" : 2
          }
        }
      },
      {
        "_index" : "test1",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "COLOR" : {
            "value" : "Jaune",
            "order" : 1
          }
        }
      },
      {
        "_index" : "test1",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "COLOR" : {
            "value" : "Bleu",
            "order" : 3
          }
        }
      }
    ]
  },
  "aggregations" : {
    "group_by_color" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Bleu",
          "doc_count" : 1
        },
        {
          "key" : "Jaune",
          "doc_count" : 1
        },
        {
          "key" : "Rose",
          "doc_count" : 1
        }
      ]
    }
  }
}

```

Wanted result :  
On `aggregations.group_by_color.buckets`, i want my aggregation values ordered by `COLOR.order`

---

<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 14, 2021, 12:59pm UTC](https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645/5 "2021-10-14T12:59:09Z")

</div>

You can do this:

```auto
GET test1/_search
{
  "size": 0, 
  "aggs": {
    "group_by_color": {
      "terms": {
        "field": "COLOR.value.keyword",
        "order": { "order": "desc" }
      },
      "aggs": {
        "order": {
          "max": {
            "field": "COLOR.order"
          }
        }
      }
    }
  }
}

```

It gives:

```auto
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "group_by_color" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Bleu",
          "doc_count" : 1,
          "order" : {
            "value" : 3.0
          }
        },
        {
          "key" : "Rose",
          "doc_count" : 1,
          "order" : {
            "value" : 2.0
          }
        },
        {
          "key" : "Jaune",
          "doc_count" : 1,
          "order" : {
            "value" : 1.0
          }
        }
      ]
    }
  }
}

```

---

<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 11, 2021, 12:59pm UTC](https://discuss.elastic.co/t/how-order-aggregations-using-elasticsearch-7-15/286645/6 "2021-11-11T12:59:46Z")

</div>

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