首页 > 解决方案 > 映射定义不能嵌套在类型 [_doc] 下,除非 include_type_name 设置为 true

问题描述

我的 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);

标签: javaelasticsearchrest-clientresthighlevelclient

解决方案


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

另请参阅时间表以及如何处理您的版本上的这一重大更改

  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有效,然后将其添加到您的代码中。


推荐阅读