Search code examples
elasticsearchelasticsearch-mapping

Cannot create mapping on elasticsearch


I want to create an index on elasticsearch and i want to create a mapping.

What i have understood about mapping is this is an optional step before inserting data to define fields types.

Index creation:

curl -X PUT "localhost:9200/idx5"

Mapping creation:

curl -X PUT "localhost:9200/idx5/_mapping" -H 'Content-Type: application/json' -d'
{
    "properties": {
        "element_type": {
            "type": "keyword"
        }
    }
}
'

Now, i get this error when trying to insert data in index

[type] => illegal_argument_exception
[reason] => Rejecting mapping update to [idx5] as the final mapping would have more than 1 type: [_doc, doc]

If i do not create mapping, i have no error.

Any idea ?

Thanks

** EDIT **

Version of Elastic search: 6.2.4

** EDIT **

Here is how i am trying to insert data:

curl -X POST http://localhost:9200/idx5/doc/ -H 'Content-Type: application/json' -d'{
         "id" : "1234",
         "element_type": "TYPE1",
          "title" : "test"
     }
     '

Solution

  • As you are using the Elasticsearch version 6.X, which doesn't support more than one type in a single index and by default, if you don't specify it creates _doc type, which as you didn't specify at index creation, it will use _doc type.

    Now while indexing instead of using _doc you are using doc which means Elasticsearch thinks you are using another type name for your index and as it's not supported Elastic throwing an error(even specifying the names of types in your index 😊, which are doc and _doc in this case).

    Just replace your the URL in your index doc request to http://localhost:9200/idx5/_doc/ and it should work.

    Please read the elastic doc for removal of type and what version has what changes. And specific to Elastic 6.X

    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