# How to perform automatic update\_by\_query on an index?

**URL:** <https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306>\
**Category:** Elasticsearch\
**Created:** [September 5, 2019, 6:33pm UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306 "2019-09-05T18:33:31Z")\
**Posts on this page:** 7\
**Page:** 1

<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 5, 2019, 6:33pm UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306/1 "2019-09-05T18:33:31Z")

</div>

Here is an example:

```
POST /test-scholarship/_doc
{
  "student": "George",
  "grade": 98
}

```

I want to add a new field (scholarship) to the above doc.

```
POST /test-scholarship/_update_by_query
{
  "script": "ctx._source.scholarship = 'Yes'",
  "query": {
    "grade": {
      "row": {
        "gte": 95
      }
    }
  }
}

```

Then

`GET /test-scholarship/_search`  
returns:

```
"hits" : [
  {
    "_index" : "test-scholarship",
    "_type" : "_doc",
    "_id" : "ORawAm0BHjv4SKuqp9C8",
    "_score" : 1.0,
    "_source" : {
      "student" : "George",
      "grade" : 98,
      "scholarship" : "Yes"
    }
  }
]

```

**Question:**

- How can I automate this update\_by\_query task? I don't want to run this query every once in a while to update my documents with a new field, I want to run this query automatically on my data every day. Is it possible?

I appreciate any feedback or suggestions.

---

<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 5, 2019, 10:25pm UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306/2 "2019-09-05T22:25:23Z")

</div>

Bump

---

<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, 2019, 3:17am UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306/3 "2019-09-06T03:17:51Z")

</div>

You can not automate it but might be able to run the calculation on ingest through an ingest pipeline you associate with the index.

---

<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 6, 2019, 5:55pm UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306/4 "2019-09-06T17:55:04Z")

</div>

What is the name of the process called? Not quite sure what to search for.

---

<div class="post-metadata">

**Author:** ![Doc\_Kaos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doc_kaos/32/53671_2.png) [@Doc\_Kaos](https://discuss.elastic.co/u/Doc_Kaos)\
**Post date:** [September 6, 2019, 7:00pm UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306/5 "2019-09-06T19:00:37Z")

</div>

Start here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/pipeline.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/pipeline.html)

Then add processors

---

<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:** [October 4, 2019, 5:08am UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306/6 "2019-10-04T05:08:21Z")

</div>

Solution:

Update by query using Python Elasticsearch:

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

```

Add a cron to run the script on your machine automatically (locally) or put it in docker and run it in ECS task on a cloud service like AWS.

---

<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:** [November 1, 2019, 5:08am UTC](https://discuss.elastic.co/t/how-to-perform-automatic-update-by-query-on-an-index/198306/7 "2019-11-01T05:08:37Z")

</div>

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