Elastic Search

How Do I Change the Field Type in Elasticsearch?

Using the _ mapping API, you can update existing fields or add new fields to an existing index.

NOTE: To perform changes to an index, ensure you have the manage privileges on the target index.

Basic Usage

To modify a field type, send a PUT request to the _mapping API followed by the request body. The request body includes the properties parameter and mapping of the target field. When creating a new field, ensure that you include the field name, type, and mapping parameters.

For example, the following request changes the field type from integer to long.

PUT /my-index/_mapping
{
"properties": {
"base_price": {
"type": "long"
  }
  }
}

Upon successful completion, you should see an output as:

{
  "acknowledged" :true
}

Re-indexing Method

In most cases, Elasticsearch will prevent you from updating the field type of an existing index. Doing so could lead to the present data becoming invalid and causing errors in the index.

If you still want to update the type of an existing field, you can do it in a few simple steps.

  1. Create a new index with the correct mapping info where the field type is changed to your desired type.
  2. Re-index the data from your old index to the new index.
  3. Remove the old index

Using this method allows you to reduce the minimal downtime for your index.

Creating an Old index

Let us start by creating an index with the incorrect field type.

PUT /change-me
{
"mappings": {
"properties": {
"id": {
"type": "integer"
      },
"username": {
"type": "text"
  }
    }
  }
}

In the example above, we have a simple index with the two fields: id and username. The field types are integer and text, respectively.

Let us assume the field contains data as shown in the following query:

POST /change-me/_doc

{
"id": 1000,
"username": "root"
}
POST /change-me/_doc
{
"id": 1001,
"username": "other"
}

The two queries above will create a document with the data as provided in the request body.

Ensure the data exists:

GET /change-me/_search?pretty
{
"query": {
"match_all": {}
  }
}

We should see the two records as shown:

Create a new index

Let’s say we want to change the id field from an integer to a keyword. We will start by creating a new index with the type as keywords.

PUT /change-me-reindex
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
      },
"username": {
"type": "text"
  }
    }
  }
}

In the request above, we create a new index and set the id type to a keyword.

Re-index the old data

The next step is to re-index the data from the old index to the new one using the _ re-index API. The request for that is below:

POST /_reindex
{
"source": {
"index": "change-me"
  },
"dest": {
"index": "change-me-reindex"
  }
}

The above request will copy the documents from the old index to the new one where the field type changes from an integer to a keyword.

Output from the above query:

{
  "took" : 8,
  "timed_out" : false,
  "total" : 4,
  "updated" : 0,
  "created" : 4,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

Delete the old index

Now that we have an updated index with the correct mapping, it is time to remove the old index. We can do this by sending a DELETE request to the index as:

DELETE /change-me

Upon successful removal, you should see an output as:

{
  "acknowledged" :true
}

Create index alias

If you had applications using the old index, they might stop working since it no longer exists.

We can solve this by creating an alias for the new index with the name of the old index.

PUT /change-me-reindex/_alias/change-me

The above request should create an alias for the new index.

Conclusion

In this guide, you discovered how to change the type of an existing field in an Elasticsearch index.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list