# Strings in integer fields?

**URL:** <https://discuss.elastic.co/t/strings-in-integer-fields/58301>\
**Category:** Elasticsearch\
**Created:** [August 17, 2016, 11:59pm UTC](https://discuss.elastic.co/t/strings-in-integer-fields/58301 "2016-08-17T23:59:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spazm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spazm/32/7256_2.png) [@spazm](https://discuss.elastic.co/u/spazm)\
**Post date:** [August 17, 2016, 11:59pm UTC](https://discuss.elastic.co/t/strings-in-integer-fields/58301/1 "2016-08-17T23:59:58Z")

</div>

I've noticed that if I define a field as type `integer` I'm allowed to insert values as either strings-of-integers or integers. And when I pull these values back out, they come back in the same form.

I'd like my integers to all be saved as integers, converting as necessary. Is this possible? Am I misunderstanding numeric field types?

For filtering and querying, the integer and string-of-integer values act the same. (good!)  
They are not the same for json output. (maybe bad!)  
They are not the same in groovy. (e.g. `.contains()`) (bad!)

In my application, I have a groovy script to do conditional inserts and removals into an array of values in a document, short-circuiting to a no-op (`ctx.op="none"`) if the value is (not) already present -- treating an array of label\_ids as a set. This logic gets broken if given a mix of integers and strings-of-integers.

> <https://gist.github.com/spazm/efb652831e6fb0f9db4e6640a31e3817>
>
> There are more than three files. show original

Any pointers would be appreciated.

Thanks,  
@spazm

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [August 19, 2016, 9:52am UTC](https://discuss.elastic.co/t/strings-in-integer-fields/58301/2 "2016-08-19T09:52:31Z")

</div>

Maybe you are looking for the `coerce` option? [https://www.elastic.co/guide/en/elasticsearch/reference/current/coerce.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/coerce.html)

---

<div class="post-metadata">

**Author:** ![spazm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spazm/32/7256_2.png) [@spazm](https://discuss.elastic.co/u/spazm)\
**Post date:** [August 25, 2016, 5:29am UTC](https://discuss.elastic.co/t/strings-in-integer-fields/58301/3 "2016-08-25T05:29:47Z")

</div>

coerce (default=true) allows quoted numbers to be coerced into numeric fields. But for some reason, ES remembers the original form of string vs numeric, and returns values in their original form.

with coerce=false, the strings would be rejected as invalid.

What I want is a stronger coercion 🙂

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [August 31, 2016, 9:41am UTC](https://discuss.elastic.co/t/strings-in-integer-fields/58301/4 "2016-08-31T09:41:59Z")

</div>

Right, elasticsearch does not alter the source document, so this can only be fixed on client side.

---

<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:** [July 5, 2017, 10:23pm UTC](https://discuss.elastic.co/t/strings-in-integer-fields/58301/5 "2017-07-05T22:23:41Z")

</div>


