Search code examples
elasticsearch

Adding analyzer to existing index in elasticsearch 6.3.1


I am trying to add analyzer in existing index in elasticsearch.

Below is the code :-

curl -X POST "localhost:9200/existing_index_name/_mapping/_doc?pretty" -H 'Content-Type:     application/json' -d'
{
"settings":{
    "analysis":{
    "analyzer":{
    "analyzer_startswith":{
    "tokenizer":"keyword",
    "filter":["lowercase"]
     }
    }
   }      
  }
 }
'

Below is the error i am getting :-

 ["type" : "mapper_parsing_exception",
        "reason" : "Root mapping definition has unsupported parameters:  [settings : {analysis={analyzer={analyzer_startswith={tokenizer=keyword, filter=[lowercase]}}}}]"

Solution

  • You need to call the _settings endpoint not the _mapping one:

                                                    change this
                                                         |
                                                         v
    curl -X PUT "localhost:9200/existing_index_name/_settings?pretty" -H 'Content-Type: application/json' -d'{
      "analysis": {
        "analyzer": {
          "analyzer_startswith": {
            "tokenizer": "keyword",
            "filter": [
              "lowercase"
            ]
          }
        }
      }
    }
    

    Beware, though, that you need to first close the index:

    curl -XPOST http://localhost:9200/existing_index_name/_close
    

    And then after updating the settings, you need to open it again

    curl -XPOST http://localhost:9200/existing_index_name/_open