elasticsearchpyelasticsearch

Can't set types of fields when creating indexes


I am using Elasticsearch 7.6.2 (by running the official docker image with the default options) and py-elasticsearch 7.6.0 (installed with version >=7.6 <8)

I am trying to create an index with a float field like this (as described in the lib docs):

index_name = 'test_index'

body = {'mappings': {'properties': {'float_field': {'type': 'float'}}}}

client.create(index_name, body=body)

then I test if the type is working (I try to index an int to see if it converts to float as set in the mappings during index creation above):

id_ = 123
value = 11

client.index(index_name, body={'float_field': value}, id=id_)

r = client.get(index_name, id=id_)['_source']['float_field']

And the resulting value is integer, not float:

assert r == value  # passes
assert isinstance(r, float)  # AssertionError

so the mapping of float_field to float type doesn't seem to work, what am I doing wrong here?

the types seem to only be inferred automatically no matter what I set in mappings.


Solution

  • Setting the type to float will only index the value as a float inside the inverted index, but ES will never change whatever values you added inside the source document, so the value you retrieve from the source document you indexed (i.e. from a GET call) will always be the original one.

    Bottom line: ES does not do any type conversion to field values inside your source document.