# Exclude some documents (and category filter combination) for some queries

**URL:** https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061
**Category:** Elasticsearch
**Created:** [June 12, 2014, 6:15am UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061 "2014-06-12T06:15:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)
#### Post date: [June 12, 2014, 6:15am UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061/1 "2014-06-12T06:15:18Z")

</div>

I would like to exclude some documents belonging to certain category from  
the results only for certain search queries. I have a ES client layer where  
i am thinking of implementing this logic as a "not" filter depending on the  
search query. Let me give an example.

sample index

designId: 100  
tags: ["dog", "cute"]  
caption : cute dog in the garden  
products : [ { productId: "200", category: 1}, {productId: "201", category:  
2} ]

designId: 101  
tags: ["brown", "dog"]  
caption : little brown dog  
products : [{productId: "202", category: 3}]

designId: 102  
tags: ["black", "dog"]  
caption : little black dog  
products : [ { productId: "202", category: 4}, {productId: "203", category:  
5} ]

products is a nested field inside each design.

I would like to write a query to get all matches for "dog", (not for other  
keywords) but filter out few categories from the result. As ES returns the  
whole nested document even if only one nested document matches the query,  
my expected result is

designId: 100  
tags: ["dog", "cute"]  
caption : cute dog in the garden  
products : [ { productId: "200", category: 1}, {productId: "201", category:  
2} ]

designId: 102  
tags: ["black", "dog"]  
caption : little black dog  
products : [ { productId: "202", category: 4}, {productId: "203", category:  
5} ]  
Here is the query i tried but it doesn't work. Can anyone help me point out  
the mistake ?

GET /\_search/  
{  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"not": {  
"term": {  
"category": 1  
}  
}  
},  
{  
"not": {  
"term": {  
"category": 3  
}  
}  
}  
]

```
     },
     "query": {
        "multi_match": {
           "query": "dog",
           "fields": [
              "tags",
              "caption"
           ],
           "minimum_should_match": "50%"
        }
     }
  }

```

}  
}

--  
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/7293c299-16ad-420f-bcad-2e0d80681b17%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7293c299-16ad-420f-bcad-2e0d80681b17%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)
#### Post date: [June 12, 2014, 9:17pm UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061/2 "2014-06-12T21:17:15Z")

</div>

any thoughts anyone ?

On Wednesday, June 11, 2014 11:15:18 PM UTC-7, Srinivasan Ramaswamy wrote:

