20

I would like the following settings and mapping set on an already existing index in Elasticsearch:

{
    "analysis": {
        "analyzer": {
            "dot-analyzer": {
                "type": "custom",
                "tokenizer": "dot-tokenizer"
            }
        },
        "tokenizer": {
            "dot-tokenizer": {
                "type": "path_hierarchy",
                "delimiter": "."
            }
        }
    }
}

{
    "doc": {
        "properties": {
            "location": {
                "type": "string",
                "index_analyzer": "dot-analyzer",
                "search_analyzer": "keyword"
            }
        }
    }
}

I have tried to add these two lines of code:

client.admin().indices().prepareUpdateSettings(Index).setSettings(settings).execute().actionGet();
client.admin().indices().preparePutMapping(Index).setType(Type).setSource(mapping).execute().actionGet();

But this is the result:

org.elasticsearch.index.mapper.MapperParsingException: Analyzer [dot-analyzer] not found for field [location]

Anyone? Thanks a lot,

Stine


This seems to work:

if (client.admin().indices().prepareExists(Index).execute().actionGet().exists()) {            
    client.admin().indices().prepareClose(Index).execute().actionGet();
    client.admin().indices().prepareUpdateSettings(Index).setSettings(settings.string()).execute().actionGet();
    client.admin().indices().prepareOpen(Index).execute().actionGet();
    client.admin().indices().prepareDeleteMapping(Index).setType(Type).execute().actionGet();
    client.admin().indices().preparePutMapping(Index).setType(Type).setSource(mapping).execute().actionGet();
} else {
    client.admin().indices().prepareCreate(Index).addMapping(Type, mapping).setSettings(settings).execute().actionGet();
}
Stine
  • 1,605
  • 5
  • 23
  • 44
  • Possible duplicate of [error when trying to update the settings](http://stackoverflow.com/questions/19758335/error-when-trying-to-update-the-settings) – RASG Feb 25 '16 at 18:24

1 Answers1

37

If you look at your settings after sending the changes you'll notice that the analyzer is not there. In fact you can't change the analysis section of the settings on a live index. Better to create it with the desired settings, otherwise you can just close it:

curl -XPOST localhost:9200/index_name/_close

While the index is closed you can send the new settings. After that you can reopen the index:

curl -XPOST localhost:9200/index_name/_open

While the index is closed it doesn't use any cluster resource, but it is not readable nor writable. If you want to close and reopen the index using the Java API you can use the following code:

client.admin().indices().prepareClose(indexName).execute().actionGet();
//TODO update settings
client.admin().indices().prepareOpen(indexName).execute().actionGet();
javanna
  • 59,145
  • 14
  • 144
  • 125
  • 3
    Your suggestion didn't work for me - after closing an index, I get IndexAlreadyExistsException. ElasticSearch 1.3.4 – chester89 Jul 29 '15 at 13:23
  • @javanna when you re-open the index, what will happen? Will ES need to re-index everything because you have a new analyzer? – micah Jul 29 '17 at 00:39
  • @javanna I am using elasticsearch version 2.4 and I got error- **index already exist** even after closing the index. Any other suggestion you can give? – Ankit Seth Oct 30 '17 at 05:06