# How to convert string field to number and then apply rang

**URL:** https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785
**Category:** Elasticsearch
**Created:** [December 21, 2017, 9:47am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785 "2017-12-21T09:47:48Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![joe\_print](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@joe\_print](https://discuss.elastic.co/u/joe_print)
#### Post date: [December 21, 2017, 9:47am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/1 "2017-12-21T09:47:48Z")

</div>

Hi,  
I have a field in Elastic that its type is number but its saved as string,  
now I want to know is it possible to treat it as number and apply range operation on that?

---

<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: [December 21, 2017, 10:00am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/2 "2017-12-21T10:00:38Z")

</div>

You need to reindex for that and change the mapping.

You can use reindex and an ingest pipeline to do that conversion or reindex from your source.

---

<div class="post-metadata">

### Author: ![joe\_print](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@joe\_print](https://discuss.elastic.co/u/joe_print)
#### Post date: [December 21, 2017, 10:05am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/3 "2017-12-21T10:05:02Z")

</div>

Thank you for your reply,  
unfortunately, I can't do this because this type can be number and string, I want to convert that field in query and then apply range operation,

---

<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: [December 21, 2017, 10:25am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/4 "2017-12-21T10:25:58Z")

</div>

You can't do both. If it's a String then a range on it will consider `21` as higher than `100`.

If you want to do number comparison it must be a number.

---

<div class="post-metadata">

### Author: ![joe\_print](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@joe\_print](https://discuss.elastic.co/u/joe_print)
#### Post date: [December 21, 2017, 10:49am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/5 "2017-12-21T10:49:51Z")

</div>

Can't I write any script to run before process that field?  
or if I could treat it as the date that is millisecond it works too. I can have customized date format, can't I do this?

---

<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: [December 21, 2017, 11:02am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/6 "2017-12-21T11:02:16Z")

</div>

Oh? It's a date?

You did not tell before.

You can provide a date as a number or as a string but in the mapping it must be a `date` datatype. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html)

---

<div class="post-metadata">

### Author: ![joe\_print](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@joe\_print](https://discuss.elastic.co/u/joe_print)
#### Post date: [December 21, 2017, 11:07am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/7 "2017-12-21T11:07:23Z")

</div>

actually no! it's not.  
what I get from your answers it's that I can't have dynamic convert type in a search. am I right?

---

<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: [December 21, 2017, 11:31am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/8 "2017-12-21T11:31:18Z")

</div>

No. You can't do miracles by comparing apples and oranges.

It's always better to have a clean schema. Just like with a SQL datastore, if you store everything as a String, you can't efficiently search for numbers, geo points, dates...

Have a look at elasticsearch ingest feature. That might help you to extract meaningful information from non structured data.

---

<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 18, 2018, 11:31am UTC](https://discuss.elastic.co/t/how-to-convert-string-field-to-number-and-then-apply-rang/112785/9 "2018-01-18T11:31:48Z")

</div>

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