ElasticSearch Update Multi-field Mapping

4.2k Views Asked by At

I have an existing mapping for a field, and I want to change it to a multi-field.

The existing mapping is

{
   "my_index": {
      "mappings": {
         "my_type": {
            "properties": {
               "author": {
                  "type": "string"
               },
               "isbn": {
                  "type": "string",
                  "analyzer": "standard",
                  "fields": {
                     "ngram": {
                        "type": "string",
                        "search_analyzer": "keyword"
                     }
                  }
               },
               "title": {
                  "type": "string",
                  "analyzer": "english",
                  "fields": {
                     "std": {
                        "type": "string",
                        "analyzer": "standard"
                     }
                  }
               }
            }
         }
      }
   }
}

Based on the documentation, I should be able to change "author" to a multi-field by executing the following

PUT /my_index
{
  "mappings": {
        "my_type": {
            "properties": {
                "author": 
                { 
                    "type": "multi-field",
                    "fields": {
                        "ngram": {
                          "type": "string",
                          "indexanalyzer": "ngram_analyzer",
                          "search_analyzer": "keyword"
                        },
                         "name" : {
                         "type": "string"
                        }
                    }
                }              
            }
        }
    }
}

But instead I get the following error:

{
"error": "IndexAlreadyExistsException[[my_index] already exists]",
"status": 400
}

Am I missing something really obvious?

3

There are 3 best solutions below

1
jhilden On

Instead of PUT to /my_index do:

POST /my_index/_mapping
0
Prabin Meitei On

You won't be able to change the field type in an already existing index. If you can't recreate your index you can make use of the copy to field to achieve a similar capability.

   PUT /my_index
        {
          "mappings": {
           "my_type": {
        "properties": {
                   "author": 
                   { 
                      "type": "string",
                      "copy_to": ["author-name","author-ngram"]
                   }

                    "author-ngram": {
                      "type": "string",
                      "indexanalyzer": "ngram_analyzer",
                      "search_analyzer": "keyword"
                    },
                     "author-name" : {
                     "type": "string"
                    }

            }              
          }
       }
     }
  }
0
yoel halb On

While I have not tried it in your particular example, it is indeed possible to update field mappings by first closing the index and then applying the mappings.

Example:

POST /my_index/_close
POST /my_index/_mapping
{
     "my_field:{"new_mapping"}
}
POST /my_index/_open

I have tested it, by adding a "copy_to" mapping property to mapped field.

Based on https://gist.github.com/nicolashery/6317643.