25

I am trying to update a field in a document with an array. I want to add an array to the field "products". I tried this:

POST /index/type/1/_update
{
    "doc" :{
       "products": [
         {
           "name": "A",
           "count": 1
         },
         {
           "name": "B",
           "count": 2
         },
         {
           "name": "c",
           "count": 3
         }
       ]
    }
}

this is the error response I am getting when I try and run the code:

{
   "error": {
      "root_cause": [
         {
            "type": "mapper_parsing_exception",
            "reason": "failed to parse [products]"
         }
      ],
      "type": "mapper_parsing_exception",
      "reason": "failed to parse [products]",
      "caused_by": {
         "type": "illegal_state_exception",
         "reason": "Can't get text on a START_OBJECT at 1:2073"
      }
   },
   "status": 400
}

Anyone know what I am doing wrong?

1 Answer 1

40

The message "Can't get text on a START_OBJECT" means that Elasticsearch was expecting an entry of type "string" but you are trying to give an object as input.

If you check Kibana you will find that the field "products" exists there and is defined as a string. But since you are entering a list of dictionaries then the field "products" should have been defined from the beginning as an object (preferably with dynamic fields in it). An example would be (see full example at https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html )

    "products": { 
      "dynamic": true,
      "properties": {}
    }

However since you already have the index then you can't change the mapping so you would need to delete the index, do the mapping beforehand and then do the update.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.