# Aggregation with OR filter

**URL:** <https://discuss.elastic.co/t/aggregation-with-or-filter/252390>\
**Category:** Elasticsearch\
**Created:** [October 16, 2020, 7:23pm UTC](https://discuss.elastic.co/t/aggregation-with-or-filter/252390 "2020-10-16T19:23:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-t0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex-t0/32/77362_2.png) [@alex-t0](https://discuss.elastic.co/u/alex-t0)\
**Post date:** [October 16, 2020, 7:23pm UTC](https://discuss.elastic.co/t/aggregation-with-or-filter/252390/1 "2020-10-16T19:23:03Z")

</div>

I have an index of jBPM usertasks. Each usertask has `id`, `actualOwner` and `actualOwner.keyword`, `potentialOwners` and `potentialOwners.keyword`, and `processId` fields.

I need this aggregation: count of usertasks per process for some user. User task belongs to user if it's `actualOwner` or `potentialOwner` is that user. And in some statuses.

Seems that is very easy query, but I cannot write it. I try this:

```auto
POST jbpm-tasks/_search?size=1000
{
  "query": {
    "bool": {
      "should": [
        { 
          "term": {
            "actualOwner.keyword": {
              "value": "vasya"
            }
          }
        },
        {
          "term": {
            "potentialOwners.keyword": {
              "value": "vasya"
            }
          }
        }
      ],
      "must": [
        {
          "terms": {
            "status.keyword": [
              "Created",
              "Reserved",
              "InProgress",
              "Ready"
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "byProcesses": {
      "terms": { "field": "processId.keyword", "size": 1000000 } 
    }
  }
}

```

Query as I understand does not filter, it calculate score only. So, for this query really not matter user login, for `vasya` and. for example, `fedya` request returns same aggregation results (with different scores for tasks, but it is not interesting for now).

I not understand how write filter aggregation for multiple filter with `OR` and `AND` formula. In all examples always one field...

How to write this query properly?

---

<div class="post-metadata">

**Author:** ![alex-t0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex-t0/32/77362_2.png) [@alex-t0](https://discuss.elastic.co/u/alex-t0)\
**Post date:** [October 17, 2020, 8:14am UTC](https://discuss.elastic.co/t/aggregation-with-or-filter/252390/2 "2020-10-17T08:14:01Z")

</div>

> <https://stackoverflow.com/questions/64396947/aggregation-with-or-filter/64400221#64400221>

---

<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:** [November 14, 2020, 8:14am UTC](https://discuss.elastic.co/t/aggregation-with-or-filter/252390/3 "2020-11-14T08:14:03Z")

</div>

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