Elasticsearch:无法解析映射[_doc]

时间:2019-12-16 08:17:02

标签: java elasticsearch

我正在尝试为索引创建动态模板映射,并且不断收到以下错误:

Invocation of init method failed; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [mappings : {dynamic_templates=[{articleNumber={mapping={type=text, fields={keyword={type=keyword}}}, match_mapping_type=*, match=articleNumber*}}]}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Root mapping definition has unsupported parameters: [mappings : {dynamic_templates=[{articleNumber={mapping={type=text, fields={keyword={type=keyword}}}, match_mapping_type=*, match=articleNumber*}}]}]]];

以下是我要创建映射的结构:

{
  "mappings": {
    "dynamic_templates": [
      {
        "articleNumber": {
          "match_mapping_type": "*",
          "match": "articleNumber*",
          "mapping": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    ]
  }
}

映射结构有误吗?还是一切正常,我应该看看解析映射对象的方式吗?

已更新

这是我用来创建映射的代码:

CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
CreateIndexResponse createIndexResponse = getClient().indices().create(createIndexRequest.mapping(getIndexMapping()), RequestOptions.DEFAULT);

private XContentBuilder getIndexMapping() throws IOException {
    String mappingObj = "{\"mappings\":{\"dynamic_templates\":[{\"articleNumber\":{\"match_mapping_type\":\"string\",\"match\":\"articleNumber*\",\"mapping\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\"}}}}}]}}";
    XContentBuilder b = XContentFactory.jsonBuilder().prettyPrint();
    try (XContentParser p = XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY, 
            null, mappingObj)) {
        b.copyCurrentStructure(p);
    }
    System.err.println(b.toString());

    return b;
}

1 个答案:

答案 0 :(得分:0)

在@dhamo评论后,解决方法是从以下位置切换

CreateIndexResponse createIndexResponse = getClient()。indices()。create(createIndexRequest。 映射 (getIndexMapping()), RequestOptions.DEFAULT);

CreateIndexResponse createIndexResponse = getClient()。indices()。create(createIndexRequest。 (getIndexMapping()), RequestOptions.DEFAULT);

CreateIndexRequests上的旧.mappings()方法现在仅声明索引定义的映射部分(使用.settings()方法单独定义设置)。因此,现在我们只有一个用于定义索引的json对象时,才使用.source()。