# Retrieve records and update a field at the same time

**URL:** <https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465>\
**Category:** Elasticsearch\
**Created:** [November 21, 2022, 2:38pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465 "2022-11-21T14:38:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [November 21, 2022, 2:38pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/1 "2022-11-21T14:38:45Z")

</div>

Is there anyway when making a query in Elasticsearch, to update a field (such as say a counter++) in each record that is selected by the query.  
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:** [November 21, 2022, 5:06pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/2 "2022-11-21T17:06:40Z")

</div>

You can use [the update by query API together with a script](https://www.elastic.co/guide/en/elasticsearch/reference/8.5/docs-update-by-query.html#docs-update-by-query-api-source), but be aware this can lead to conflict if you perform concurrent updates. Elasticsearch is IMHO not really optimised for a high update rate, so if you will be having lots of concurrent updates going on I would probably reconsider the design.

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [November 21, 2022, 11:17pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/3 "2022-11-21T23:17:09Z")

</div>

Hi  
Thanks for your response.  
How would we reconsider the design in order to achieve this?

We could store the query and re-execute the query with the update api.

Imagine there are 1000000 queries . We would have to do another 1000000 queries just to update the field. That is exactly what we are trying to avoid.  
Regards

---

<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:** [November 22, 2022, 6:08am UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/4 "2022-11-22T06:08:43Z")

</div>

I do not understand. What is the use case? What exactly are you trying to achieve? How frequently will you perform updates?

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [November 23, 2022, 3:52pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/5 "2022-11-23T15:52:42Z")

</div>

The frequency is about 1000 times a day (for us in any case).  
The use case is the following:  
Users on our site mkae a query to search for an item. These searches happen frequently, think about Etsy or Amazon people trying to find items.

For every search that is executed, a number of items will match that query which are returned to the user.

Now, what we need to do is every item that has satisfied the query and is returned, we would like to update a flag on that item showing how many times that item has satisfied the all users searches. i.e it has been included in the search results.

---

<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:** [November 23, 2022, 4:00pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/6 "2022-11-23T16:00:16Z")

</div>

You would need to perform that update through a bulk update from your application tier.

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [November 23, 2022, 4:25pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/7 "2022-11-23T16:25:58Z")

</div>

Thanks.  
Any examples anywhere.  
Would greatly appreciate it.

---

<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:** [November 23, 2022, 4:38pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/8 "2022-11-23T16:38:34Z")

</div>

I am not aware of any examples, so this is something you will need to build yourself using the language client of your choice.

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [November 23, 2022, 5:35pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/9 "2022-11-23T17:35:38Z")

</div>

It does not seem taht Elastic has a Bulk Update API - only Indexing and Deleting.  
Oh well, back to square one.  
Thanks anyway.

---

<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:** [December 21, 2022, 5:35pm UTC](https://discuss.elastic.co/t/retrieve-records-and-update-a-field-at-the-same-time/319465/10 "2022-12-21T17:35:54Z")

</div>

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