# Boolean filter: A and ((C or D) and (X or Y))

**URL:** <https://discuss.elastic.co/t/boolean-filter-a-and-c-or-d-and-x-or-y/5644>\
**Category:** Elasticsearch\
**Created:** [October 19, 2011, 4:46pm UTC](https://discuss.elastic.co/t/boolean-filter-a-and-c-or-d-and-x-or-y/5644 "2011-10-19T16:46:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcdinh](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pcdinh](https://discuss.elastic.co/u/pcdinh)\
**Post date:** [October 19, 2011, 4:46pm UTC](https://discuss.elastic.co/t/boolean-filter-a-and-c-or-d-and-x-or-y/5644/1 "2011-10-19T16:46:09Z")

</div>

Hi all,

In ES, I can write a query that represents the boolean expression: A and (C  
or D)

"filter" : {

```
        "bool" : {

            "must" : {

                "term" : { "tag" : "A" }

            },

            "should" : [

                {

                    "term" : { "tag" : "C" }

                },

                {

                    "term" : { "tag" : "D" }

                }

            ]

        }

```

}

However how can I write a query that represents the expression: A and ((C or  
D) and (X or Y))? I plan to use a Python dictionary and convert it into a  
JSON object.

Thanks

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [October 19, 2011, 7:29pm UTC](https://discuss.elastic.co/t/boolean-filter-a-and-c-or-d-and-x-or-y/5644/2 "2011-10-19T19:29:13Z")

</div>

Hi,

the example that you have in your email does not in fact corresponds to A  
and (C or D). Both C and D are optional in this case and only A is  
mandatory. It translates to +A C D, where + denotes terms that must be found  
in the document, and C and D are only optional. It seems that documentation  
for boolean filter (  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html)) has  
similar example and can be little confusing. I think in your particular  
example the optional (should) clauses have no effect on the result data set  
since it will filter all documents having A clause present. If it were a  
boolean query and not filter then the effect of optional (should) clauses  
would be reflected in document relevancy. Check boolean query doc  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html) for  
further info.

Note that both boolean queries and boolean filters can contain nested  
queries (or filters) thus creating more complex queries is possible (think  
of using bool query instead of term queries in your particular example).

Regards,  
Lukas

On Wed, Oct 19, 2011 at 6:46 PM, pcdinh [pcdinh@gmail.com](mailto:pcdinh@gmail.com) wrote:

> Hi all,
> 
> In ES, I can write a query that represents the boolean expression: A and  
> (C or D)
> 
> "filter" : {
> 
> ```
> "bool" : {
> 
> "must" : {
> 
> "term" : { "tag" : "A" }
> 
> },
> 
> "should" : [
> 
> {
> 
> "term" : { "tag" : "C" }
> 
> },
> 
> {
> 
> "term" : { "tag" : "D" }
> 
> }
> 
> ]
> 
> }
> 
> ```
> 
> }
> 
> However how can I write a query that represents the expression: A and ((C  
> or D) and (X or Y))? I plan to use a Python dictionary and convert it into a  
> JSON object.
> 
> Thanks

---

<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 6, 2017, 3:51am UTC](https://discuss.elastic.co/t/boolean-filter-a-and-c-or-d-and-x-or-y/5644/3 "2017-07-06T03:51:24Z")

</div>


