# Python script update by query elasticsearch doesn't work

**URL:** <https://discuss.elastic.co/t/python-script-update-by-query-elasticsearch-doesnt-work/199334>\
**Category:** Elasticsearch\
**Created:** [September 12, 2019, 10:40pm UTC](https://discuss.elastic.co/t/python-script-update-by-query-elasticsearch-doesnt-work/199334 "2019-09-12T22:40:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![EZprogramming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ezprogramming/32/57291_2.png) [@EZprogramming](https://discuss.elastic.co/u/EZprogramming)\
**Post date:** [September 12, 2019, 11:06pm UTC](https://discuss.elastic.co/t/python-script-update-by-query-elasticsearch-doesnt-work/199334/2 "2019-09-12T23:06:47Z")

</div>

**Solution:**

**Reference:**

> [@Elasticsearch delete\_by\_query 409 version conflict](https://discuss.elastic.co/t/elasticsearch-delete-by-query-409-version-conflict/174150/3):
>
> Hi @HenningAndersen, So \_delete\_by\_query basically searches for the documents to delete and then deletes them one by one. And a version conflict occurs if one or more of the documents gets update in between the time when the search was completed and the delete operation was started. But as I said, I had received a successful created/updated response for all the documents that have to deleted, before sending the \_delete\_by\_query request. And I am pretty sure that that none of the documents are …

I made this change to my python code and it worked:

I added `self.es.indices.refresh(index = i)`

```
if self.es.indices.exists(index = i):
            for q in queries:
                self.es.indices.refresh(index = i)
                self.es.update_by_query(index = i, body = q)

```

---

_[View the full topic](https://discuss.elastic.co/t/python-script-update-by-query-elasticsearch-doesnt-work/199334)._
