# Compound query with inversed (exclude) logic

**URL:** <https://discuss.elastic.co/t/compound-query-with-inversed-exclude-logic/74454>\
**Category:** Elasticsearch\
**Created:** [February 9, 2017, 1:31am UTC](https://discuss.elastic.co/t/compound-query-with-inversed-exclude-logic/74454 "2017-02-09T01:31:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Butenko](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Andrew\_Butenko](https://discuss.elastic.co/u/Andrew_Butenko)\
**Post date:** [February 9, 2017, 1:31am UTC](https://discuss.elastic.co/t/compound-query-with-inversed-exclude-logic/74454/1 "2017-02-09T01:31:12Z")

</div>

Hi,  
I use [Dot.Net](http://Dot.Net) Elastic Client v5 to build original query dynamically based on user choice.

For that reason I use QueryContainer with different types of query. It works fine for inclusion filter logic (condition1 + condition2+ etc.).

Before sending request to the ElasticSearch server I would like to apply inverse filter (if user clicked exclude in my UI) which should inverse all combined conditions in one exclude statement. As I understand, I have to use BoolQuery MustNot. so, my pseudo code looks like:

QueryContainer qc = new QueryContainer();  
///...populate all conditions  
///check exclude option is chousen by user and inverse it  
if (query.Inversed.HasValue && query.Inversed.Value == true)  
{  
qc = new BoolQuery()  
{  
MustNot = new QueryContainer[] { qc }  
};  
}  
///send request to ElasticSearch server...

For example, all query result has 100 hits, with all conditions it should return 10 items. But with exclude option it should be 90 items.  
Unfortunatly my query returns 0 hits with MustNot option,

Could you please advise how to implement inverse logic in my case?

---

<div class="post-metadata">

**Author:** ![Andrew\_Butenko](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Andrew\_Butenko](https://discuss.elastic.co/u/Andrew_Butenko)\
**Post date:** [February 14, 2017, 4:35am UTC](https://discuss.elastic.co/t/compound-query-with-inversed-exclude-logic/74454/2 "2017-02-14T04:35:04Z")

</div>

Seems like nobody knows how to implement inverse logic by one click.  
Any idea?

---

<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:** [March 14, 2017, 4:35am UTC](https://discuss.elastic.co/t/compound-query-with-inversed-exclude-logic/74454/3 "2017-03-14T04:35:34Z")

</div>

Closed on March 14, 2017, 4:35am UTC

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