Sort Terms Aggregations on Keyword field

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

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.

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