spring-data-elasticsearch - registering custom analyser

Viewed 7972

I'm trying to use ElasticSearch in my application for full text search and at this time I'm trying use autocomplete analyser:

{
    "settings": {
        "number_of_shards": 1,
        "analysis": {
            "filter": {
                "autocomplete_filter": {
                    "type": "edge_ngram",
                    "min_gram": 1,
                    "max_gram": 20
                }
            },
            "analyzer": {
                "autocomplete": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "autocomplete_filter"
                    ]
                }
            }
        }
    }
}

As my application was constructed with Spring, I've decided use Spring-data-elasticsearch and mapped my entity this way:

@Document(indexName = "estabelecimento")
@Setting(settingPath = "/elasticsearch/autocomplete-analyser.json")
public class ESEstabelecimento {

    private Long id;
    @Field(type = FieldType.String, indexAnalyzer = "autocomplete")
    private String nome;
    private String razaoSocial;
    private String tipoEstabelecimento;
    @Field(type = FieldType.Object)
    private ESCidade cidade;
}

However, elasticsearch isn't loading the custom analyser:

[DEBUG] org.elasticsearch.action.admin.indices.mapping.put - [Magus] failed to put mappings on indices [[estabelecimento]], type [esestabelecimento] org.elasticsearch.index.mapper.MapperParsingException: Analyzer [autocomplete] not found for field [nome] at org.elasticsearch.index.mapper.core.TypeParsers.parseField(TypeParsers.java:220) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.index.mapper.core.StringFieldMapper$TypeParser.parse(StringFieldMapper.java:153) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parseProperties(ObjectMapper.java:290) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parseObjectOrDocumentTypeProperties(ObjectMapper.java:214) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.index.mapper.object.RootObjectMapper$TypeParser.parse(RootObjectMapper.java:136) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:211) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.index.mapper.DocumentMapperParser.parseCompressed(DocumentMapperParser.java:192) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:434) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.cluster.metadata.MetaDataMappingService$4.execute(MetaDataMappingService.java:505) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.cluster.service.InternalClusterService$UpdateTask.run(InternalClusterService.java:365) ~[elasticsearch-1.5.2.jar:na] at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:188) [elasticsearch-1.5.2.jar:na] at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:158) [elasticsearch-1.5.2.jar:na] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_77] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_77] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_77] [ERROR] org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository - failed to load elasticsearch nodes : org.elasticsearch.index.mapper.MapperParsingException: Analyzer [autocomplete] not found for field [nome]

I can't identify the cause of the problem, I don't know if the analyser is invalid or elasticsearch even found the autocomplete-analyser.json file. How can I solve this?

3 Answers

I had a similar issue and this can occur due to multiple reasons:

  1. Make sure that your json file located at the same location as mentioned in the settingPath and its being getting read properly. To verify this as Richa said in the above just remove the analyzer from the json and test the number of shards created.
  2. In case the location is correct but still json file is not read then just check did you add @Document(indexName = "indexName", createIndex = false) annotation in the model class then simply remove the createIndex = false from document annotation and then try again.
  3. Always remember to remove the old index and then test it again.
Related