映射定义不能嵌套在类型 [_doc] 下,除非 include_type_name 设置为 true

Zhe*_*Guo 1 java rest-client elasticsearch resthighlevelclient

我的 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);
Run Code Online (Sandbox Code Playgroud)

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

 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);
Run Code Online (Sandbox Code Playgroud)

kuj*_*jiy 8

原因是映射 API 查询。我固定如下。

# ES6
PUT myindex
{
    "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 2
    },
    "mappings": {
        "_doc": {                 <--------- Delete this layer
            "properties": {
                "blahblah": {
 
# ES7
PUT myindex
{
    "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 2
    },
    "mappings": {
        "properties": {
            "blahblah": {
Run Code Online (Sandbox Code Playgroud)


Ami*_*wal 6

_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_nameindex.mapping.single_type它的工作原理或不并相应地在代码中添加。