# Counting documents that match both buckets at same time

**URL:** <https://discuss.elastic.co/t/counting-documents-that-match-both-buckets-at-same-time/106343>\
**Category:** Elasticsearch\
**Created:** [November 3, 2017, 4:28pm UTC](https://discuss.elastic.co/t/counting-documents-that-match-both-buckets-at-same-time/106343 "2017-11-03T16:28:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ergo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ergo/32/837_2.png) [@ergo](https://discuss.elastic.co/u/ergo)\
**Post date:** [November 3, 2017, 4:28pm UTC](https://discuss.elastic.co/t/counting-documents-that-match-both-buckets-at-same-time/106343/1 "2017-11-03T16:28:59Z")

</div>

> <https://stackoverflow.com/questions/47100127/elasticsearch-bucket-union>

I have this example list of documents:

```
curl -XPOST 'localhost:9200/test_data/test_row/_bulk?pretty' -H 'Content-Type: application/json' -d'
{ "index": {}}
{ "value": 1, "user_id": 1, "profiler_id": 1}
{ "index": {}}
{ "value": 3, "user_id": 1, "profiler_id": 1}
{ "index": {}}
{ "value": 3, "user_id": 1, "profiler_id": 2}
{ "index": {}}
{ "value": 1, "user_id": 2, "profiler_id": 1}
{ "index": {}}
{ "value": 1, "user_id": 2, "profiler_id": 2}
{ "index": {}}
{ "value": 1, "user_id": 3, "profiler_id": 1}
{ "index": {}}
{ "value": 2, "user_id": 3, "profiler_id": 2}
{ "index": {}}
{ "value": 3, "user_id": 3, "profiler_id": 2}
{ "index": {}}
{ "value": 2, "user_id": 4, "profiler_id": 1}
{ "index": {}}
{ "value": 1, "user_id": 4, "profiler_id": 2}
{ "index": {}}
{ "value": 3, "user_id": 4, "profiler_id": 2}
'

```

I want to count all users that answered profiler\_id=1 and value=1 AND profiler\_id=2 and value 1 or 2. This means that only users 2 and 3 match the criteria for both.

I know how to make a bucket aggregation on terms and count matching documents per profiler\_id but is there a way in Elasticsearch to count users matching BOTH criteria at once?

```
{
  "query": {
    "bool": {
      "must": [],
      "must_not": [],
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "profiler_id": 1
                }
              },
              {
                "term": {
                  "value": 1
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "profiler_id": 2
                }
              },
              {
                "terms": {
                  "value": [
                    1,
                    2
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "genres": {
      "terms": {
        "field": "profiler_id"
      },
      "aggs": {
        "type_count": {
          "cardinality": {
            "field": "user_id"
          }
        }
      }
    }
  }
}

```

Would give me the counts separately per `profiler_id` which is not what I want. I'm not sure how to count users that have documents intersecting in both buckets.

---

<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 1, 2017, 4:29pm UTC](https://discuss.elastic.co/t/counting-documents-that-match-both-buckets-at-same-time/106343/2 "2017-12-01T16:29:32Z")

</div>

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