# How update, update\_by\_query in ES really work?

**URL:** <https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727>\
**Category:** Elasticsearch\
**Created:** [September 6, 2022, 3:55am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727 "2022-09-06T03:55:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)\
**Post date:** [September 6, 2022, 3:55am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/1 "2022-09-06T03:55:12Z")

</div>

Hi everyone, i'm trying to optimize update speed on my ES and want to know deeper about it's update and update\_by\_query. So I have some question:

1. how update in elasticsearch really word. As I know when I update 1 doc, it need a time to refresh and then I can see the result. but before it refresh, if I send another update request to that doc, it will skip first update and start with later update request right?
2. I have read ES doc of update\_by\_query and know that after use search to query, it will bulk with docs from the query

```auto
While processing an update by query request, Elasticsearch performs multiple search requests 
sequentially to find all of the matching documents. A bulk update request is performed for each 
batch of matching documents. Any query or update failures cause the update by query request to
 fail and the failures are shown in the response. Any update requests that completed successfully 
still stick, they are not rolled back.

```

so if i have exactly docs index ( don't need to query to know) so what is the faster way bulk or update by query? [link](https://discuss.elastic.co/t/how-to-increase-elasticsearch-update-and-skip-high-load/313680) to my old question how to not let ES high load.

So combine 2 question, after i use first update\_by\_query request then immediately send second update\_by\_query request ( not have time to refresh doc) will it will word, and i should i use second update\_by\_query request instead of bulk?

Thanks

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [September 6, 2022, 3:59am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/2 "2022-09-06T03:59:52Z")

</div>

_If_ the second update makes it to the shard and is applied, it will still work yes.

However if you are talking about running multiple updates and worrying about the timing of refreshes, you might want to rethink your use of Elasticsearch for your problem as this approach seems seriously inefficient.

---

<div class="post-metadata">

**Author:** ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)\
**Post date:** [September 6, 2022, 4:04am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/3 "2022-09-06T04:04:31Z")

</div>

hi @warkolm, thanks for reply me.  
hmmm, so it will do request as FIFO right? in my case there are 2 request to a same doc/shard. if ES has response for first request, it will complete right? and can i immediately send 2nd request?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [September 6, 2022, 4:15am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/4 "2022-09-06T04:15:27Z")

</div>

Are you using `refresh` when you are making the request? [Update By Query API | Elasticsearch Guide [8.4] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.4/docs-update-by-query.html)

---

<div class="post-metadata">

**Author:** ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)\
**Post date:** [September 6, 2022, 4:16am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/5 "2022-09-06T04:16:58Z")

</div>

@warkolm , no sir. I just use default update\_by\_query, cause if I set refresh=true, I can be timeout, and will it make ES high load?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 6, 2022, 7:07am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/6 "2022-09-06T07:07:04Z")

</div>

When sending bulk update requests you specify the ids of the documents to be updated. This will always update the latest version of the document, even if it is not yet searchable as a refresh has not taken place.

When you use update by query a query is first run and the update then performed based on this. If any change has not been refreshed and made available y the time you start the query you will likely see a conflict.

---

<div class="post-metadata">

**Author:** ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)\
**Post date:** [September 6, 2022, 7:27am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/7 "2022-09-06T07:27:32Z")

</div>

so in conclusion, i need to refresh after first request right? cause if I set refresh = true, my javaclientAPI will throw an ERROR timeout, how can I handle it? . What about speed and performent between bulk and update\_by\_query? I though that bulk need to search element then update it, and update\_by\_query can search elements first then update them all, is it right? Thanks

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 6, 2022, 7:35am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/8 "2022-09-06T07:35:46Z")

</div>

They both need to retrieve the document and then update it so there is no difference there. Update by query has to run a query while the bulk request has direct access to the IDs, so that is probably where the difference lies. I would not expect any major performance difference between the approaches, but it is probably best if you test it.

---

<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:** [October 4, 2022, 7:36am UTC](https://discuss.elastic.co/t/how-update-update-by-query-in-es-really-work/313727/9 "2022-10-04T07:36:02Z")

</div>

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