Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Elasticsearch: updating the mappings and settings of an existing index

Elasticsearch: updating the mappings and settings of an existing index

Note: This was written using elasticsearch 0.9.

Elasticsearch will automatically create an index (with basic settings and mappings) for you if you post a first document:

$ curl -X POST 'http://localhost:9200/thegame/weapons/1' -d \
'{
  "_id": 1,
  "name": "Longsword",
  "description": "The Longsword can be wielded in one or two hands and is also known as a Bastard Sword. It combines power, speed and reach to become one of the most well rounded weapons.",
  "category": "Sharp"
}'

You can see the settings (for the index) and mapping (for the weapons type) with:

$ curl -X GET 'http://localhost:9200/thegame/_settings'
$ curl -X GET 'http://localhost:9200/thegame/weapons/_mapping'

To update the settings, if you're defining new analyzers or filters, you first need to _close the index, then _open it when done updating:

$ curl -X POST 'http://localhost:9200/thegame/_close'

$ curl -X PUT 'http://localhost:9200/thegame/_settings' -d \
'{
  "analysis": {
    "analyzer": {
      "full_name": {
        "filter": [
          "standard",
          "lowercase",
          "asciifolding"
        ],
          "type": "custom",
          "tokenizer": "standard"
      }
    }
  }
}'

$ curl -X POST 'http://localhost:9200/thegame/_open'

To update the mappings of this existing index, you need to do it for each type (here we only have the weapons type):

$ curl -X PUT 'http://localhost:9200/thegame/weapons/_mapping?ignore_conflicts=true' -d \
'{
  "weapons": {
    "properties": {
      "name": {
        "type": "string",
        "analyzer": "full_name"
      },
      "description": {
        "type": "string"
      },
      "category": {
        "type": "string"
      }
    }
  }
}'

You can do all of this at once if you delete then re-create your index, but you will loose all stored documents, so you will have to reload them. But sometimes for big changes to mapping/settings, this makes more sense:

$ curl -X DELETE 'http://localhost:9200/thegame'
$ curl -X POST 'http://localhost:9200/thegame' -d \
'{
  "mappings": {
    "weapons": {
      "properties": {
        "name": {
          "type": "string",
          "analyzer": "full_name"
        },
        "description": {
          "type": "string"
        },
        "category": {
          "type": "string"
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "full_name": {
          "filter": [
            "standard",
            "lowercase",
            "asciifolding"
          ],
            "type": "custom",
            "tokenizer": "standard"
        }
      }
    }
  }
}'

yienge commented Mar 27, 2015

examples with explanations, good.

Thanks ! 👍

Alino commented Jul 27, 2015

thank you

ebaizel commented Jul 29, 2015

perfect

Clear and simple :)

DavyLin commented Aug 27, 2015

cool

Excellent examples, thanks a lot!

Hi
The above example is understandable.thanks ,But
Can u please help me how to check whether the analyzer is working or not.

I have created the above example.
And trying to check like this

GET /thegame/_analyze?analyzer=full_name&text=KTUFTD

so it is effecting on the data which is given in the text i ,,,,,
BUT when i index the data to the above index my custom analyzer is not effecting To that DATA.
As i am new to this concept,please anyone help me out..

Thanks

jdxlabs commented Apr 17, 2016

Very clear thanks !

Very nice document, clear instructions and examples !!

good job

I am not sure if "update mappings" part works anymore. I think that now the option is to delete and recreate an index. To do this without downtime, official docs: https://www.elastic.co/blog/changing-mapping-with-zero-downtime

Why it is not working on ES version 2.4.0
I tried this and got error :

{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Mapper for [name] conflicts with existing mapping in other types:\n[mapper [name] has different [analyzer]]"
}
],
"type": "illegal_argument_exception",
"reason": "Mapper for [name] conflicts with existing mapping in other types:\n[mapper [name] has different [analyzer]]"
},
"status": 400
}

I also got the same error. (I am using version 5.0.0)

MPurcell-EDR commented Feb 8, 2017

Although not listed, the fields with mapping conflicts are strings with the 'standard' analyzer on them. If you try to add an analyzer that conflicts with the present standard analyzer, you will get the above warning. I always recommend listing the standard analyzer on all string fields, so if you have to make an adjustment in the future you will see why you're having mappings conflicts.

I found exeception like "merge_mapping _exception" while changing the type String to date.
My command is:
curl -X PUT "http://{url}/{index}/json/_mapping?ignore_conflicts=true" -d "{"json": {"properties": {"Sent_Date": {"type": "date"}}}}"

@mpurcell
I think it's now not possible to update mapping on existing fields. Updating mapping is only possible for new fields.
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#updating-field-mappings

I added default standard analyzer for name field while creating the index
and then updated settings (adding full_name analyzer by closing index and then opening it)
and then tried updating mapping (changing the analyzer on name field from standard to full_name analyzer). This step triggers error with Mapper for [name] conflicts with existing mapping in other types.

ometa commented Dec 11, 2017

Exactly what I was looking for; thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment