Create Custom Analyzer after index has been created

3k views Asked by At

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?

2

There are 2 answers

3
Cris Pinto On BEST ANSWER

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.

1
Siba Prasad Tripathy On

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.