# Updating Elasticsearch Document Popularity by number of times it gets queried

**URL:** <https://discuss.elastic.co/t/updating-elasticsearch-document-popularity-by-number-of-times-it-gets-queried/99448>\
**Category:** Elasticsearch\
**Created:** [September 5, 2017, 2:25pm UTC](https://discuss.elastic.co/t/updating-elasticsearch-document-popularity-by-number-of-times-it-gets-queried/99448 "2017-09-05T14:25:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit2](https://discuss.elastic.co/u/ankit2)\
**Post date:** [September 5, 2017, 2:25pm UTC](https://discuss.elastic.co/t/updating-elasticsearch-document-popularity-by-number-of-times-it-gets-queried/99448/1 "2017-09-05T14:25:35Z")

</div>

I was wondering if elasticsearch provides any kind of functionality that lets you keep track & update the documents in indices based on the number of times any document gets queried directly using document\_id. Would that be something that would be a part of my query DSL (I am thinking a function to increase popularity count wrapped on my query everytime my API looks for document by document\_id, which would mean I would have to provide an extra field in my document/template)? Or would that be something I implement on the application layer/API layer?

If anyone have implemented something similar, insights would be appreciated.

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [September 7, 2017, 8:06pm UTC](https://discuss.elastic.co/t/updating-elasticsearch-document-popularity-by-number-of-times-it-gets-queried/99448/2 "2017-09-07T20:06:15Z")

</div>

Elasticsearch does not provides this out of the box and should be implemented in application layer.

There is, though, one feature in X-Pack Security that could help implementing this. The plugin has an auditing feature in which you can easily index an event whenever a document GET is issued and the event can go straight to an index. Here is a sample of an auditing event generated by `GET /test/test/1`:

```json
{
  "_index": ".security_audit_log-2017.09.07",
  "_type": "event",
  "_id": "AV5dxdgAhEw-lasSg8W0",
  "_score": 0,
  "_source": {
    "@timestamp": "2017-09-07T19:16:39.925Z",
    "node_name": null,
    "node_host_name": "127.0.0.1",
    "node_host_address": "127.0.0.1",
    "layer": "rest",
    "event_type": "authentication_success",
    "principal": "elastic",
    "realm": "reserved",
    "request_body": "",
    "origin_type": "rest",
    "origin_address": "127.0.0.1",
    "uri": "/test/test/1"
  }
}

```

As this is an indexed document, it becomes fairly easy to aggregate on it and find out top documents, for instance.

---

<div class="post-metadata">

**Author:** ![ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit2](https://discuss.elastic.co/u/ankit2)\
**Post date:** [September 11, 2017, 5:45pm UTC](https://discuss.elastic.co/t/updating-elasticsearch-document-popularity-by-number-of-times-it-gets-queried/99448/3 "2017-09-11T17:45:49Z")

</div>

thanks @thiago

if anyone runs into this with similar problem, I ended up using `_update` to search for my document instead of `_search`.

So `_update/document_id` with painless script to increment the view counter by one every time a user tries to view product detail via API, would return back the document with updated view. i can then perform sort or other query dsl on that property of documents in that index.

what i didnt know before i posted this was i could get back the updated document after i hit `_update` endpoint!

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [September 11, 2017, 9:44pm UTC](https://discuss.elastic.co/t/updating-elasticsearch-document-popularity-by-number-of-times-it-gets-queried/99448/4 "2017-09-11T21:44:51Z")

</div>

That is certainly another possibility, which is how the application layer handling it as I mentioned 😉

---

<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:** [October 9, 2017, 9:45pm UTC](https://discuss.elastic.co/t/updating-elasticsearch-document-popularity-by-number-of-times-it-gets-queried/99448/5 "2017-10-09T21:45:08Z")

</div>

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