# Match query with must\_not bool filter

**URL:** <https://discuss.elastic.co/t/match-query-with-must-not-bool-filter/12955>\
**Category:** Elasticsearch\
**Created:** [July 26, 2013, 2:41pm UTC](https://discuss.elastic.co/t/match-query-with-must-not-bool-filter/12955 "2013-07-26T14:41:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mli\_2](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mli\_2](https://discuss.elastic.co/u/mli_2)\
**Post date:** [July 26, 2013, 2:41pm UTC](https://discuss.elastic.co/t/match-query-with-must-not-bool-filter/12955/1 "2013-07-26T14:41:47Z")

</div>

I am trying to find documents that do not contain certain values in a  
field. This is the query I am constructing:

{  
"from" : 0,  
"size" : 20,  
"fliter" : {  
"bool" : {  
"must" : {  
"nested" : {  
"filter" : {  
"bool" : {  
"must\_not" : [ {  
"query" : {  
"match" : {  
"country.country\_code" : {  
"query" :"rus"  
}  
}  
}  
}, {  
"query" : {  
"match" : {  
"country.country\_code" : {  
"query" : "chn"  
}  
}  
}  
} ]  
}  
},  
"path": "targets.country"  
}  
}  
}  
}  
}

I was hoping that the above query will yield documents that don't contain  
country code "chn" AND "rus". But what got filtered out were documents that  
contain both "chn" and "rus". Documents that contain either "chn" or "rus"  
should return.

How should I rewrite the query to filter out documents that contains any of  
the two country code?

Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [July 26, 2013, 7:20pm UTC](https://discuss.elastic.co/t/match-query-with-must-not-bool-filter/12955/2 "2013-07-26T19:20:37Z")

</div>

You want documents that contain wither "chn" or "rus". Therefore, you need  
a logical OR.

Simple fix to your query is to change "must\_not" to "should". I think the  
minimum\_should\_match default will be 1, so I don't believe you need to  
specify it.

Hope this helps.

Brian

On Friday, July 26, 2013 10:41:47 AM UTC-4, mli wrote:

> I am trying to find documents that do not contain certain values in a  
> field. This is the query I am constructing:
> 
> {  
> "from" : 0,  
> "size" : 20,  
> "fliter" : {  
> "bool" : {  
> "must" : {  
> "nested" : {  
> "filter" : {  
> "bool" : {  
> "must\_not" : [ {  
> "query" : {  
> "match" : {  
> "country.country\_code" : {  
> "query" :"rus"  
> }  
> }  
> }  
> }, {  
> "query" : {  
> "match" : {  
> "country.country\_code" : {  
> "query" : "chn"  
> }  
> }  
> }  
> } ]  
> }  
> },  
> "path": "targets.country"  
> }  
> }  
> }  
> }  
> }
> 
> I was hoping that the above query will yield documents that don't contain  
> country code "chn" AND "rus". But what got filtered out were documents that  
> contain both "chn" and "rus". Documents that contain either "chn" or "rus"  
> should return.
> 
> How should I rewrite the query to filter out documents that contains any  
> of the two country code?
> 
> Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [July 26, 2013, 7:28pm UTC](https://discuss.elastic.co/t/match-query-with-must-not-bool-filter/12955/3 "2013-07-26T19:28:16Z")

</div>

Ah, not so simple.

Re: I was hoping that the above query will yield documents that don't

> contain country code "chn" AND "rus". But what got filtered out were  
> documents that contain both "chn" and "rus". Documents that contain either  
> "chn" or "rus" should return.

What you really want is the ES version of the following:

(country.country\_code : "chn" OR country.country\_code : "rus") AND NOT  
(country.country\_code : "chn" AND country.country\_code : "rus")

Which would return documents with one or the other but not both. Hope this  
helps more!

Brian

On Friday, July 26, 2013 3:20:37 PM UTC-4, InquiringMind wrote:

> You want documents that contain wither "chn" or "rus". Therefore, you need  
> a logical OR.
> 
> Simple fix to your query is to change "must\_not" to "should". I think the  
> minimum\_should\_match default will be 1, so I don't believe you need to  
> specify it.
> 
> Hope this helps.
> 
> Brian
> 
> On Friday, July 26, 2013 10:41:47 AM UTC-4, mli wrote:
> 
> > I am trying to find documents that do not contain certain values in a  
> > field. This is the query I am constructing:
> > 
> > {  
> > "from" : 0,  
> > "size" : 20,  
> > "fliter" : {  
> > "bool" : {  
> > "must" : {  
> > "nested" : {  
> > "filter" : {  
> > "bool" : {  
> > "must\_not" : [ {  
> > "query" : {  
> > "match" : {  
> > "country.country\_code" : {  
> > "query" :"rus"  
> > }  
> > }  
> > }  
> > }, {  
> > "query" : {  
> > "match" : {  
> > "country.country\_code" : {  
> > "query" : "chn"  
> > }  
> > }  
> > }  
> > } ]  
> > }  
> > },  
> > "path": "targets.country"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > I was hoping that the above query will yield documents that don't contain  
> > country code "chn" AND "rus". But what got filtered out were documents that  
> > contain both "chn" and "rus". Documents that contain either "chn" or "rus"  
> > should return.
> > 
> > How should I rewrite the query to filter out documents that contains any  
> > of the two country code?
> > 
> > Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:24am UTC](https://discuss.elastic.co/t/match-query-with-must-not-bool-filter/12955/4 "2017-07-06T02:24:12Z")

</div>

Closed on July 6, 2017, 2:24am UTC


