# The Best Practice to Updates Lot of Documents

**URL:** <https://discuss.elastic.co/t/the-best-practice-to-updates-lot-of-documents/72143>\
**Category:** Elasticsearch\
**Created:** [January 19, 2017, 10:03am UTC](https://discuss.elastic.co/t/the-best-practice-to-updates-lot-of-documents/72143 "2017-01-19T10:03:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deerawan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deerawan/32/14676_2.png) [@deerawan](https://discuss.elastic.co/u/deerawan)\
**Post date:** [January 19, 2017, 10:03am UTC](https://discuss.elastic.co/t/the-best-practice-to-updates-lot-of-documents/72143/1 "2017-01-19T10:03:55Z")

</div>

Hi guys,

need suggestion and recommendation. I have documents with this body and `items` is nested object.

```json
{
  "subtotal": 1000,
  "multiplier": 2,
  "total": 2000,
   "items": [
      {
         "subtotal": 100,
         "multiplier": 2,
         "total": 200
      },
      {
         "subtotal": 500,
         "multiplier": 2,
         "total": 1000
      }
   ]
}

```

So, `total = multiplier * subtotal`. Multiplier is determined by User on my application. When multiplier is changed to another value, I have to update `multiplier` and `total` in all documents. I'm pondering how to achieve this with the best performance. I came up with strategies below:

1. Update\_By\_Query  
I don't have any idea how to update nested object fields using update\_by\_query. Is it also possible to update those fields in native script plugin (Java)? (in my real case I'm using native script)

2. Batch GET and Bulk UPDATE  
The last strategy that I'm thinking of. In my code, get all documents, looping each document to update the multiplier and total and send them back to elasticsearch. I'm worry about the performance especially when there are 100,000 documents.

Is there any other recommended strategy? What do you suggest?

Thank you 🙇‍♂️

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [January 20, 2017, 1:04pm UTC](https://discuss.elastic.co/t/the-best-practice-to-updates-lot-of-documents/72143/2 "2017-01-20T13:04:28Z")

</div>

Hi,

Update\_By\_Query with a groovy script (in 2.4. in my case) works well.  
Copy your script on all nodes, and have a code like:

```
UpdateByQueryRequestBuilder contactRequestBuilder = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
BulkIndexByScrollResponse contactResponse = contactRequestBuilder
        			.source(documentType.getIndexName())
        			.script(new Script("adddel_intlong_in_listfield", ScriptService.ScriptType.FILE, "groovy", scriptParams))
        			.filter(contactQuery).get();

```

bye,  
Xavier

---

<div class="post-metadata">

**Author:** ![deerawan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deerawan/32/14676_2.png) [@deerawan](https://discuss.elastic.co/u/deerawan)\
**Post date:** [January 23, 2017, 1:50am UTC](https://discuss.elastic.co/t/the-best-practice-to-updates-lot-of-documents/72143/3 "2017-01-23T01:50:21Z")

</div>

@xavierfacq  
Thanks for your answer

What did you do in the script? Did you add some columns there?

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [January 23, 2017, 9:11am UTC](https://discuss.elastic.co/t/the-best-practice-to-updates-lot-of-documents/72143/4 "2017-01-23T09:11:36Z")

</div>

Hi,

My script add or remove Long value from an array field, you can find it here:

> [@Script having issue with long data type](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795):
>
> Hello, I'm kind of new to elasticsearch. I had a question regarding using transport client ( v5.0.1 and my elasticsearch cluster is also at 5.0.1). I'm seeing this error when I try to call update operation. My document is of following format: { x: [\_array\_of\_longs] } I tried to use script to have an append only if it exists in the array kind of operation. I don't know if elasticsearch supports Set data structure. Whenever i try with small integer like 100, 200 it works. But when I enter l…

bye,  
Xavier

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 20, 2017, 9:11am UTC](https://discuss.elastic.co/t/the-best-practice-to-updates-lot-of-documents/72143/5 "2017-02-20T09:11:44Z")

</div>

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