Reindexing Data in Elasticsearch changing Field Type

The below script is useful for reindexing data in Elasticsearch where there are field mapping conflicts (Eg data has been indexed as multiple field types – Say, string and number). This script will process all indexes matching a patter (Eg if you have daily indexes).

The below script will:

  • Match indexes based on a pattern provided in the top variable
  • Create a new index for each found in the search
  • Set the correct field mappings (Multiple can be set)
  • Reindex the old index onto the new one
  • Delete the original index.

Be careful with the below script as it is destructive and unforgiving!



for currentIndex in $(curl -X GET "http://localhost:9200/_cat/indices?v&pretty" | grep "$indexPattern" | awk {'print $3'}); do 
    curl -X PUT "localhost:9200/$currentIndex-reindexed"
    # Set Field mapping for new index field
    curl -XPUT "localhost:9200/"$currentIndex-reindexed"/_mapping/doc" -H 'Content-Type: application/json' -d '{
        "properties": {
            "details.field1": { "type": "integer" },
            "details.field2": { "type": "integer" },
            "details.field3": { "type": "integer" }

    # Reindex to new index
    curl -H Content-Type:application/json -XPOST localhost:9200/_reindex?pretty -d'{
        "source": {
            "index": "'$currentIndex'"
        "dest": {
            "index": "'$currentIndex'-reindexed"

    # Delete original index
    curl -XDELETE localhost:9200/$currentIndex

Any comments or questions? Get in touch here or Email me at [email protected]