Changing type of an existing field in Elasticsearch

Views: 59   

In Elasticsearch you can't change the type of a field once the data indexed. So there is no straight way to so this in Elasticsearch. You need to follow the below steps to achieve this.

  1. create temp index
  2. put the mapping for the field with the type you want in temp index
  3. re-index data from source index to temp index
  4. drop the actual index
  5. create the actual index
  6. Put the mapping
  7. re-index data from temp index to actual index
  8. drop temp index

Following are the calls you need to make for the above points. For example, I am taking the index name as 'users', type as 'doc' and changing the field 'location' from String to geo_point

1. create temp index

PUT users_temp

2. put the mapping for the field with the type you want in temp index

PUT users_temp/_mapping/doc 
{
  "properties": {
    "location": {
      "type": "geo_point"
    }
  }
}

3. re-index data from source index to temp index

POST _reindex
{
  "source": {
    "index": "users"
  },
  "dest": {
    "index": "users_temp"
  }
}

4. drop the actual index

DELETE /users

5. create the actual index

PUT users

6. Put the mapping

PUT users/_mapping/doc
{
  "properties": {
    "location": {
      "type": "geo_point"
    }
  }
}

7. re-index data from temp index to actual index

POST _reindex
{
  "source": {
    "index": "users_temp"
  },
  "dest": {
    "index": "users"
  }
}

8. drop temp index

DELETE /users_temp
On By