# Sorting on Integer field whose values are not the same length

**URL:** <https://discuss.elastic.co/t/sorting-on-integer-field-whose-values-are-not-the-same-length/112373>\
**Category:** Elasticsearch\
**Created:** [December 19, 2017, 8:22am UTC](https://discuss.elastic.co/t/sorting-on-integer-field-whose-values-are-not-the-same-length/112373 "2017-12-19T08:22:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bswingle](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bswingle](https://discuss.elastic.co/u/bswingle)\
**Post date:** [December 19, 2017, 8:22am UTC](https://discuss.elastic.co/t/sorting-on-integer-field-whose-values-are-not-the-same-length/112373/1 "2017-12-19T08:22:47Z")

</div>

Hello All,  
My elastic search index has about 325K records in it of data I've crawled over time and is no longer available publicly. I have an integer field (So i can use range) containing the items price. I'm having an issue when sorting as I get 1, 111, 21, etc. as my results and obviously not ordered by the actual value representation. I've researched this and it seems the little bit of information I've been able to find recommends to reindex the data with leading zeros to get all the values to the same length. Is there a simpler way to do this and if not has anyone written a script that could be used to bulk update this field in every record?

---

<div class="post-metadata">

**Author:** ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)\
**Post date:** [December 20, 2017, 5:21pm UTC](https://discuss.elastic.co/t/sorting-on-integer-field-whose-values-are-not-the-same-length/112373/2 "2017-12-20T17:21:30Z")

</div>

@bswingle for integers, you should not be seeing sorts that take length into consideration: it's an integer vs text-based sort. Are you sure the field is mapped as an integer? Sharing your mapping could help confirm: `GET <index_name>/_mapping`

if you are trying to change an existing field type, the easiest way is to do a re-index operation in combination with an Ingest pipeline. This [Stack Overflow](https://stackoverflow.com/questions/43120430/elasticsearch-mapping-rename-existing-field) answer shows how it'd work for a field re-name. In your case, the Convert or Script processors might be the most appropriate choice.

---

<div class="post-metadata">

**Author:** ![bswingle](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bswingle](https://discuss.elastic.co/u/bswingle)\
**Post date:** [December 20, 2017, 9:07pm UTC](https://discuss.elastic.co/t/sorting-on-integer-field-whose-values-are-not-the-same-length/112373/3 "2017-12-20T21:07:59Z")

</div>

Mike,  
It appears you are correct it looks like my mapping is text so i'll need to run that re-index process. I'll take a look at that example and come back if I have any questions. Thanks for the quick reply and all the guidance!

"price": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 256  
}  
},  
"fielddata": true  
}

Thanks  
Brian

---

<div class="post-metadata">

**Author:** ![bswingle](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bswingle](https://discuss.elastic.co/u/bswingle)\
**Post date:** [December 29, 2017, 11:34pm UTC](https://discuss.elastic.co/t/sorting-on-integer-field-whose-values-are-not-the-same-length/112373/4 "2017-12-29T23:34:25Z")

</div>

> [@Mike.Barretta](#):
>
> Convert

This worked perfectly but I ended up not needing the convert but here was my what I used with the convert in case it helps someone else!

PUT \_ingest/pipeline/my\_rename\_pipeline  
{  
"description" : "describe pipeline",  
"processors" : [  
{  
"convert": {  
"field": "price",  
"type": "integer"  
}  
}  
]  
}

POST \_reindex  
{  
"source": {  
"index": "myindex"  
},  
"dest": {  
"index": "mynewindex",  
"pipeline": "my\_rename\_pipeline"  
}  
}

---

<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 26, 2018, 11:34pm UTC](https://discuss.elastic.co/t/sorting-on-integer-field-whose-values-are-not-the-same-length/112373/5 "2018-01-26T23:34:52Z")

</div>

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