Changing mapping fields structure flow

I have an index with the mappings

{
   "mappings": {
      "properties": {
         "title": {
            "type": "text"
         },
         "location": {
            "type": "keyword"
         }
      }
   }
}

In location field at the moment we are storing the city name.
And we need to change the mapping structure to store also the country and state, so the mapping will be

{
   "mappings": {
      "properties": {
         "title": {
            "type": "text"
         },
         "location": {
            "properties": {
                "country": {
                    "type": "keyword"
                },
                "state": {
                    "type": "keyword"
                },
                "city": {
                    "type": "keyword"
                }
            }
         }
      }
   }
}

What is the recommended flow for such migration?

This requires a full reindexing operation, as you are changing the structure of a field from a keyword to a full blown object. This will not work on an existing index, but requires a new one with a new mapping.

You could use the reindex API in combination with a script or an index pipeline to achieve this by changing the JSON before it gets indexed into the new index.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.