# Coerce attribute ineffective?

**URL:** <https://discuss.elastic.co/t/coerce-attribute-ineffective/94003>\
**Category:** Elasticsearch\
**Created:** [July 20, 2017, 6:41pm UTC](https://discuss.elastic.co/t/coerce-attribute-ineffective/94003 "2017-07-20T18:41:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)\
**Post date:** [July 20, 2017, 6:41pm UTC](https://discuss.elastic.co/t/coerce-attribute-ineffective/94003/1 "2017-07-20T18:41:09Z")

</div>

Hi,

I have a mapping with this :

```auto

              "mins" : {
                "type" : "byte",
				"coerce" : true,
				"index" : false 
}

```

But attempting to reindex into it I get:

```auto
"failed to parse [mins]","caused_by":{"type":"number_format_exception","reason":"For input string: \"0.0\""

```

Am I misunderstanding what 'coerce' is supposed to do?

Thanks

Paul

---

<div class="post-metadata">

**Author:** ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)\
**Post date:** [July 20, 2017, 10:18pm UTC](https://discuss.elastic.co/t/coerce-attribute-ineffective/94003/2 "2017-07-20T22:18:00Z")

</div>

Actually, I've had a good mess about with 'coerce' and here is how it is behaving in 5.5:

indexing a value of "1" will succeed. Indexing a value of 1.1 will succeed. However, "1.0" (or "1._any digit_") will fail.

You can see from the [relevant code block](https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java#L389-L405) that this is how it is coded to work (if the value is a string, coerce is apparently ignored altogether, it just tries to parse it)....  
I'm not certain that is right, so I have opened [this issue](https://github.com/elastic/elasticsearch/issues/25819) to suss it out 🙂

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [July 20, 2017, 10:31pm UTC](https://discuss.elastic.co/t/coerce-attribute-ineffective/94003/3 "2017-07-20T22:31:47Z")

</div>

We were just chatting about this internally and I was about to open an issue as well 🙂

The behavior is a bit surprising (for me too). The behavior is basically what you described:

- Floating numerics are truncated
- Integral strings are converted via string parsing
- Floating strings fail, because the integral string parsing bit doesn't know how to deal with them and we don't do anything to truncate the `.xxx` portion (or cast to double first and check to see if integral like we do in the numeric portion)

I think it should be fixed, and is fixable. 🙂

---

<div class="post-metadata">

**Author:** ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)\
**Post date:** [July 20, 2017, 10:41pm UTC](https://discuss.elastic.co/t/coerce-attribute-ineffective/94003/4 "2017-07-20T22:41:40Z")

</div>

Cool bananas, thanks Zachary... Agreed it looks pretty fixable; I'm happy to volunteer my assistance, though I currently have no dev env standing up with the appropriate tooling or anything 🙂

---

<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:** [August 17, 2017, 10:42pm UTC](https://discuss.elastic.co/t/coerce-attribute-ineffective/94003/5 "2017-08-17T22:42:05Z")

</div>

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