Create a gist now

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Show comment
Hide comment
@yienge

yienge Mar 27, 2015

examples with explanations, good.

yienge commented Mar 27, 2015

examples with explanations, good.

@mehdi-farsi

This comment has been minimized.

Show comment
Hide comment
@mehdi-farsi

mehdi-farsi Apr 17, 2015

Thanks ! 👍

Thanks ! 👍

@Alino

This comment has been minimized.

Show comment
Hide comment
@Alino

Alino Jul 27, 2015

thank you

Alino commented Jul 27, 2015

thank you

@ebaizel

This comment has been minimized.

Show comment
Hide comment

ebaizel commented Jul 29, 2015

perfect

@grizzlylab

This comment has been minimized.

Show comment
Hide comment
@grizzlylab

grizzlylab Aug 11, 2015

Clear and simple :)

Clear and simple :)

@DavyLin

This comment has been minimized.

Show comment
Hide comment

DavyLin commented Aug 27, 2015

cool

@gauravarora

This comment has been minimized.

Show comment
Hide comment
@gauravarora

gauravarora Dec 3, 2015

Excellent examples, thanks a lot!

Excellent examples, thanks a lot!

@balubollam

This comment has been minimized.

Show comment
Hide comment
@balubollam

balubollam Jan 11, 2016

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

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

This comment has been minimized.

Show comment
Hide comment
@jdxlabs

jdxlabs Apr 17, 2016

Very clear thanks !

jdxlabs commented Apr 17, 2016

Very clear thanks !

@meenalluktuke

This comment has been minimized.

Show comment
Hide comment
@meenalluktuke

meenalluktuke Apr 26, 2016

Very nice document, clear instructions and examples !!

Very nice document, clear instructions and examples !!

@chanlito

This comment has been minimized.

Show comment
Hide comment

good job

@divyenduz

This comment has been minimized.

Show comment
Hide comment
@divyenduz

divyenduz Sep 26, 2016

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

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

@sachin032

This comment has been minimized.

Show comment
Hide comment
@sachin032

sachin032 Oct 28, 2016

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
}

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
}

@shd101wyy

This comment has been minimized.

Show comment
Hide comment
@shd101wyy

shd101wyy Nov 3, 2016

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

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

@MPurcell-EDR

This comment has been minimized.

Show comment
Hide comment
@MPurcell-EDR

MPurcell-EDR 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.

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.

@abhishek3112

This comment has been minimized.

Show comment
Hide comment
@abhishek3112

abhishek3112 Feb 24, 2017

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"}}}}"

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"}}}}"

@stephen-talari

This comment has been minimized.

Show comment
Hide comment
@stephen-talari

stephen-talari Feb 27, 2017

@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.

@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

This comment has been minimized.

Show comment
Hide comment
@ometa

ometa Dec 11, 2017

Exactly what I was looking for; thanks.

ometa commented Dec 11, 2017

Exactly what I was looking for; thanks.

@joeyhacker

This comment has been minimized.

Show comment
Hide comment
@joeyhacker

joeyhacker Mar 8, 2018

Updating existing mappings

Other than where documented, existing type and field mappings cannot be updated. Changing the mapping would mean invalidating already indexed documents. Instead, you should create a new index with the correct mappings and reindex your data into that index.

joeyhacker commented Mar 8, 2018

Updating existing mappings

Other than where documented, existing type and field mappings cannot be updated. Changing the mapping would mean invalidating already indexed documents. Instead, you should create a new index with the correct mappings and reindex your data into that index.

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