I have a field that I want to change its' data type. I know it is not possible to change mapping without reindexing. I also have seen some solutions like this, but I need a Pythonic solution. In this example test_new
is a float
data type, that I want to convert it to integer
assuming the values are integer. I implemented two approaches, but none of them do the change. Here is my code:
def replace_mapping(es, old_index, new_index, new_mapping):
# 1-1) Delete old-index
# es.indices.delete(index=old_index, ignore=[400, 404])
# 1-2) Create index with nothing in it.
es.indices.create(index=new_index, ignore=[400, 404], body=new_mapping)
# 1-3) Reindex to copy data from old index to new index
helpers.reindex(es, source_index=old_index, target_index=new_index)
# Allow writing
es.indices.put_settings(index=old_index, body={"index.blocks.write": True})
def replace_mapping2(es, old_index, new_index, new_mapping)
# 2-1) Create alias for existing index
if es.indices.exists(old_index):
es.indices.put_alias(index=old_index, name=old_index+'_alias')
# 2-2) Create new index with nothing in it.
es.indices.create(index=new_index, ignore=[400, 404], body=new_mapping)
# 2-3) Reindex to copy data from old index to new index
helpers.reindex(es, source_index=old_index+'_alias', target_index=new_index)
# 2-4) Move the alias index_alias from old_index to new_index.
es.indices.put_alias(index=new_index, name=old_index + '_alias')
new_mapping = {
"properties": {
"test_new": {"type": "integer"}
}
}
replace_mapping(es, 'v1_backup', 'v1_new', new_mapping)