Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Custom Analyzer after index has been created

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?

like image 485
Cris Pinto Avatar asked Jun 10 '15 00:06

Cris Pinto


People also ask

How do I add an analyzer to an existing index Elasticsearch?

To add an analyzer, you must close the index, define the analyzer, and reopen the index. You cannot close the write index of a data stream. To update the analyzer for a data stream's write index and future backing indices, update the analyzer in the index template used by the stream.

What is difference between analyzer and tokenizer in Elasticsearch?

Elasticsearch analyzers and normalizers are used to convert text into tokens that can be searched. Analyzers use a tokenizer to produce one or more tokens per text field. Normalizers use only character filters and token filters to produce a single token.

What is the default analyzer in Elasticsearch?

By default, Elasticsearch uses the standard analyzer for all text analysis. The standard analyzer gives you out-of-the-box support for most natural languages and use cases. If you chose to use the standard analyzer as-is, no further configuration is needed.


1 Answers

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.

like image 185
Cris Pinto Avatar answered Oct 28 '22 04:10

Cris Pinto