# Varying data types in object causing mapping errors

**URL:** https://discuss.elastic.co/t/varying-data-types-in-object-causing-mapping-errors/341717
**Category:** Elasticsearch
**Created:** [August 25, 2023, 7:35pm UTC](https://discuss.elastic.co/t/varying-data-types-in-object-causing-mapping-errors/341717 "2023-08-25T19:35:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Wesley84](https://avatars.discourse-cdn.com/v4/letter/w/b4bc9f/32.png) [@Wesley84](https://discuss.elastic.co/u/Wesley84)
#### Post date: [August 25, 2023, 7:35pm UTC](https://discuss.elastic.co/t/varying-data-types-in-object-causing-mapping-errors/341717/1 "2023-08-25T19:35:40Z")

</div>

I have a data object called "weekly\_values" that I want to send to an existing elastic index. This object contains fields which when populated have a float data type. However these fields do not always have a value and when they don't I set them to a default of '0'. I use dynamic mapping, and when I first sent this object to elastic the very first time it encountered some of these fields they had a value of '0' so elastic mapped them as a long. Then the first time it encountered that same field with data, which was a float, it fails to map (see attached). As a fix, I set the default value to 0.0 instead of 0 thinking this would set the field as a float whether it has data or not. But I discovered that elastic rounds 0.0 to 0 so it still treats it as a long. I tried using the update mapping API to explicitly set these field's data types in advance but there are just too many (about 300). I'd like to continue using dynamic mapping, how can I prevent this mapping error? Thanks!

 ![Screenshot 2023-08-17 at 3.01.42 PM](https://us1.discourse-cdn.com/elastic/original/3X/f/f/ff8f245e891b90961fd57517b72bbd9061f52711.png)

---

<div class="post-metadata">

### Author: ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)
#### Post date: [August 25, 2023, 8:04pm UTC](https://discuss.elastic.co/t/varying-data-types-in-object-causing-mapping-errors/341717/2 "2023-08-25T20:04:20Z")

</div>

> [@Wesley84](#):
>
> As a fix, I set the default value to 0.0 instead of 0 thinking this would set the field as a float whether it has data or not.

Did you send the data to a new index? It won't work if you are still using the same index as the mapping cannot be changed.

You need to use a new index.

> [@Wesley84](#):
>
> But I discovered that elastic rounds 0.0 to 0 so it still treats it as a long.

I don't think this is correct, if Elasticsearch receives a numeric value of `0.0` it will map the field as float.

You can test with the following requests:

```auto
POST discuss-float-example/_doc/
{
  "float_value": 0.0,
}

```

And using `GET discuss-float-example/_mapping` will return the following:

```auto
{
  "discuss-float-example": {
    "mappings": {
      "properties": {
        "float_value": {
          "type": "float"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Wesley84](https://avatars.discourse-cdn.com/v4/letter/w/b4bc9f/32.png) [@Wesley84](https://discuss.elastic.co/u/Wesley84)
#### Post date: [August 28, 2023, 2:39pm UTC](https://discuss.elastic.co/t/varying-data-types-in-object-causing-mapping-errors/341717/3 "2023-08-28T14:39:28Z")

</div>

Thanks for the reply. You're right 0.0 does map as a float, I think my issue was actually related to some bad data. It's been resolved.

---

<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: [September 25, 2023, 2:40pm UTC](https://discuss.elastic.co/t/varying-data-types-in-object-causing-mapping-errors/341717/4 "2023-09-25T14:40:17Z")

</div>

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