> I would like to exclude some documents belonging to certain category from  
> the results only for certain search queries. I have a ES client layer where  
> i am thinking of implementing this logic as a "not" filter depending on the  
> search query. Let me give an example.
> 
> sample index
> 
> designId: 100  
> tags: ["dog", "cute"]  
> caption : cute dog in the garden  
> products : [ { productId: "200", category: 1}, {productId: "201",  
> category: 2} ]
> 
> designId: 101  
> tags: ["brown", "dog"]  
> caption : little brown dog  
> products : [{productId: "202", category: 3}]
> 
> designId: 102  
> tags: ["black", "dog"]  
> caption : little black dog  
> products : [ { productId: "202", category: 4}, {productId: "203",  
> category: 5} ]
> 
> products is a nested field inside each design.
> 
> I would like to write a query to get all matches for "dog", (not for other  
> keywords) but filter out few categories from the result. As ES returns the  
> whole nested document even if only one nested document matches the query,  
> my expected result is
> 
> designId: 100  
> tags: ["dog", "cute"]  
> caption : cute dog in the garden  
> products : [ { productId: "200", category: 1}, {productId: "201",  
> category: 2} ]
> 
> designId: 102  
> tags: ["black", "dog"]  
> caption : little black dog  
> products : [ { productId: "202", category: 4}, {productId: "203",  
> category: 5} ]  
> Here is the query i tried but it doesn't work. Can anyone help me point  
> out the mistake ?
> 
> GET /\_search/  
> {  
> "query": {  
> "filtered": {  
> "filter": {  
> "and": [  
> {  
> "not": {  
> "term": {  
> "category": 1  
> }  
> }  
> },  
> {  
> "not": {  
> "term": {  
> "category": 3  
> }  
> }  
> }  
> ]
> 
> ```
> },
> "query": {
> "multi_match": {
> "query": "dog",
> "fields": [
> "tags",
> "caption"
> ],
> "minimum_should_match": "50%"
> }
> }
> }
> 
> ```
> 
> }  
> }

--  
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/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%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: [June 13, 2014, 5:58pm UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061/3 "2014-06-13T17:58:49Z")

</div>

Currently not possible. Elasticsearch will return all the nested documents  
as long as one of the nested documents satisfies the query.

> <https://github.com/elastic/elasticsearch/issues/3022>
>
> Add support for including the matching nested inner objects per hit element.

The issue is my personal #1 feature requested. Frustrating considering  
there has been a working implementation since version 0.90.5. 1.0, 1.1, 1.2  
and still nothing.

--  
Ivan

On Thu, Jun 12, 2014 at 2:17 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
wrote:

> any thoughts anyone ?
> 
> On Wednesday, June 11, 2014 11:15:18 PM UTC-7, Srinivasan Ramaswamy wrote:
> 
> > I would like to exclude some documents belonging to certain category from  
> > the results only for certain search queries. I have a ES client layer where  
> > i am thinking of implementing this logic as a "not" filter depending on the  
> > search query. Let me give an example.
> > 
> > sample index
> > 
> > designId: 100  
> > tags: ["dog", "cute"]  
> > caption : cute dog in the garden  
> > products : [ { productId: "200", category: 1}, {productId: "201",  
> > category: 2} ]
> > 
> > designId: 101  
> > tags: ["brown", "dog"]  
> > caption : little brown dog  
> > products : [{productId: "202", category: 3}]
> > 
> > designId: 102  
> > tags: ["black", "dog"]  
> > caption : little black dog  
> > products : [ { productId: "202", category: 4}, {productId: "203",  
> > category: 5} ]
> > 
> > products is a nested field inside each design.
> > 
> > I would like to write a query to get all matches for "dog", (not for  
> > other keywords) but filter out few categories from the result. As ES  
> > returns the whole nested document even if only one nested document matches  
> > the query, my expected result is
> > 
> > designId: 100  
> > tags: ["dog", "cute"]  
> > caption : cute dog in the garden  
> > products : [ { productId: "200", category: 1}, {productId: "201",  
> > category: 2} ]
> > 
> > designId: 102  
> > tags: ["black", "dog"]  
> > caption : little black dog  
> > products : [ { productId: "202", category: 4}, {productId: "203",  
> > category: 5} ]  
> > Here is the query i tried but it doesn't work. Can anyone help me point  
> > out the mistake ?
> > 
> > GET /\_search/  
> > {  
> > "query": {  
> > "filtered": {  
> > "filter": {  
> > "and": [  
> > {  
> > "not": {  
> > "term": {  
> > "category": 1  
> > }  
> > }  
> > },  
> > {  
> > "not": {  
> > "term": {  
> > "category": 3  
> > }  
> > }  
> > }  
> > ]
> > 
> > ```
> > },
> > "query": {
> > "multi_match": {
> > "query": "dog",
> > "fields": [
> > "tags",
> > "caption"
> > ],
> > "minimum_should_match": "50%"
> > }
> > }
> > }
> > 
> > ```
> > 
> > }  
> > }
> 
> --  
> 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/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com)  
> .
> 
> 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%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK\_FDUwQv%2BpQw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK_FDUwQv%2BpQw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)
#### Post date: [June 13, 2014, 6:52pm UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061/4 "2014-06-13T18:52:05Z")

</div>

Hi Ivan

Thanks for your reply. Yeah, I do understand that currently elasticsearch  
returns the whole nested doc.  
Can you help me how can i get the negative query with multiple categories  
working ?

Thanks  
Srini

On Fri, Jun 13, 2014 at 10:58 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Currently not possible. Elasticsearch will return all the nested documents  
> as long as one of the nested documents satisfies the query.
> 
> [https://github.com/elasticsearch/elasticsearch/issues/3022](https://github.com/elasticsearch/elasticsearch/issues/3022)
> 
> The issue is my personal #1 feature requested. Frustrating considering  
> there has been a working implementation since version 0.90.5. 1.0, 1.1, 1.2  
> and still nothing.
> 
> --  
> Ivan
> 
> On Thu, Jun 12, 2014 at 2:17 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
> wrote:
> 
> > any thoughts anyone ?
> > 
> > On Wednesday, June 11, 2014 11:15:18 PM UTC-7, Srinivasan Ramaswamy wrote:
> > 
> > > I would like to exclude some documents belonging to certain category  
> > > from the results only for certain search queries. I have a ES client layer  
> > > where i am thinking of implementing this logic as a "not" filter depending  
> > > on the search query. Let me give an example.
> > > 
> > > sample index
> > > 
> > > designId: 100  
> > > tags: ["dog", "cute"]  
> > > caption : cute dog in the garden  
> > > products : [ { productId: "200", category: 1}, {productId: "201",  
> > > category: 2} ]
> > > 
> > > designId: 101  
> > > tags: ["brown", "dog"]  
> > > caption : little brown dog  
> > > products : [{productId: "202", category: 3}]
> > > 
> > > designId: 102  
> > > tags: ["black", "dog"]  
> > > caption : little black dog  
> > > products : [ { productId: "202", category: 4}, {productId: "203",  
> > > category: 5} ]
> > > 
> > > products is a nested field inside each design.
> > > 
> > > I would like to write a query to get all matches for "dog", (not for  
> > > other keywords) but filter out few categories from the result. As ES  
> > > returns the whole nested document even if only one nested document matches  
> > > the query, my expected result is
> > > 
> > > designId: 100  
> > > tags: ["dog", "cute"]  
> > > caption : cute dog in the garden  
> > > products : [ { productId: "200", category: 1}, {productId: "201",  
> > > category: 2} ]
> > > 
> > > designId: 102  
> > > tags: ["black", "dog"]  
> > > caption : little black dog  
> > > products : [ { productId: "202", category: 4}, {productId: "203",  
> > > category: 5} ]  
> > > Here is the query i tried but it doesn't work. Can anyone help me  
> > > point out the mistake ?
> > > 
> > > GET /\_search/  
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "filter": {  
> > > "and": [  
> > > {  
> > > "not": {  
> > > "term": {  
> > > "category": 1  
> > > }  
> > > }  
> > > },  
> > > {  
> > > "not": {  
> > > "term": {  
> > > "category": 3  
> > > }  
> > > }  
> > > }  
> > > ]
> > > 
> > > ```
> > > },
> > > "query": {
> > > "multi_match": {
> > > "query": "dog",
> > > "fields": [
> > > "tags",
> > > "caption"
> > > ],
> > > "minimum_should_match": "50%"
> > > }
> > > }
> > > }
> > > 
> > > ```
> > > 
> > > }  
> > > }
> > 
> > --  
> > 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/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com)  
> > .
> > 
> > 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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe).  
> To unsubscribe from this group and all its topics, 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%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK\_FDUwQv%2BpQw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK_FDUwQv%2BpQw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK\_FDUwQv%2BpQw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK_FDUwQv%2BpQw%40mail.gmail.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/CAL1MvVyOvd\_YDi92z\_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVyOvd_YDi92z_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.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: [June 17, 2014, 11:46pm UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061/5 "2014-06-17T23:46:09Z")

