# Two successive UpdateByQuery, How to refresh after first one

**URL:** <https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912>\
**Category:** Elasticsearch\
**Created:** [March 16, 2017, 5:11pm UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912 "2017-03-16T17:11:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amidani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amidani/32/16336_2.png) [@amidani](https://discuss.elastic.co/u/amidani)\
**Post date:** [March 16, 2017, 5:11pm UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/1 "2017-03-16T17:11:00Z")

</div>

Hi all,

I have two updatebyquery like this one.

```auto
UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
ubqrb.source(index).source().setTypes(itemType);
BulkIndexByScrollResponse response = ubqrb.setSlices(2).setRequestsPerSecond(100000F)
                        .setMaxRetries(1000).abortOnVersionConflict(false).script(actualScript)
                        .filter(query)
                        .refresh(true).get();

```

the second one's response contains version conflicts. if I wait few seconds between both, I got a coherent result without version conflict. Is there a better way to do this or a refresh that make the updates of the first query visible.

Thx for your help.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 16, 2017, 6:50pm UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/2 "2017-03-16T18:50:52Z")

</div>

`.refresh(true)` on the first one should make it visible to the second one.

A less intrusive option is to use `refresh(RefreshPolicy.WAIT_FOR)` but that isn't supported by update by query. I just never got around to supporting it, mostly because I wasn't really sure of the "right" way to do it.

BTW, `.setRequestsPerSecond(100000F)` is pretty much the same as not setting it at all.

---

<div class="post-metadata">

**Author:** ![amidani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amidani/32/16336_2.png) [@amidani](https://discuss.elastic.co/u/amidani)\
**Post date:** [March 17, 2017, 10:20am UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/3 "2017-03-17T10:20:52Z")

</div>

Hi Nik,  
Thx for your answer, the problem is even when I use `.refresh(true)` I can't find a way to tell the second query to wait for the first one to finish the refresh, in my case the first query is much heavier than the second one. I changed the partial update with script to update by query because I didn't find a way to solve this. So I really need the second query to wait for the refresh of the first one to finish in elasticsearch. I don't know if I can check the information "My Index has a refresh in progress" and wait while it's true before sending the second on, is it possible?

Thax a lot for the reply

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 17, 2017, 10:23am UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/4 "2017-03-17T10:23:41Z")

</div>

When you set `refresh(true)` elasticsearch will only response after the refresh is done.  
If the second query is in the same thread after the first one, it should work.

Are you doing that from 2 different threads?

---

<div class="post-metadata">

**Author:** ![amidani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amidani/32/16336_2.png) [@amidani](https://discuss.elastic.co/u/amidani)\
**Post date:** [March 17, 2017, 10:27am UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/5 "2017-03-17T10:27:43Z")

</div>

The same thread:

```
for(int i=0; i<scripts.length; i++) {
                    Script actualScript = new Script(ScriptType.INLINE, "painless", scripts[i], scriptParams[i]);

                    UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
                    ubqrb.source(index).source().setTypes(itemType);
                    BulkIndexByScrollResponse response = ubqrb.setSlices(2)
                            .setMaxRetries(1000).abortOnVersionConflict(false).script(actualScript)
                            .filter(query)
                            .refresh(true).get();
                    if(response.getBulkFailures().size() > 0) {
                        for(BulkItemResponse.Failure failure : response.getBulkFailures()) {
                            logger.error("Failure : cause={} , message={}",failure.getCause(), failure.getMessage());
                        }
                    }
                    if(response.isTimedOut()) {
                        logger.error("Update By Query ended with timeout!");
                    }
                    if(response.getVersionConflicts() > 0) {
                        logger.warn("Update By Query ended with {} Version Conflicts!", response.getVersionConflicts());
                    }
                    if(response.getNoops() > 0) {
                        logger.warn("Update By Query ended with {} noops!", response.getNoops());
                    }
                }

```

I got the warn for version conflicts each time the first request is longer than the second one. If I put a break point and wait a little bit I got no conflict

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 17, 2017, 10:38am UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/6 "2017-03-17T10:38:42Z")

</div>

Try to add at the beginning of your loop something like:

```auto
client.admin().indices().prepareRefresh(index).get();

```

And then remove `.refresh(true)`.

---

<div class="post-metadata">

**Author:** ![amidani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amidani/32/16336_2.png) [@amidani](https://discuss.elastic.co/u/amidani)\
**Post date:** [March 17, 2017, 10:56am UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/7 "2017-03-17T10:56:37Z")

</div>

Thanks a lot

---

<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:** [April 14, 2017, 10:56am UTC](https://discuss.elastic.co/t/two-successive-updatebyquery-how-to-refresh-after-first-one/78912/8 "2017-04-14T10:56:46Z")

</div>

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