创建具有映射和设置的索引时,找不到分析器异常

时间:2013-07-10 11:51:41

标签: java elasticsearch

您好我使用的是弹性搜索elasticsearch-0.90.2版本。我正在尝试使用jsonBuilder使用自定义分析器映射分析器,同时创建索引,它会抛出异常,就像找不到分析器一样。

设置: -

       XContentBuilder settings = XContentFactory.jsonBuilder()
            .startObject()
                 .startObject("settings")
                      .startObject("analysis")
                           .startObject("analyzer")                 
                                .startObject("search_analyzer")             
                                      .field("tokenizer", "ngram_specification")
                                      .field("type", "custom")
                                      .field("filter", new String[]{ "ngram_specification"})
                                .endObject()
                            .endObject()
                            .startObject("ngram_specification")
                                 .field("type", "nGram")
                                 .field("min_gram", "2")
                                 .field("max_gram", "5")
                            .endObject()    
                      .endObject()
                 .endObject()
            .endObject();

映射: -

  XContentBuilder mapping = XContentFactory.jsonBuilder()

      .startObject()
             .startObject("sample")
                 .startObject("properties")
                     .startObject("email").field("type", "string").field("index", "analyzed").field("analyzer", "search_analyzer")
                     .endObject()
                 .endObject()
            .endObject()
      .endObject();

像这样创建索引: -

        CreateIndexRequest indexRequest = new CreateIndexRequest(indexName);
        indexRequest.settings(settings);
        indexRequest.mapping("sample", mapping);
        CreateIndexResponse response = client.admin().indices().create(indexRequest).actionGet();

例外:

    17:00:52,767 ERROR [STDERR] org.elasticsearch.index.mapper.MapperParsingException: mapping [3]
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:289)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.cluster.service.InternalClusterService$2.run(InternalClusterService.java:229)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:95)
    17:00:52,767 ERROR [STDERR]     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    17:00:52,767 ERROR [STDERR]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    17:00:52,767 ERROR [STDERR]     at java.lang.Thread.run(Thread.java:662)
    17:00:52,767 ERROR [STDERR] Caused by: org.elasticsearch.index.mapper.MapperParsingException: Analyzer [search_analyzer] not found for field [email]
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.index.mapper.core.TypeParsers.parseField(TypeParsers.java:100)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.index.mapper.core.StringFieldMapper$TypeParser.parse(StringFieldMapper.java:151)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parseProperties(ObjectMapper.java:263)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parse(ObjectMapper.java:219)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:176)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:380)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.index.mapper.MapperService.merge(MapperService.java:190)
    17:00:52,767 ERROR [STDERR]     at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:286)]    

有人可以告诉这种方法的错误吗?

2 个答案:

答案 0 :(得分:5)

我找到了解决方法。在“设置”中,如果使用“索引”而不是“设置”,那么它将正常工作。

这是错误的:

XContentBuilder settings = XContentFactory.jsonBuilder()
    .startObject()
         .startObject("settings") // "settings"
              .startObject("analysis")

XContentBuilder settings = XContentFactory.jsonBuilder()
    .startObject()
         .startObject("index") // "index" instead of "settings"
              .startObject("analysis")

答案 1 :(得分:2)

您可以使用Get Settings API检查您的设置是否正确应用。

我认为由于设置配置中的错误,尚未创建分析器。可能缺少tokenizer条目。这是一个更正版本:

  XContentBuilder settings = XContentFactory.jsonBuilder()
        .startObject()
             .startObject("settings")
                  .startObject("analysis")
                       .startObject("analyzer")                 
                            .startObject("search_analyzer")             
                                  .field("tokenizer", "ngram_specification")
                                  .field("type", "custom")
                                  .field("filter", new String[]{ "ngram_specification"})
                            .endObject()
                        .endObject()
                        .startObject("tokenizer")
                             .startObject("ngram_specification")
                                  .field("type", "nGram")
                                  .field("min_gram", "2")
                                  .field("max_gram", "5")
                             .endObject()    
                        .endObject()    
                  .endObject()
             .endObject()
        .endObject();