The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true
Asked Answered
G

2

6

My API version is 6.3 and I try to create index. The failure says "The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true" .Here is my code:

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

After I remove "_doc" as following, failure says "Failed to parse mapping [_doc]: No type specified for field [properties]". So what should I do now?

 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);
Golconda answered 8/7, 2020 at 20:47 Comment(2)
What is your Elasticsearch version? And what API are you talking about? C#?Sigmon
Its really long, would be great if you can upvote and accept my answerArtemis
A
14

_doc was a temporary type name which was kept for backward compatibility and to prepare for removing it, refer removal of type for more details on this.

Also refer schedule and how to handle this breaking change on your version

  1. Indices created in 6.x only allow a single-type per index. Any name can be used for the type, but there can be only one. The preferred type name is _doc, so that index APIs have the same path as they will have in 7.0: PUT {index}/_doc/{id} and POST {index}/_doc

Also please refer https://www.elastic.co/guide/en/elasticsearch/reference/7.x/removal-of-types.html#_migrating_multi_type_indices_to_single_type this section where its explained how to use it in 6.X.

I would suggest first try to create a index using REST API and see with the combination of include_type_name and index.mapping.single_type it works or not and accordingly add it in your code.

Artemis answered 9/7, 2020 at 1:9 Comment(0)
B
9

The reason was a mapping API query. I fixed as below.

# 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": {
Blend answered 15/3, 2022 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.