How to convert string field to number and then apply rang


(Joe Print) #1

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?


(David Pilato) #2

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.


(Joe Print) #3

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,


(David Pilato) #4

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.


(Joe Print) #5

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?


(David Pilato) #6

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


(Joe Print) #7

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?


(David Pilato) #8

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.


(system) #9

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