# Select and Update matching docs

**URL:** <https://discuss.elastic.co/t/select-and-update-matching-docs/180038>\
**Category:** Elasticsearch\
**Created:** [May 7, 2019, 5:24pm UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038 "2019-05-07T17:24:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [May 7, 2019, 5:24pm UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/1 "2019-05-07T17:24:32Z")

</div>

Hi.  
We are trying to do the following and any help would be appreciated.  
Say you make a search and 100,000 documents match.  
We would like to increment a counter in each document that matched. Then at the same time select the first page say the first 50.

Can this be done in one operation or may be a parallel scenario.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [May 8, 2019, 3:59am UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/2 "2019-05-08T03:59:15Z")

</div>

As I understand your problem you can solve it by doing 2 queries.

> base\_query={"query":{"whateverfilter"}}  
> query = base\_query + {"size": 50, "from": page} # here you add your limit as you want only 50 result on page 1  
> result = query.run() # you run your query that you'll display result on your first page 50 doc

keeping the same base query that you use for your search and apply it to update all your docs.

> **[Update By Query API | Elasticsearch Guide \[7.0\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-update-by-query.html)**

> update\_query={"script": {  
> "source": "ctx.\_source.match\_docs\_increment++",  
> "lang": "painless"  
> },  
> "query": {base\_query}  
> update\_query.run() # run the update

If you have few traffic on search this one can be ok but it will add load on your server as you'll update your documents each time you make a search.

Is it to set a weight on your document to sort on the most popular?  
If it's just for statistics you can dump the result of your request in a file and set a filebeat to send them in elastic in a different index or server... Depends on what you want to do with.

Hope it help.

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [May 8, 2019, 4:45am UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/3 "2019-05-08T04:45:21Z")

</div>

Many thanks for your response.  
This is in fact for statistics purposes.  
Running 2 queries will indeed be problematic.  
Any chance you could help us achieve it using the most efficient method. Of course we will pay for your consultancy service

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [May 8, 2019, 12:03pm UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/4 "2019-05-08T12:03:05Z")

</div>

I can help but can't provide you a service as consultant.

Which language, framework are you using?

My solution is pretty simple, just send the content to a log file it can be done in 2~3 lines of code, maybe less depend on your framework, then you can use filebeat to parse the logs and store them in elastic. I think this solution don't need deep technical skill as filebeat is really easy to use.

[https://www.elastic.co/guide/en/beats/filebeat/current/index.html](https://www.elastic.co/guide/en/beats/filebeat/current/index.html)

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [May 8, 2019, 12:30pm UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/5 "2019-05-08T12:30:40Z")

</div>

Hi,  
Thanks for you response.  
We are using the NEST library in an MVC Core application.

Unfortunately we have never used filebeat and we may need more time than a couple of lines of code for someone who has more experience. Is there any one you can recommend who assist us achieve this task?

Many thanks

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [May 10, 2019, 12:26am UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/6 "2019-05-10T00:26:30Z")

</div>

You can try on stackoverflow, there's certainly some people with .net and nest skill that can help.

All the best.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 11, 2019, 12:28am UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/7 "2019-05-11T00:28:55Z")

</div>

I can't think of a way in which you can efficiently do this in one operation\* (well, one request).

[Update by query API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html) would be the most logical way to increment a counter on 100,000 documents, however it does not return the ids of documents that were updated, so you wouldn't be able to collect the first 50 documents and return those.

I think two requests, a search query that returns the first 50 documents, and an update by query to increment counters executed at the same time would be the straightforward way to approach this.

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [May 11, 2019, 1:56am UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/8 "2019-05-11T01:56:59Z")

</div>

Hi  
Thanks for the advice.

Any chance of a very simple query using NEST so that we get it right.

Would greatly appreciate it.

Kindest Regards

---

<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:** [June 8, 2019, 1:59am UTC](https://discuss.elastic.co/t/select-and-update-matching-docs/180038/9 "2019-06-08T01:59:06Z")

</div>

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