Change Elastic Search Mapping

Remapping elastic search index

For every change in the mapping or settings of the elastic search index, we have to create a new index as there is no way to edit an existing index.

So here are the basic requests that you have to fire to achieve it, but first thing first, lets create an alias for our existing index.

Step #1: Creating an alias

Having an alias for your index has many advantages as you can use the alias in your code and for every change in settings or mappings you can do it on your index and don’t need any change in code, so no deployments and zero downtime.

PUT old_index_name/_aliases
{
  "actions": [
    {
      "add": {
        "index": "old_index_name",
        "alias": "index_alias"
      }
    }
  ]
} 

Step #2: Check the aliases

GET cat/_aliases

Step #3: Create an index with the new mapping and settings

Step #4: Reindex the data from old index to new index

curl --request POST 
 --url http://elastic_search_url/_reindex 
 --data '{
  "source": {
    "index": "old_index_name"
  },
  "dest": {
    "index": "new_index_name"
  }
}'

Step #5: Check the count of documents

GET old_index_name/_count 

GET new_index_name/_count

Both the indices should have the same count of documents and it indicates the reindexing is completed.

Step #6: Switch the alias

Now its time to switch the alias to point to new index, so that the new index starts serving our requests.

curl --request POST \
 --url http://elastic_search_url/_aliases \
 --data '{
  "actions": [
    {
      "remove": {
        "index": "old_index_name",
        "alias": "index_alias_name"
      },
      "add": {
        "index": "new_index_name",
        "alias": "index_alias_name"
      }
    }
  ]
}'

Now, you can delete the old index and continue using the new one.

Some bonus queries :

To update existing documents

#Add a new field

curl --request PUT \
 --url http://elastic_search_url/index_name/mapping/index_type_name \
 --data '{
  "properties": {
    "Field_name": {
      "type": "keyword"
    }
  }
}'

#Delete a field from all documents

curl --request POST \
 --url http://elastic_search_url/index_name/_update_by_query \
 --data '{
  "script": {
    "source": "ctx._source.remove('\''field_name'\'')"
  }
}'

#Update a field’s value

curl --request POST \
 --url http://index_name/type/id/_update \
 --data '{
  "script": "ctx._source.field_name = '\''new value'\''"
}'

Here is the collection available for the steps that we followed, for remapping our old index to a new onePostman collection

Hope you like the article. Kindly share your feedback.

Ciao,

Shreya

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s