【问题标题】:The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true映射定义不能嵌套在类型 [_doc] 下,除非 include_type_name 设置为 true
【发布时间】:2020-10-29 08:55:07
【问题描述】:

我的 API 版本是 6.3,我尝试创建索引。失败说“映射定义不能嵌套在类型 [_doc] 下,除非 include_type_name 设置为 true”。这是我的代码:

CreateIndexRequest indexRequest = new CreateIndexRequest("user");

request.source("{\n" +
        "    \"settings\" : {\n" +
        "        \"number_of_shards\" : 1,\n" +
        "        \"number_of_replicas\" : 0\n" +
        "    },\n" +
        "    \"mappings\" : {\n" +
        "        \"_doc\" : {\n" +
        "            \"properties\" : {\n" +
        "                \"message\" : { \"type\" : \"text\" }\n" +
        "            }\n" +
        "        }\n" +
        "    },\n" +
        "    \"aliases\" : {\n" +
        "        \"twitter_alias\" : {}\n" +
        "    }\n" +
        "}", XContentType.JSON);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(indexRequest);

如下所示删除“_doc”后,失败提示“无法解析映射 [_doc]:未为字段 [properties] 指定类型”。那我现在该怎么办?

 request.source("{\n" +
            "    \"settings\" : {\n" +
            "        \"number_of_shards\" : 1,\n" +
            "        \"number_of_replicas\" : 0\n" +
            "    },\n" +
            "    \"mappings\" : {\n" +
            "       
            "            \"properties\" : {\n" +
            "                \"message\" : { \"type\" : \"text\" }\n" +
            "            }\n" +
            "     
            "    },\n" +
            "    \"aliases\" : {\n" +
            "        \"twitter_alias\" : {}\n" +
            "    }\n" +
            "}", XContentType.JSON);

【问题讨论】:

  • 您的 Elasticsearch 版本是多少?你在说什么 API? C#?
  • 已经有一段时间了,如果您能跟进并让我知道您是否有进一步的问题,那就太好了,如果您可以投票并接受我的回答,如果它对您有帮助,那就太好了。

标签: java elasticsearch rest-client resthighlevelclient


【解决方案1】:

_doc 是一个临时类型名称,保留它是为了向后兼容并准备删除它,请参阅removal of type 了解更多详细信息。

另请参阅schedule and how to handle this breaking change on your version

  1. 在 6.x 中创建的索引只允许每个索引使用一种类型。任何名字 可以用于类型,但只能有一个。首选 类型名称为 _doc,以便索引 API 与它们具有相同的路径 在 7.0 中会有:PUT {index}/_doc/{id} 和 POST {index}/_doc

另外请参考https://www.elastic.co/guide/en/elasticsearch/reference/7.x/removal-of-types.html#_migrating_multi_type_indices_to_single_type 这个部分,它解释了如何在 6.X 中使用它。

我建议首先尝试使用 REST API 创建一个索引,看看 include_type_name 和 index.mapping.single_type 的组合是否有效,然后将其添加到您的代码中。

【讨论】:

    猜你喜欢
    • 2019-09-15
    • 2019-12-11
    • 2018-06-15
    • 2016-08-03
    • 2017-01-28
    • 2020-01-06
    • 1970-01-01
    • 2011-10-06
    • 2014-03-18
    相关资源
    最近更新 更多