Skip to content

Instantly share code, notes, and snippets.

@DavyLin
Forked from nicolashery/elasticsearch.md
Last active August 27, 2015 03:51
Show Gist options
  • Save DavyLin/04e5a5bc593a79ec51c9 to your computer and use it in GitHub Desktop.
Save DavyLin/04e5a5bc593a79ec51c9 to your computer and use it in GitHub Desktop.

Revisions

  1. @nicolashery nicolashery revised this gist Aug 29, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion elasticsearch.md
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ $ 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):

    ```bash
    $ curl -X PUT 'http://localhost:9200/thegame/weapons/_mapping' -d \
    $ curl -X PUT 'http://localhost:9200/thegame/weapons/_mapping?ignore_conflicts=true' -d \
    '{
    "weapons": {
    "properties": {
  2. @nicolashery nicolashery revised this gist Aug 29, 2013. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions elasticsearch.md
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,7 @@ $ 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):

    ```bash
    $ curl -X DELETE 'http://localhost:9200/thegame/weapons/_mapping'
    $ curl -X POST 'http://localhost:9200/thegame/weapons/_mapping' -d \
    $ curl -X PUT 'http://localhost:9200/thegame/weapons/_mapping' -d \
    '{
    "weapons": {
    "properties": {
    @@ -69,9 +68,7 @@ $ curl -X POST 'http://localhost:9200/thegame/weapons/_mapping' -d \
    }'
    ```

    **Note**: Instead of using `PUT`, I like to use `DELETE` and `POST` to avoid any "merge conflict" errors.

    You can do all of this at once if you delete then re-create your index, but you will **loose all stored documents**:
    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:

    ```bash
    $ curl -X DELETE 'http://localhost:9200/thegame'
  3. @nicolashery nicolashery created this gist Aug 23, 2013.
    112 changes: 112 additions & 0 deletions elasticsearch.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    # Elasticsearch: updating the mappings and settings of an existing index

    *Note: This was written using elasticsearch 0.9.*

    [Elasticsearch](http://www.elasticsearch.org/) will automatically create an index (with basic settings and mappings) for you if you post a first document:

    ```bash
    $ 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:

    ```bash
    $ 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:

    ```bash
    $ 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):

    ```bash
    $ curl -X DELETE 'http://localhost:9200/thegame/weapons/_mapping'
    $ curl -X POST 'http://localhost:9200/thegame/weapons/_mapping' -d \
    '{
    "weapons": {
    "properties": {
    "name": {
    "type": "string",
    "analyzer": "full_name"
    },
    "description": {
    "type": "string"
    },
    "category": {
    "type": "string"
    }
    }
    }
    }'
    ```

    **Note**: Instead of using `PUT`, I like to use `DELETE` and `POST` to avoid any "merge conflict" errors.

    You can do all of this at once if you delete then re-create your index, but you will **loose all stored documents**:

    ```bash
    $ 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"
    }
    }
    }
    }
    }'
    ```