# "Often buyed together" using aggregations?

**URL:** <https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106>\
**Category:** Elasticsearch\
**Created:** [November 25, 2017, 4:26pm UTC](https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106 "2017-11-25T16:26:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnolle](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@cnolle](https://discuss.elastic.co/u/cnolle)\
**Post date:** [November 25, 2017, 4:26pm UTC](https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106/1 "2017-11-25T16:26:40Z")

</div>

Hi everybody,

thank you for reading this post 🙂

I recently read an [aritlce](http://opensourceconnections.com/blog/2016/09/09/better-recsys-elasticsearch/) about using ElasticSearch as a Recommendation System. Now I'am trying to implement my own simple "Frequently buyed together" Recommender based on ElastisSearchs aggregations and an articleId as input.

A document in my dataset contains the following fields (I parsed a CSV file via logstash):

- articleId
- orderId
- orderId / articleId
- count
- revenue

So different than in the mentioned article my dataset is "flat". The documents in ElasticSearch are representing the purchase of a single article and not the whole "receipt". The connection to the "receipt" is made by the orderId.

I tried to use nested aggregations :

terms on orderId -\> terms on articleId -\> filter on a specific id ...

But now I'am kind of stuck. Do you think my dataset suits this task? Do you have any ideas how to help me?

Thank you very much!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 25, 2017, 4:29pm UTC](https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106/2 "2017-11-25T16:29:30Z")

</div>

Can you share some concrete examples, what you tried so far and the kind of result you are expecting?

Ideally provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![cnolle](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@cnolle](https://discuss.elastic.co/u/cnolle)\
**Post date:** [November 25, 2017, 5:19pm UTC](https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106/3 "2017-11-25T17:19:25Z")

</div>

Hi David,  
thank you for your quick reply. A purchase document in my ES index looks like this:

```
{
"orderId/articleId": "123/456",
"date": "2016-10-31T23:00:00.000Z",
"count": 1,
"orderId": "123",
"articleId": "456",
"revenue": 5
"currency": "EUR"
}

```

What I want in the end is a list of article numbers which are frequently ordered together with a certain article. So far I tried to identify orders which a containing the input article (12345):

```
{
    "aggs": {
        "baskets": {
            "aggs": {
                "articles": {
                    "aggs": {
                        "with_article": {
                            "filter": {
                                "term": {
                                    "articleId.keyword": "12345"
                                }
                            }
                        }
                    },
                    "terms": {
                        "field": "articleId.keyword"
                    }
                }
            },
            "terms": {
                "field": "orderId.keyword"
            }
        }
    }
}

```

Identifying the orders containing the article can only be a part of the solution and even with this query I still get all the orders not containing the article. So I'am not sure if I am on the right track.

---

<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 25, 2017, 9:06pm UTC](https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106/4 "2017-11-25T21:06:25Z")

</div>

You need to create order docs where purchased item IDs are held in an array on the order doc. Query for an item ID and use the 'significant\_terms' aggregation on the order itemIDs field to find strongly related other purchases.  
See [Graph explore api for webshop case](https://discuss.elastic.co/t/graph-explore-api-for-webshop-case/99582/7?u=mark_harwood)

---

<div class="post-metadata">

**Author:** ![cnolle](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@cnolle](https://discuss.elastic.co/u/cnolle)\
**Post date:** [December 2, 2017, 11:43pm UTC](https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106/5 "2017-12-02T23:43:15Z")

</div>

Thank you very much for your help! I got what I wanted using Logstashs 'Aggregation Filter'.

---

<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 30, 2017, 11:44pm UTC](https://discuss.elastic.co/t/often-buyed-together-using-aggregations/109106/6 "2017-12-30T23:44:59Z")

</div>

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