Save field as float instead of String

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?

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

var body = {

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

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

        }
    },

}

}

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.

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

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.

I'd recommend trying deleting this part:

                "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.

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

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

1 Like

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?

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

Yes, I was just looking around for it. It's basically the put pipeline API. However, I'm looking for it's implementation in Node JS.

Using Convert Processor

@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?

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.

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