# Can I filter with exact phrases?

**URL:** <https://discuss.elastic.co/t/can-i-filter-with-exact-phrases/18841>\
**Category:** Elasticsearch\
**Created:** [July 23, 2014, 8:52pm UTC](https://discuss.elastic.co/t/can-i-filter-with-exact-phrases/18841 "2014-07-23T20:52:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![IronMike](https://avatars.discourse-cdn.com/v4/letter/i/e9a140/32.png) [@IronMike](https://discuss.elastic.co/u/IronMike)\
**Post date:** [July 23, 2014, 8:52pm UTC](https://discuss.elastic.co/t/can-i-filter-with-exact-phrases/18841/1 "2014-07-23T20:52:42Z")

</div>

How can I exclude exact phrases with a filter?

Lets say I want to search for "heaven is blue" but exclude exact phrase  
"nature books". I understand I can use a bool query with must & must\_not,  
but is there a way to filter exact phrases like the example below, but  
instead of terms, I use match\_phrase?

"query": {  
"query\_string": {  
"query": "heaven is blue",  
"fields": ["content"],  
"default\_operator": "AND"  
}  
},  
"filter" : {  
"not" : {  
"terms": { "content": ["book","nature"] }  
}  
}

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [July 23, 2014, 9:08pm UTC](https://discuss.elastic.co/t/can-i-filter-with-exact-phrases/18841/2 "2014-07-23T21:08:15Z")

</div>

You can wrap any query with a query filter:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--  
Ivan

On Wed, Jul 23, 2014 at 1:52 PM, IronMike [sabdalla80@gmail.com](mailto:sabdalla80@gmail.com) wrote:

> How can I exclude exact phrases with a filter?
> 
> Lets say I want to search for "heaven is blue" but exclude exact phrase  
> "nature books". I understand I can use a bool query with must & must\_not,  
> but is there a way to filter exact phrases like the example below, but  
> instead of terms, I use match\_phrase?
> 
> "query": {  
> "query\_string": {  
> "query": "heaven is blue",  
> "fields": ["content"],  
> "default\_operator": "AND"  
> }  
> },  
> "filter" : {  
> "not" : {  
> "terms": { "content": ["book","nature"] }  
> }  
> }
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAEDKJnE5XNyZ%3DwdmczGiF%3Do\_kxbKuAhY6D9D5b2eNPzQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAEDKJnE5XNyZ%3DwdmczGiF%3Do_kxbKuAhY6D9D5b2eNPzQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![IronMike](https://avatars.discourse-cdn.com/v4/letter/i/e9a140/32.png) [@IronMike](https://discuss.elastic.co/u/IronMike)\
**Post date:** [July 23, 2014, 9:29pm UTC](https://discuss.elastic.co/t/can-i-filter-with-exact-phrases/18841/3 "2014-07-23T21:29:14Z")

</div>

Brilliant, I will give it a try!

On Wednesday, July 23, 2014 5:08:31 PM UTC-4, Ivan Brusic wrote:

> You can wrap any query with a query filter:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-filter.html)
> 
> --  
> Ivan
> 
> On Wed, Jul 23, 2014 at 1:52 PM, IronMike \<[sabda...@gmail.com](mailto:sabda...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > How can I exclude exact phrases with a filter?
> > 
> > Lets say I want to search for "heaven is blue" but exclude exact phrase  
> > "nature books". I understand I can use a bool query with must & must\_not,  
> > but is there a way to filter exact phrases like the example below, but  
> > instead of terms, I use match\_phrase?
> > 
> > "query": {  
> > "query\_string": {  
> > "query": "heaven is blue",  
> > "fields": ["content"],  
> > "default\_operator": "AND"  
> > }  
> > },  
> > "filter" : {  
> > "not" : {  
> > "terms": { "content": ["book","nature"] }  
> > }  
> > }
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/55ec0d35-27b1-447e-9136-d86695e789c0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fdf32a51-bcd5-43bc-987a-207c2032fcc5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fdf32a51-bcd5-43bc-987a-207c2032fcc5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:13am UTC](https://discuss.elastic.co/t/can-i-filter-with-exact-phrases/18841/4 "2017-07-06T01:13:45Z")

</div>


