Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a field with a nested array in Elastic Search

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?

like image 647
user3407300 Avatar asked Jan 26 '17 12:01

user3407300


People also ask

Can we update data in elastic search?

The script can update, delete, or skip modifying the document. The update API also supports passing a partial document, which is merged into the existing document. To fully replace an existing document, use the index API.

How does nested query work in Elasticsearch?

Nested queryedit. Wraps another query to search nested fields. The nested query searches nested field objects as if they were indexed as separate documents. If an object matches the search, the nested query returns the root parent document.

What is dynamic mapping in Elasticsearch?

When Elasticsearch detects a new field in a document, it dynamically adds the field to the type mapping by default. The dynamic parameter controls this behavior. You can explicitly instruct Elasticsearch to dynamically create fields based on incoming documents by setting the dynamic parameter to true or runtime .

What is inner hits in Elasticsearch?

The inner hits feature can be used for this. This feature returns per search hit in the search response additional nested hits that caused a search hit to match in a different scope. Inner hits can be used by defining an inner_hits definition on a nested , has_child or has_parent query and filter.


1 Answers

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.

like image 133
mshabeeb Avatar answered Sep 22 '22 23:09

mshabeeb