# ElasticSearch aggregation for recommendation engine

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregation-for-recommendation-engine/256789>\
**Category:** Elasticsearch\
**Created:** [November 26, 2020, 2:55pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-for-recommendation-engine/256789 "2020-11-26T14:55:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![martsuga](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@martsuga](https://discuss.elastic.co/u/martsuga)\
**Post date:** [November 26, 2020, 2:55pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-for-recommendation-engine/256789/1 "2020-11-26T14:55:40Z")

</div>

I need some advice for aggregations.  
I am trying to build a recommendation engine based on existing ElasticSearch data but i am out of ideas how to build this query with aggregations.

The easiest way would be to use "significant\_terms" but all the examples i found assume a structure like this:

```auto
{
    "userId": 123
    "viewedVideos": [112233, 112244]
},
{
    "userId": 124
    "viewedVideos": [112233]
}

```

...but my current index structure looks like this:

```auto
{
    "userId": 123
    "viewedVideoId": 112233
},
{
    "userId": 123
    "viewedVideoId": 112244
},
{
    "userId": 124
    "viewedVideoId": 112233
}

```

What i want to achieve is that user 124 get a suggestion to view the video 112244 after he has watched 112233 because other user who viewed 112233 also viewed 112244.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [November 26, 2020, 3:52pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-for-recommendation-engine/256789/2 "2020-11-26T15:52:36Z")

</div>

You have a log-centric index and you need to make an entity-centric index with a document for each user which includes that array of viewings.

This will probably involve some degree of coding on your side.  
Generally the [transforms](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html) API is how we'd pivot log data to an entity-centric index. This particular transformation would require some scripting of the `scripted_metric` aggregation to gather the video IDs into an array.

Alternatively you can write custom code to do this transformation using the search and index or update APIs.

---

<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 24, 2020, 3:52pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-for-recommendation-engine/256789/3 "2020-12-24T15:52:47Z")

</div>

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