Skip to content

Instantly share code, notes, and snippets.

@farhad-taran
Forked from nicolashery/elasticsearch.md
Created May 25, 2018 08:28
Show Gist options
  • Save farhad-taran/f55051cdbd0280cf1d3667ed4e6eb38f to your computer and use it in GitHub Desktop.
Save farhad-taran/f55051cdbd0280cf1d3667ed4e6eb38f 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"
        }
      }
    }
  }
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment