# Save field as float instead of String

**URL:** <https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674>\
**Category:** Elasticsearch\
**Created:** [April 19, 2018, 10:42am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674 "2018-04-19T10:42:59Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 19, 2018, 10:42am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/1 "2018-04-19T10:42:59Z")

</div>

So basically, I'm doing a bulk insert for a huge amount of data.  
There is a field in that data called price which is of String data type by default. However, what I want is to save that field in ES as a float data type. I tried changing type = float in the mapping.; However, it's not having any effect. Do I need to do anything else?

---

<div class="post-metadata">

**Author:** ![kartheek91](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kartheek91](https://discuss.elastic.co/u/kartheek91)\
**Post date:** [April 19, 2018, 11:54am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/2 "2018-04-19T11:54:34Z")

</div>

So can you show me the insert script so that we can figure out what might be the reason.

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 19, 2018, 12:13pm UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/3 "2018-04-19T12:13:41Z")

</div>

```
var body = {

"mappings": {
    "products": {
        "properties": {

            "price": {
                "type": "float",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }

        }
    },

}

```

}

---

<div class="post-metadata">

**Author:** ![atira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atira/32/28699_2.png) [@atira](https://discuss.elastic.co/u/atira)\
**Post date:** [April 19, 2018, 12:18pm UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/4 "2018-04-19T12:18:20Z")

</div>

Does the field already exist in the index?  
You cannot change the mapping of an already existing field (with some exceptions), you would have to create a new index with the new mapping and reindex the data again.

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 19, 2018, 12:39pm UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/5 "2018-04-19T12:39:15Z")

</div>

No. I first deleted the entire index that was previously created.

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 19, 2018, 12:43pm UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/6 "2018-04-19T12:43:51Z")

</div>

The index is created after the mapping. And it works fine since I had many more changes that were done in the mapping structure. I wasn't able to paste the entire code due to an 8000 character limit. Hence I only mentioned the field that I was talking about.

---

<div class="post-metadata">

**Author:** ![atira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atira/32/28699_2.png) [@atira](https://discuss.elastic.co/u/atira)\
**Post date:** [April 19, 2018, 12:58pm UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/7 "2018-04-19T12:58:25Z")

</div>

I'd recommend trying deleting this part:

```auto
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }

```

This is for strings. I think it's usually for creating two fields for a string, a text and a keyword type. It's the default mapping for fields when not manually defined. `keyword` doesn't make sense for a number field type.

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 19, 2018, 3:07pm UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/8 "2018-04-19T15:07:59Z")

</div>

Tried it.  
Still, the price eventually gets saved as **"price": "100.00"**

 ![Screenshot%20at%20Apr%2019%2020-42-52](https://us1.discourse-cdn.com/elastic/original/3X/0/d/0de73f92304672b41383d4f286ad1b6d2e949622.png)

---

<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:** [April 20, 2018, 2:03am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/9 "2018-04-20T02:03:30Z")

</div>

WDYM? That the \_source is still showing a String ?

If so that is expected as elasticsearch never transforms the document you sent.

If you want to modify \_source, have a look at [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/convert-processor.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/convert-processor.html)

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 20, 2018, 2:16am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/10 "2018-04-20T02:16:49Z")

</div>

Yes. This is what I was looking for.  
Thanks @dadoonet. However, can you guide me as to how should I use the convert processor in my mapping?

---

<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:** [April 20, 2018, 2:45am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/11 "2018-04-20T02:45:05Z")

</div>

It's not a mapping thing. See [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/ingest.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/ingest.html)

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 20, 2018, 2:46am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/12 "2018-04-20T02:46:42Z")

</div>

Yes, I was just looking around for it. It's basically the [put pipeline API](https://www.elastic.co/guide/en/elasticsearch/reference/current/put-pipeline-api.html). However, I'm looking for it's implementation in [Node JS](https://www.npmjs.com/package/elasticsearch).

[Using Convert Processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/convert-processor.html)

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 20, 2018, 2:57am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/13 "2018-04-20T02:57:25Z")

</div>

@dadoonet Can we close this thread? I do know now about the direction that I need to head over to achieve my purpose. However, since I'm using the npm's elasticsearch library, I'm specifically looking to achieve what I want using that library. I've posted a question on stackoverflow . Should I create another new post in this forum?

---

<div class="post-metadata">

**Author:** ![Abhilash\_Sathe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_sathe/32/28555_2.png) [@Abhilash\_Sathe](https://discuss.elastic.co/u/Abhilash_Sathe)\
**Post date:** [April 20, 2018, 4:13am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/14 "2018-04-20T04:13:04Z")

</div>

I got my solution. Thanks @dadoonet You may close this post.

Basically, adding the type = float did work. It was just that I wasn't able to see the changes in \_source. However, while querying, it did consider the value as a float.

---

<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:** [May 18, 2018, 4:13am UTC](https://discuss.elastic.co/t/save-field-as-float-instead-of-string/128674/15 "2018-05-18T04:13:13Z")

</div>

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