# Intersection aggregation

**URL:** <https://discuss.elastic.co/t/intersection-aggregation/89963>\
**Category:** Elasticsearch\
**Created:** [June 19, 2017, 3:40pm UTC](https://discuss.elastic.co/t/intersection-aggregation/89963 "2017-06-19T15:40:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martingrayson](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@martingrayson](https://discuss.elastic.co/u/martingrayson)\
**Post date:** [June 19, 2017, 3:40pm UTC](https://discuss.elastic.co/t/intersection-aggregation/89963/1 "2017-06-19T15:40:19Z")

</div>

Hi,

I've been struggling to write a query to do the following, any help would be greatly appreciated.

My index contains one document per transaction. I'm looking to find:

- Number of customers that have purchased product A AND product B
- Number of customers that have purchased A OR purchased product B

The latter is easy, using a terms agg (below), but I cant figure out how to correctly identify an intersection. Is this possible?

```
GET trans/_search
{
  "size": 0,
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "product.ds_product_key": "88300"
          }
        },
        {
          "match": {
            "product.ds_product_key": "421345"
          }
        }
      ]
    }
  },
  "aggs": {
    "by_product": {
      "terms": {
        "field": "product.ds_product_key"
      },
      "aggs": {
        "total_customers": {
          "cardinality": {
            "field": "customer.identifiers.customer_key"
          }
        }
      }
    },
    "total_customers": {
          "cardinality": {
            "field": "customer.identifiers.customer_key"
          }
        }
  }
}

```

Thanks,  
Martin

---

<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:** [June 19, 2017, 4:18pm UTC](https://discuss.elastic.co/t/intersection-aggregation/89963/2 "2017-06-19T16:18:12Z")

</div>

There is an agg specifically designed for examining intersections - the [adjacency\_matrix](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/search-aggregations-bucket-adjacency-matrix-aggregation.html).

However, the usual rules apply about attempting user-behaviour analytics on an index storing only event logs. See the guidance on creating an [entity-centric-index](https://www.youtube.com/watch?v=yBf7oeJKH2Y) with each doc representing a customer. Then use the `adjacency_matrix` on a multi-valued field that lists each customer's purchases.

---

<div class="post-metadata">

**Author:** ![martingrayson](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@martingrayson](https://discuss.elastic.co/u/martingrayson)\
**Post date:** [June 19, 2017, 4:21pm UTC](https://discuss.elastic.co/t/intersection-aggregation/89963/3 "2017-06-19T16:21:56Z")

</div>

Thanks Mark, I had been looking at this type of aggregation. I'll dig a little deeper into this after I've created my new index.

---

<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:** [July 17, 2017, 4:21pm UTC](https://discuss.elastic.co/t/intersection-aggregation/89963/4 "2017-07-17T16:21:57Z")

</div>

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