# Filtered query vs using filter outside?

**URL:** <https://discuss.elastic.co/t/filtered-query-vs-using-filter-outside/7555>\
**Category:** Elasticsearch\
**Created:** [May 3, 2012, 6:33pm UTC](https://discuss.elastic.co/t/filtered-query-vs-using-filter-outside/7555 "2012-05-03T18:33:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcoder](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bcoder](https://discuss.elastic.co/u/bcoder)\
**Post date:** [May 3, 2012, 6:33pm UTC](https://discuss.elastic.co/t/filtered-query-vs-using-filter-outside/7555/1 "2012-05-03T18:33:35Z")

</div>

What is the difference between these two:

Case 1:  
{  
"query": {  
"filtered": {  
"filter": {  
"prefix": {  
"name": "blah"  
}  
},  
"query": {  
"term": {  
"dept": "engineering"  
}  
}  
}  
}  
}

AND,  
Case 2:  
{  
"query": {  
"term": {  
"dept": "engineering"  
}  
},  
"filter": {  
"prefix": {  
"name": "blah"  
}  
}  
}

i..e, is there a difference if I put the filter and query together  
inside a "filtered" query, OR, if i put them separately like Case 2  
(i.e., a "query", and a "filter"). Are there any performance gains in  
either of the two ?

Note: In my case I also want to get facets on a particular field, and  
use exactly same filters (as my query) for facets as well (does that  
make either of the two cases more desirable). Currently I am doing  
something like this:

{  
"query": {  
"term": {  
"dept": "engineering"  
}  
},  
"filter": {  
"prefix": {  
"name": "blah"  
}  
}  
facets": {  
"type\_facet": {  
"terms": {  
"field": "type",  
"size": 100  
},  
"facet\_filter": {  
// Basically I repeat the exact same filter (at top  
level) here as well.  
"prefix": {  
"name": "blah"  
}  
}  
}  
}  
}

Is this the optimal way to do it ? Is there a better way then to  
repeat the filter at N+1 places in query for N facets, i.e., 1 for top-  
level query and N for facet\_filter for each facet. Will using  
'filtered" help me somehow in performance ?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 4, 2012, 1:37pm UTC](https://discuss.elastic.co/t/filtered-query-vs-using-filter-outside/7555/2 "2012-05-04T13:37:56Z")

</div>

Its explained here:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/filter.html). The  
idea of the search filter is the filter documents from the result, but not  
affect the facets. Facets run on the "query" element, further filtered with  
an optional facet\_filter.

In your case, if you place the filter in a filtered query within the  
"query" element, "everything" will be filtered by it, facets and hits.

On Thu, May 3, 2012 at 9:33 PM, bcoder [blitzkriegcoder@gmail.com](mailto:blitzkriegcoder@gmail.com) wrote:

> What is the difference between these two:
> 
> Case 1:  
> {  
> "query": {  
> "filtered": {  
> "filter": {  
> "prefix": {  
> "name": "blah"  
> }  
> },  
> "query": {  
> "term": {  
> "dept": "engineering"  
> }  
> }  
> }  
> }  
> }
> 
> AND,  
> Case 2:  
> {  
> "query": {  
> "term": {  
> "dept": "engineering"  
> }  
> },  
> "filter": {  
> "prefix": {  
> "name": "blah"  
> }  
> }  
> }
> 
> i..e, is there a difference if I put the filter and query together  
> inside a "filtered" query, OR, if i put them separately like Case 2  
> (i.e., a "query", and a "filter"). Are there any performance gains in  
> either of the two ?
> 
> Note: In my case I also want to get facets on a particular field, and  
> use exactly same filters (as my query) for facets as well (does that  
> make either of the two cases more desirable). Currently I am doing  
> something like this:
> 
> {  
> "query": {  
> "term": {  
> "dept": "engineering"  
> }  
> },  
> "filter": {  
> "prefix": {  
> "name": "blah"  
> }  
> }  
> facets": {  
> "type\_facet": {  
> "terms": {  
> "field": "type",  
> "size": 100  
> },  
> "facet\_filter": {  
> // Basically I repeat the exact same filter (at top  
> level) here as well.  
> "prefix": {  
> "name": "blah"  
> }  
> }  
> }  
> }  
> }
> 
> Is this the optimal way to do it ? Is there a better way then to  
> repeat the filter at N+1 places in query for N facets, i.e., 1 for top-  
> level query and N for facet\_filter for each facet. Will using  
> 'filtered" help me somehow in performance ?
> 
> Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bcoder](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bcoder](https://discuss.elastic.co/u/bcoder)\
**Post date:** [May 4, 2012, 6:13pm UTC](https://discuss.elastic.co/t/filtered-query-vs-using-filter-outside/7555/3 "2012-05-04T18:13:11Z")

</div>

Thanks a lot! 🙂

On May 4, 6:37 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Its explained here:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/filter.html). The  
> idea of the search filter is the filter documents from the result, but not  
> affect the facets. Facets run on the "query" element, further filtered with  
> an optional facet\_filter.
> 
> In your case, if you place the filter in a filtered query within the  
> "query" element, "everything" will be filtered by it, facets and hits.
> 
> On Thu, May 3, 2012 at 9:33 PM, bcoder [blitzkriegco...@gmail.com](mailto:blitzkriegco...@gmail.com) wrote:
> 
> > What is the difference between these two:
> 
> > Case 1:  
> > {  
> > "query": {  
> > "filtered": {  
> > "filter": {  
> > "prefix": {  
> > "name": "blah"  
> > }  
> > },  
> > "query": {  
> > "term": {  
> > "dept": "engineering"  
> > }  
> > }  
> > }  
> > }  
> > }
> 
> > AND,  
> > Case 2:  
> > {  
> > "query": {  
> > "term": {  
> > "dept": "engineering"  
> > }  
> > },  
> > "filter": {  
> > "prefix": {  
> > "name": "blah"  
> > }  
> > }  
> > }
> 
> > i..e, is there a difference if I put the filter and query together  
> > inside a "filtered" query, OR, if i put them separately like Case 2  
> > (i.e., a "query", and a "filter"). Are there any performance gains in  
> > either of the two ?
> 
> > Note: In my case I also want to get facets on a particular field, and  
> > use exactly same filters (as my query) for facets as well (does that  
> > make either of the two cases more desirable). Currently I am doing  
> > something like this:
> 
> > {  
> > "query": {  
> > "term": {  
> > "dept": "engineering"  
> > }  
> > },  
> > "filter": {  
> > "prefix": {  
> > "name": "blah"  
> > }  
> > }  
> > facets": {  
> > "type\_facet": {  
> > "terms": {  
> > "field": "type",  
> > "size": 100  
> > },  
> > "facet\_filter": {  
> > // Basically I repeat the exact same filter (at top  
> > level) here as well.  
> > "prefix": {  
> > "name": "blah"  
> > }  
> > }  
> > }  
> > }  
> > }
> 
> > Is this the optimal way to do it ? Is there a better way then to  
> > repeat the filter at N+1 places in query for N facets, i.e., 1 for top-  
> > level query and N for facet\_filter for each facet. Will using  
> > 'filtered" help me somehow in performance ?
> 
> > Thanks in advance!

---

<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, 3:30am UTC](https://discuss.elastic.co/t/filtered-query-vs-using-filter-outside/7555/4 "2017-07-06T03:30:02Z")

</div>


