# Aggregation and query bool must multiple on array fields "and"

**URL:** <https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948>\
**Category:** Elasticsearch\
**Created:** [September 19, 2016, 9:53pm UTC](https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948 "2016-09-19T21:53:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stepane](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Stepane](https://discuss.elastic.co/u/Stepane)\
**Post date:** [September 19, 2016, 9:53pm UTC](https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948/1 "2016-09-19T21:53:50Z")

</div>

Hi all,

I am looking for many hours the solution of a request aggregates.

I have this data : [http://pastebin.com/ZUWSDkEX](http://pastebin.com/ZUWSDkEX)

I want to find the number of person have the consent id 374 and 375 active (status = 1) but dont have the validate id 800 active (status = 1).

I have this query :

> **[Create a new version of paste: HHbY5mcq - Pastebin.com](https://pastebin.com/index/HHbY5mcq)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

But she select and exclude all consent with filter on status.

How i can count only the person have a list of consent id active, and not the other list (active) ?

Thank for you help.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 20, 2016, 10:46am UTC](https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948/2 "2016-09-20T10:46:49Z")

</div>

The easiest way would be to have different arrays for the active and inactive consents so that your current query would work.

Otherwise you will need to use `nested` objects and queries, which will work but will also make things slower and more complicated.

---

<div class="post-metadata">

**Author:** ![Stepane](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Stepane](https://discuss.elastic.co/u/Stepane)\
**Post date:** [September 20, 2016, 1:20pm UTC](https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948/3 "2016-09-20T13:20:20Z")

</div>

Hi Adrien,

Thank for your reply, but the mapping is "array" for the consents field, and i cant convert.

Another solution ?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 20, 2016, 2:43pm UTC](https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948/4 "2016-09-20T14:43:46Z")

</div>

There is no way to solve the problem without reindexing since Elasticsearch indexes data in a flat way by default: it has no way to figure which consent the 0s and the 1s map to.

Nested documents would allow you to keep the structure but you would still need to reindex in order to take into account different mappings. That said, I was advise against it since nested documents will make things complicated and slow.

---

<div class="post-metadata">

**Author:** ![Stepane](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Stepane](https://discuss.elastic.co/u/Stepane)\
**Post date:** [October 22, 2016, 11:05pm UTC](https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948/5 "2016-10-22T23:05:24Z")

</div>

Hi,

I have change my mapping, and i have now "nested" object for consents.

So, now, how i can select person have consent id 374 and 375 with status = 1 but dont have the consent id 800 with status = 1 ?

Thank you.

---

<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 5, 2017, 10:10pm UTC](https://discuss.elastic.co/t/aggregation-and-query-bool-must-multiple-on-array-fields-and/60948/6 "2017-07-05T22:10:21Z")

</div>


