9

I am trying to add a custom analyzer.

curl -XPUT 'http://localhost:9200/my_index' -d '{
    "settings" : {
        "analysis" : {
            "filter" : {
                "my_filter" : {
                    "type" : "word_delimiter",
                    "type_table": [": => ALPHA", "/ => ALPHA"]
                }
            },
            "analyzer" : {
                "my_analyzer" : {
                    "type" : "custom",
                    "tokenizer" : "whitespace",
                    "filter" : ["lowercase", "my_filter"]
                }
            }
        }
    }
}'

It works on my local environment when I can recreate the index every time I want, the problem comes when I try to do the same on other environments like qa or prod, where the index has already been created.

{
    "error": "IndexAlreadyExistsException[[my_index] already exists]",
    "status": 400
}

How can I add my custom analyzer through the HTTP API?

Cris Pinto
  • 293
  • 3
  • 18

2 Answers2

8

In the documentation I found that to update index settings I can do this:

curl -XPUT 'localhost:9200/my_index/_settings' -d '
{
    "index" : {
        "number_of_replicas" : 4
    }
}'

And to update analyzer settings the documentation says:

"...it is required to close the index first and open it after the changes are made."

So I ended up doing this:

curl -XPOST 'http://localhost:9200/my_index/_close'

curl -XPUT 'http://localhost:9200/my_index' -d '{
    "settings" : {
        "analysis" : {
            "filter" : {
                "my_filter" : {
                    "type" : "word_delimiter",
                    "type_table": [": => ALPHA", "/ => ALPHA"]
                }
            },
            "analyzer" : {
                "my_analyzer" : {
                    "type" : "custom",
                    "tokenizer" : "whitespace",
                    "filter" : ["lowercase", "my_filter"]
                }
            }
        }
    }
}'

curl -XPOST 'http://localhost:9200/my_index/_open'

Which fixed everything for me.

Cris Pinto
  • 293
  • 3
  • 18
  • 2
    Besides the `_close` and `_open`, I found it works with `curl -XPUT 'http://localhost:9200/my_index/_settings` with the `_settings` endpoint and not directly `PUT` to `/my_index`. Adjust json level accordingly. – Sophia Feng Jul 20 '16 at 21:19
  • 1
    I have tried this solution but its giving error of 'index_already_exists_exception' – Mahesh Aug 30 '19 at 10:13
  • https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html – Mahesh Aug 30 '19 at 10:56
1

For folks using AWS Elastic-search service, closing and opening is not allowed, They need to follow re-indexing as mentioned here.

Basically create a temp index with all mappings of current original index and add/modify those mappings and settings(where analyzers sit), delete original index and create a new index with that name and copy back all mappings and settings from temp index.

  • I'm using AWS Elastic Search and both `close` and `open` index work as expected (I'm using the `nodejs` SDK and both the operations are confirmed with a `{ "acknowledge" : true }` response. Of course this still need to reindex the documents involved in the change! – loretoparisi Sep 25 '21 at 12:07