Index mapping changed from object to nested and reindex

Hello I want to change an index property which is object (default) to nested type and keep the data. The process I'm following is creating a new index with the new mappings then use the reindex API.

The problem comes when I use the reindex API to reindex from Old_index to the New_index I'm getting an obvious error.

OLD Index (cities is a list)

    {
       "settings":{
          "number_of_shards":1,
          "number_of_replicas":1
       },
       "mapping":{
          "properties":{
             "cities":{
                "type":"object",
                "properties":{
                   "name":{
                      "type":"keyword"
                   }
                }
             }
          }
       }
    }

New INDEX (cities is a list but now nested with new properties on its name)

    {
       "settings":{
          "number_of_shards":1,
          "number_of_replicas":1
       },
       "mapping":{
          "properties":{
             "cities":{
                "type":"nested",
                "properties":{
                   "id":{
                      "type":"keyword"
                   },
                   "name":{
                      "type":"object",
                      "properties":{
                         "english":{
                            "type":"text"
                         },
                         "dutch":{
                            "type":"text"
                         },
                         "raw":{
                            "type":"keyword"
                         }
                      }
                   }
                }
             }
          }
       }
    }

The new index will use custom analyzers (which I ignored in the example) but when the reindex api tries to index the data from old index to new index in documents where cities is not empty. Following error appears as a failure:

object mapping for [cities.name] tried to parse field articleTitle as object, but found a concrete value

Is there a way to make this index change and reindex all data?

1 Like

Hi @Clony , I have similary need and I worked on it since two days.
I found an example of solution for my case : I wanted to test reindex to move a tags field type keyword with multi-value inside to a nested tags with each value

Original mapping

> "tags": {
>     "type": "keyword"
>   }

Destination mapping

> "tags": {
>    "type": "nested",
>         "properties": {
>           "value": {
>             "type": "text"
>           }
>       }
>    }

Here reindex script tested

> {
>   "source": {
>     "index": "original",
>     "_source": [
>       "tags"
>     ]
>   },
>   "dest": {
>     "index": "destination"
>   },
>   "script": {
>     "source": "def from = ctx._source.tags; if (from !== null) { List nested = new ArrayList(); for (tag in from) { nested.add(Collections.singletonMap('value', tag)); } ctx._source.tags = nested; }"
>   }
> }

this script seems to do the job, because it create a new list who contains each new entry with key and value
Something like this can reindex your data ?
I hope this can help someone

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