Nested type in Elasticsearch: "object mapping can't be changed from nested to non-nested" when indexing a document

Dirk picture Dirk · Apr 12, 2016 · Viewed 18.7k times · Source

I try to index some nested documents into an Elasticsearch (v2.3.1) mapping which looks as follows (based on this example from the documentation):

PUT /my_index
{
  "mappings": {
    "blogpost": {
      "properties": {
        "title": { "type": "string" },
        "comments": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string"  },
            "comment": { "type": "string"  }
          }
        }
      }
    }
  }
}

However, I do not understand what my JSON documents have to look like in order to fit into that mapping. I tried with

PUT /my_index/some_type/1
{
  "title": "some_title",
  "comments": {
    "name": "some_name",
    "comment": "some_comment"
  }
}

as well as with

PUT /my_index_some_type/1
{
  "title": "some_title",
  "comments": [
      {
        "name": "some_name",
        "comment": "some_comment"
      }
  ]
}

which both result in

{

    "error": 

{

    "root_cause": 

[

            {
                "type": "remote_transport_exception",
                "reason": "[Caiman][172.18.0.4:9300][indices:data/write/index[p]]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "object mapping [comments] can't be changed from nested to non-nested"
    },
    "status": ​400

}

Which is the correct format to index nested documents? Any working examples are much appreciated, most examples here at SO or on other pages concentrate on nested queries rather than how the documents have been indexed before.

Answer

Val picture Val · Apr 12, 2016

It seems you're really creating a document of type some_type and comments will default to a normal object (i.e. not nested), which is not allowed since you already have a nested object called comments in the blogpost mapping type in the same index.

Try this instead and it should work:

PUT /my_index/blogpost/1
{
  "title": "some_title",
  "comments": {
    "name": "some_name",
    "comment": "some_comment"
  }
}