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?

user3407300
  • 419
  • 1
  • 5
  • 12

1 Answers1

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.

mshabeeb
  • 577
  • 2
  • 9
  • 25