How to convert string field to number and then apply rang

Hi,
I have a field in Elastic that its type is number but its saved as string,
now I want to know is it possible to treat it as number and apply range operation on that?

You need to reindex for that and change the mapping.

You can use reindex and an ingest pipeline to do that conversion or reindex from your source.

1 Like

Thank you for your reply,
unfortunately, I can't do this because this type can be number and string, I want to convert that field in query and then apply range operation,

You can't do both. If it's a String then a range on it will consider 21 as higher than 100.

If you want to do number comparison it must be a number.

1 Like

Can't I write any script to run before process that field?
or if I could treat it as the date that is millisecond it works too. I can have customized date format, can't I do this?

Oh? It's a date?

You did not tell before.

You can provide a date as a number or as a string but in the mapping it must be a date datatype. See https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html

1 Like

actually no! it's not.
what I get from your answers it's that I can't have dynamic convert type in a search. am I right?

No. You can't do miracles by comparing apples and oranges.

It's always better to have a clean schema. Just like with a SQL datastore, if you store everything as a String, you can't efficiently search for numbers, geo points, dates...

Have a look at elasticsearch ingest feature. That might help you to extract meaningful information from non structured data.

1 Like

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