# Sort Terms Aggregations on Keyword field

**URL:** <https://discuss.elastic.co/t/sort-terms-aggregations-on-keyword-field/161175>\
**Category:** Elasticsearch\
**Created:** [December 17, 2018, 2:41pm UTC](https://discuss.elastic.co/t/sort-terms-aggregations-on-keyword-field/161175 "2018-12-17T14:41:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![naceur\_ben\_said](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@naceur\_ben\_said](https://discuss.elastic.co/u/naceur_ben_said)\
**Post date:** [December 17, 2018, 2:41pm UTC](https://discuss.elastic.co/t/sort-terms-aggregations-on-keyword-field/161175/1 "2018-12-17T14:41:37Z")

</div>

Hi,  
I have a issue with sorting Terms aggragtion in the keyword field, this is my exemple :

```
PUT event
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "idEvent": {
          "type": "keyword"
        }
      }
    }
  }
}

POST event/_doc/
{
"idEvent" : "99810113"
}
POST event/_doc/
{
"idEvent" : "96614187"
}
POST event/_doc/
{
"idEvent" : "106194437"
}
POST event/_doc/
{
  "idEvent": "106193941"
}

GET event/_search
{
  "size": 0,
  "aggs": {
    "event": {
      "terms": {
        "field": "idEvent",
        "size": 10,
        "order": {
          "_key": "desc"
        }
      }
    }
  }
}

```

idEvent is an Integer value and i want to sort this fiel with its numeric value, but here is the result :

```
"buckets": [
{
"key": "99810113",
"doc_count": 1
},
{
"key": "96614187",
"doc_count": 1
},
{
"key": "106194437",
"doc_count": 1
},
{
"key": "106193941",
"doc_count": 1
}
]

```

I also tryed this :

```
GET event/_search
{
  "size": 0,
  "aggs": {
    "event": {
      "terms": {
        "field": "idEvent",
        "script": {
          "source": "_value"
        },
        "size": 10,
        "order": {
          "_key": "desc"
        }
      }
    }
  }
}

```

Also this :

```
GET event/_search
{
  "size": 0,
  "aggs": {
    "event": {
      "terms": {
        "field": "idEvent",
        "script": {
          "source": "Integer.parseInt(_value)"
        },
        "size": 10,
        "order": {
          "_key": "desc"
        }
      }
    }
  }
}

```

is this a bug ? how can i work arround ?  
thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 17, 2018, 2:54pm UTC](https://discuss.elastic.co/t/sort-terms-aggregations-on-keyword-field/161175/2 "2018-12-17T14:54:33Z")

</div>

> [@naceur\_ben\_said](#):
>
> idEvent is an Integer value

You declared it a `keyword` field not an `integer`.  
You either need to map it as an integer or make the keyword strings the same length by zero prefixing if you want sorting to work the way I expect you want.

---

<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:** [January 14, 2019, 2:59pm UTC](https://discuss.elastic.co/t/sort-terms-aggregations-on-keyword-field/161175/3 "2019-01-14T14:59:13Z")

</div>

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