How to update field inside nested document?


(Sreekumar) #1

I have a document of below structure.

{
   	"OrderId": "2500001101518267176",
	"CustID":"6576788",
    "OrderDate": "2018-02-04T11:00:25.35",
    "ShippingMethod": "standard",
    "CertCode":"",
    "Status":"1",
    "LineItem": [
					{
						"lineNumber":1,
						"LineItemId": "169673001611457008",
						"ProductId": "189944947919967210",
						"PO":"ZAZ100012001",
						"CONumber":"",
						"MWWITemCode":""
                    },
                    {
                        "lineNumber":2,
						"LineItemId": "169673001611457002",
						"ProductId": "189944947919967210",
						"PO":"ZAZ100012001",
						"CONumber":"",
						"MWWITemCode":""
                    }
            	]
}

Here I am looking to update the 'CONumber' for the first document in the nested 'LineItem'.

Is there any option to update the this nested item via Update API?


(Magnus Kessler) #2

Have a look at scripted updates in the Update API.

POST tmp_test/doc/1/_update
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.LineItem[0].CONumber = params.CONumber",
    "params": {
      "CONumber": "9876"
    }
  }
}

(Sreekumar) #3

Awesome! Thank you Magnus_Kessler.

I found a way to update with a where condition in the nested doc.

POST tmp_test/doc/1/_update
{
  
  "script": "for(i in ctx._source.LineItem){if(i.lineNumber==1){i.CONumber='123456'}}"

}

This is also working fine in my case.


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.