# Filter on query result

**URL:** <https://discuss.elastic.co/t/filter-on-query-result/12887>\
**Category:** Elasticsearch\
**Created:** [July 22, 2013, 9:21pm UTC](https://discuss.elastic.co/t/filter-on-query-result/12887 "2013-07-22T21:21:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oreno](https://avatars.discourse-cdn.com/v4/letter/o/b4bc9f/32.png) [@oreno](https://discuss.elastic.co/u/oreno)\
**Post date:** [July 22, 2013, 9:21pm UTC](https://discuss.elastic.co/t/filter-on-query-result/12887/1 "2013-07-22T21:21:26Z")

</div>

Hello, I'm trying to filter **out** results after executing a nested query, but for some reason it looks like it doesn't get filtered.

I'm using the first query to return me with documents that have at least one event in the date range  
that contains the product 1.  
Then, from the results of that query I want to filter out the documents that contains the product 2 in any of their event...  
I'm using the filter outside of the nested query because, if I understand correctly, if I add it to the nested term and there will be a single event that has only 1 (without 2) then its doc will not be filtered(it will validate the doc). So I'm using a simple filter on the result that will just filter all the docs that contain the string 2.

Is there something that I didn't consider? since in my case there's at least one doc that contains these two product Ids(in different event) and it doesn't get filtered. -\> I'm getting the same result count with or without the filter.

Any ideas anyone?

Thanks,

Oren

{  
"size" : 0,  
"query" : {  
"nested" : {  
"query" : {  
"bool" : {  
"must" : [ {  
"term" : {  
"events.products.product" : "1"  
}  
}, {  
"range" : {  
"events.event\_time" : {  
"from" : "2013-06-02",  
"to" : "2013-06-02",  
"include\_lower" : true,  
"include\_upper" : true  
}  
}  
} ]  
}  
},  
"path" : "events"  
}  
},  
"filter" : {  
"bool" : {  
"must\_not" : [ {  
"term" : {  
"events.products.product" : "2"  
}  
} ]  
}  
},  
"fields" : []  
}

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 23, 2013, 6:29am UTC](https://discuss.elastic.co/t/filter-on-query-result/12887/2 "2013-07-23T06:29:51Z")

</div>

Hey,

have you tried a nester filter inside of the filter as well (depends on  
your data model of course, which you did not show)?

--Alex

On Mon, Jul 22, 2013 at 11:21 PM, oreno [oreno@exelate.com](mailto:oreno@exelate.com) wrote:

> Hello, I'm trying to filter _out_ results after executing a nested query,  
> but  
> for some reason it looks like it doesn't get filtered.
> 
> I'm using the first query to return me with documents that have at least  
> one  
> event in the date range  
> that contains the product 1.  
> Then, from the results of that query I want to filter out the documents  
> that  
> contains the product 2 in any of their event...  
> I'm using the filter outside of the nested query because, if I understand  
> correctly, if I add it to the nested term and there will be a single event  
> that has only 1 (without 2) then its doc will not be filtered(it will  
> validate the doc). So I'm using a simple filter on the result that will  
> just  
> filter all the docs that contain the string 2.
> 
> Is there something that I didn't consider? since in my case there's at  
> least  
> one doc that contains these two product Ids(in different event) and it  
> doesn't get filtered. -\> I'm getting the same result count with or without  
> the filter.
> 
> Any ideas anyone?
> 
> Thanks,
> 
> Oren
> 
> {  
> "size" : 0,  
> "query" : {  
> "nested" : {  
> "query" : {  
> "bool" : {  
> "must" : [ {  
> "term" : {  
> "events.products.product" : "1"  
> }  
> }, {  
> "range" : {  
> "events.event\_time" : {  
> "from" : "2013-06-02",  
> "to" : "2013-06-02",  
> "include\_lower" : true,  
> "include\_upper" : true  
> }  
> }  
> } ]  
> }  
> },  
> "path" : "events"  
> }  
> },  
> "filter" : {  
> "bool" : {  
> "must\_not" : [ {  
> "term" : {  
> "events.products.product" : "2"  
> }  
> } ]  
> }  
> },  
> "fields" :   
> }
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/filter-on-query-result-tp4038474.html](http://elasticsearch-users.115913.n3.nabble.com/filter-on-query-result-tp4038474.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> 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).

--  
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:** ![oreno](https://avatars.discourse-cdn.com/v4/letter/o/b4bc9f/32.png) [@oreno](https://discuss.elastic.co/u/oreno)\
**Post date:** [July 23, 2013, 8:27am UTC](https://discuss.elastic.co/t/filter-on-query-result/12887/3 "2013-07-23T08:27:19Z")

</div>

Hi, I was able to get the result I needed by using the not filter, although I'm not sure about the performance there. Any thoughts?

{  
"size": 0,  
"query": {  
"nested": {  
"query": {  
"bool": {  
"must": [{  
"term": {  
"events.products.product": "1772178"  
}  
},  
{  
"range": {  
"events.event\_time": {  
"from": "2013-06-01",  
"to": "2013-06-03",  
"include\_lower": true,  
"include\_upper": true  
}  
}  
}]  
}  
},  
"path": "events"  
}  
},  
"filter": {  
**"not"** : {  
"query": {  
"nested": {  
"query": {  
"bool": {  
"should": {  
"term": {  
"events.products.product": "1772166"  
}  
},  
"must":  
{  
"range": {  
"events.event\_time": {  
"from": "2013-06-01",  
"to": "2013-06-03",  
"include\_lower": true,  
"include\_upper": true  
}  
}  
}  
}  
},  
"path": "events"  
}  
}  
}  
}  
}

---

<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:25am UTC](https://discuss.elastic.co/t/filter-on-query-result/12887/4 "2017-07-06T02:25:05Z")

</div>