</div>

I jumped the gun when I thought I realized the issue.

You listed your expected result, but not your actual result. Are you  
actually using nested documents? If so, you would need to use nested  
queries/filters:

> **[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.

Are you looking for a workaround for the issue referenced? You would either  
need to do the extra filtering on the client side or push all the nested  
values into the parent and query on that field.

--  
Ivan

On Fri, Jun 13, 2014 at 11:52 AM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
wrote:

> Hi Ivan
> 
> Thanks for your reply. Yeah, I do understand that currently elasticsearch  
> returns the whole nested doc.  
> Can you help me how can i get the negative query with multiple categories  
> working ?
> 
> Thanks  
> Srini
> 
> On Fri, Jun 13, 2014 at 10:58 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> 
> > Currently not possible. Elasticsearch will return all the nested  
> > documents as long as one of the nested documents satisfies the query.
> > 
> > [https://github.com/elasticsearch/elasticsearch/issues/3022](https://github.com/elasticsearch/elasticsearch/issues/3022)
> > 
> > The issue is my personal #1 feature requested. Frustrating considering  
> > there has been a working implementation since version 0.90.5. 1.0, 1.1, 1.2  
> > and still nothing.
> > 
> > --  
> > Ivan
> > 
> > On Thu, Jun 12, 2014 at 2:17 PM, Srinivasan Ramaswamy \<[ursvasan@gmail.com](mailto:ursvasan@gmail.com)
> > 
> > > wrote:
> > 
> > > any thoughts anyone ?
> > > 
> > > On Wednesday, June 11, 2014 11:15:18 PM UTC-7, Srinivasan Ramaswamy  
> > > wrote:
> > > 
> > > > I would like to exclude some documents belonging to certain category  
> > > > from the results only for certain search queries. I have a ES client layer  
> > > > where i am thinking of implementing this logic as a "not" filter depending  
> > > > on the search query. Let me give an example.
> > > > 
> > > > sample index
> > > > 
> > > > designId: 100  
> > > > tags: ["dog", "cute"]  
> > > > caption : cute dog in the garden  
> > > > products : [ { productId: "200", category: 1}, {productId: "201",  
> > > > category: 2} ]
> > > > 
> > > > designId: 101  
> > > > tags: ["brown", "dog"]  
> > > > caption : little brown dog  
> > > > products : [{productId: "202", category: 3}]
> > > > 
> > > > designId: 102  
> > > > tags: ["black", "dog"]  
> > > > caption : little black dog  
> > > > products : [ { productId: "202", category: 4}, {productId: "203",  
> > > > category: 5} ]
> > > > 
> > > > products is a nested field inside each design.
> > > > 
> > > > I would like to write a query to get all matches for "dog", (not for  
> > > > other keywords) but filter out few categories from the result. As ES  
> > > > returns the whole nested document even if only one nested document matches  
> > > > the query, my expected result is
> > > > 
> > > > designId: 100  
> > > > tags: ["dog", "cute"]  
> > > > caption : cute dog in the garden  
> > > > products : [ { productId: "200", category: 1}, {productId: "201",  
> > > > category: 2} ]
> > > > 
> > > > designId: 102  
> > > > tags: ["black", "dog"]  
> > > > caption : little black dog  
> > > > products : [ { productId: "202", category: 4}, {productId: "203",  
> > > > category: 5} ]  
> > > > Here is the query i tried but it doesn't work. Can anyone help me  
> > > > point out the mistake ?
> > > > 
> > > > GET /\_search/  
> > > > {  
> > > > "query": {  
> > > > "filtered": {  
> > > > "filter": {  
> > > > "and": [  
> > > > {  
> > > > "not": {  
> > > > "term": {  
> > > > "category": 1  
> > > > }  
> > > > }  
> > > > },  
> > > > {  
> > > > "not": {  
> > > > "term": {  
> > > > "category": 3  
> > > > }  
> > > > }  
> > > > }  
> > > > ]
> > > > 
> > > > ```
> > > > },
> > > > "query": {
> > > > "multi_match": {
> > > > "query": "dog",
> > > > "fields": [
> > > > "tags",
> > > > "caption"
> > > > ],
> > > > "minimum_should_match": "50%"
> > > > }
> > > > }
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > }  
> > > > }
> > > 
> > > --  
> > > 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/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com)  
> > > .
> > > 
> > > 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 a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe).  
> > To unsubscribe from this group and all its topics, 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%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK\_FDUwQv%2BpQw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK_FDUwQv%2BpQw%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK\_FDUwQv%2BpQw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK_FDUwQv%2BpQw%40mail.gmail.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/CAL1MvVyOvd\_YDi92z\_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVyOvd_YDi92z_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVyOvd\_YDi92z\_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVyOvd_YDi92z_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.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%3DcQA6t7Admm4foKB8C6f8rFjt-4wrb0TwTJ-ft3xbvZuZNg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA6t7Admm4foKB8C6f8rFjt-4wrb0TwTJ-ft3xbvZuZNg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)
#### Post date: [June 18, 2014, 1:23am UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061/6 "2014-06-18T01:23:52Z")

</div>

Yeah, I forgot to include my actual result. My "not" filter was not working  
at all. I got all the 3 designs back: 100, 101 and 102.

I followed the syntax in the link you sent and it worked 🙂 I tried similar  
syntax a few times before i posted the question, but i didn't have a  
"filter" clause inside the "nested" clause (basically two "filter"  
clauses). That made the trick !

Thanks a lot !  
Srini

On Tue, Jun 17, 2014 at 4:46 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> I jumped the gun when I thought I realized the issue.
> 
> You listed your expected result, but not your actual result. Are you  
> actually using nested documents? If so, you would need to use nested  
> queries/filters:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html)
> 
> Are you looking for a workaround for the issue referenced? You would  
> either need to do the extra filtering on the client side or push all the  
> nested values into the parent and query on that field.
> 
> --  
> Ivan
> 
> On Fri, Jun 13, 2014 at 11:52 AM, Srinivasan Ramaswamy \<[ursvasan@gmail.com](mailto:ursvasan@gmail.com)
> 
> > wrote:
> 
> > Hi Ivan
> > 
> > Thanks for your reply. Yeah, I do understand that currently elasticsearch  
> > returns the whole nested doc.  
> > Can you help me how can i get the negative query with multiple categories  
> > working ?
> > 
> > Thanks  
> > Srini
> > 
> > On Fri, Jun 13, 2014 at 10:58 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> > 
> > > Currently not possible. Elasticsearch will return all the nested  
> > > documents as long as one of the nested documents satisfies the query.
> > > 
> > > [https://github.com/elasticsearch/elasticsearch/issues/3022](https://github.com/elasticsearch/elasticsearch/issues/3022)
> > > 
> > > The issue is my personal #1 feature requested. Frustrating considering  
> > > there has been a working implementation since version 0.90.5. 1.0, 1.1, 1.2  
> > > and still nothing.
> > > 
> > > --  
> > > Ivan
> > > 
> > > On Thu, Jun 12, 2014 at 2:17 PM, Srinivasan Ramaswamy \<  
> > > [ursvasan@gmail.com](mailto:ursvasan@gmail.com)\> wrote:
> > > 
> > > > any thoughts anyone ?
> > > > 
> > > > On Wednesday, June 11, 2014 11:15:18 PM UTC-7, Srinivasan Ramaswamy  
> > > > wrote:
> > > > 
> > > > > I would like to exclude some documents belonging to certain category  
> > > > > from the results only for certain search queries. I have a ES client layer  
> > > > > where i am thinking of implementing this logic as a "not" filter depending  
> > > > > on the search query. Let me give an example.
> > > > > 
> > > > > sample index
> > > > > 
> > > > > designId: 100  
> > > > > tags: ["dog", "cute"]  
> > > > > caption : cute dog in the garden  
> > > > > products : [ { productId: "200", category: 1}, {productId: "201",  
> > > > > category: 2} ]
> > > > > 
> > > > > designId: 101  
> > > > > tags: ["brown", "dog"]  
> > > > > caption : little brown dog  
> > > > > products : [{productId: "202", category: 3}]
> > > > > 
> > > > > designId: 102  
> > > > > tags: ["black", "dog"]  
> > > > > caption : little black dog  
> > > > > products : [ { productId: "202", category: 4}, {productId: "203",  
> > > > > category: 5} ]
> > > > > 
> > > > > products is a nested field inside each design.
> > > > > 
> > > > > I would like to write a query to get all matches for "dog", (not for  
> > > > > other keywords) but filter out few categories from the result. As ES  
> > > > > returns the whole nested document even if only one nested document matches  
> > > > > the query, my expected result is
> > > > > 
> > > > > designId: 100  
> > > > > tags: ["dog", "cute"]  
> > > > > caption : cute dog in the garden  
> > > > > products : [ { productId: "200", category: 1}, {productId: "201",  
> > > > > category: 2} ]
> > > > > 
> > > > > designId: 102  
> > > > > tags: ["black", "dog"]  
> > > > > caption : little black dog  
> > > > > products : [ { productId: "202", category: 4}, {productId: "203",  
> > > > > category: 5} ]  
> > > > > Here is the query i tried but it doesn't work. Can anyone help me  
> > > > > point out the mistake ?
> > > > > 
> > > > > GET /\_search/  
> > > > > {  
> > > > > "query": {  
> > > > > "filtered": {  
> > > > > "filter": {  
> > > > > "and": [  
> > > > > {  
> > > > > "not": {  
> > > > > "term": {  
> > > > > "category": 1  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "not": {  
> > > > > "term": {  
> > > > > "category": 3  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > ]
> > > > > 
> > > > > ```
> > > > > },
> > > > > "query": {
> > > > > "multi_match": {
> > > > > "query": "dog",
> > > > > "fields": [
> > > > > "tags",
> > > > > "caption"
> > > > > ],
> > > > > "minimum_should_match": "50%"
> > > > > }
> > > > > }
> > > > > }
> > > > > 
> > > > > ```
> > > > > 
> > > > > }  
> > > > > }
> > > > 
> > > > --  
> > > > 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/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45fbf85d-4d29-4222-a72a-bf0a04d9a26d%40googlegroups.com)  
> > > > .
> > > > 
> > > > 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 a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe).  
> > > To unsubscribe from this group and all its topics, 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%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK\_FDUwQv%2BpQw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK_FDUwQv%2BpQw%40mail.gmail.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK\_FDUwQv%2BpQw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAfwARsZ7uGKkBf%2BH10jhrdw4dr5nxvHEK_FDUwQv%2BpQw%40mail.gmail.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/CAL1MvVyOvd\_YDi92z\_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVyOvd_YDi92z_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVyOvd\_YDi92z\_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVyOvd_YDi92z_fH9-OE6VJTcOP-Q4E-BvKj94FSkvEJOw%40mail.gmail.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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Fqt70gBtypQ/unsubscribe).  
> To unsubscribe from this group and all its topics, 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%3DcQA6t7Admm4foKB8C6f8rFjt-4wrb0TwTJ-ft3xbvZuZNg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA6t7Admm4foKB8C6f8rFjt-4wrb0TwTJ-ft3xbvZuZNg%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA6t7Admm4foKB8C6f8rFjt-4wrb0TwTJ-ft3xbvZuZNg%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQA6t7Admm4foKB8C6f8rFjt-4wrb0TwTJ-ft3xbvZuZNg%40mail.gmail.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/CAL1MvVxLCuiYz1UNtxdEGUZGN5hnLNS%2BM8LpGoXpc7YMt-TqYg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVxLCuiYz1UNtxdEGUZGN5hnLNS%2BM8LpGoXpc7YMt-TqYg%40mail.gmail.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:21am UTC](https://discuss.elastic.co/t/exclude-some-documents-and-category-filter-combination-for-some-queries/18061/7 "2017-07-06T01:21:37Z")

</div>


