How to do it...

Since we are changing the state of the data, the HTTP method is POST and the REST URL is as follows:

http://<server>/<index_name>/_update/<id>
The REST format is changed by the previous version of Elasticsearch.

To update a document, we will perform the following steps:

  1. If we consider the type order of the previous recipe, the call to update a document will be as follows:
POST /myindex/_update/2qLrAfPVQvCRMe7Ku8r0Tw
{
"script": {
"source": "ctx._source.in_stock_items += params.count",
"params": {
"count": 4
}
}
}
  1. If the request is successful, the result returned by Elasticsearch should be as follows:
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "2qLrAfPVQvCRMe7Ku8r0Tw",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 1
}
  1. The record will be as follows:
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "2qLrAfPVQvCRMe7Ku8r0Tw",
"_version" : 8,
"_seq_no" : 12,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id" : "1234",
"date" : "2013-06-07T12:14:54",
"customer_id" : "customer1",
"sent" : true,
"in_stock_items" : 4,
... truncated ...
}
}

The visible changes are as follows:

  • The scripted field has been changed
  • The version has been incremented