Skip to content

Instantly share code, notes, and snippets.

@nicolashery
Last active December 30, 2023 19:03
Show Gist options
  • Save nicolashery/6317643 to your computer and use it in GitHub Desktop.
Save nicolashery/6317643 to your computer and use it in GitHub Desktop.
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
Copy link

yienge commented Mar 27, 2015

examples with explanations, good.

@mehdi-farsi
Copy link

Thanks ! 👍

@Alino
Copy link

Alino commented Jul 27, 2015

thank you

@ebaizel
Copy link

ebaizel commented Jul 29, 2015

perfect

@grizzlylab
Copy link

Clear and simple :)

@DavyLin
Copy link

DavyLin commented Aug 27, 2015

cool

@gauravarora
Copy link

Excellent examples, thanks a lot!

@balubollam
Copy link

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
Copy link

jdxlabs commented Apr 17, 2016

Very clear thanks !

@meenalluktuke
Copy link

Very nice document, clear instructions and examples !!

@chanlito
Copy link

good job

@divyenduz
Copy link

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
Copy link

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
Copy link

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

@MPurcell-EDR
Copy link

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
Copy link

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
Copy link

@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
Copy link

ometa commented Dec 11, 2017

Exactly what I was looking for; thanks.

@joeyhacker
Copy link

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.

@dennypenta
Copy link

I got in trouble with mapping.

  • I created mapping, ok.
  • I inserted document into the index, ok.
  • I looked at mapping, it has been changed.
    Do you have any idea what could go wrong?

@chairus
Copy link

chairus commented Aug 4, 2018

Clear explanation! Thanks!

@palbiplab
Copy link

@dennypenta, elasticsearch supports dynamic filed mapping and because of which inserting document creates/changes the mapping dynamically. If you want to disable this feature, please use dynamic=false or strict as part of the mapping.

@shubham-sharma16
Copy link

Hi, can you tell how we can check what were the configurations made in custom analyzer later because the mapping command shows the name of custom analyzer does not specify what is its configuration.

@SomberOfShadow
Copy link

I wonder whether we can put mapping by programming with JAVA API, not this way.
And I had a try according to ElasticSearch guide , but failed.

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-put-mapping.html

@hajimurtaza
Copy link

hajimurtaza commented Apr 7, 2020

Adding _type field to an existing index

I created an index without _type , can I update the index to add type to my document?
I am not talking about field types for properties, I am talking about index type

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