# Facet and filteredQuery

**URL:** <https://discuss.elastic.co/t/facet-and-filteredquery/3893>\
**Category:** Elasticsearch\
**Created:** [February 7, 2011, 6:19am UTC](https://discuss.elastic.co/t/facet-and-filteredquery/3893 "2011-02-07T06:19:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [February 7, 2011, 6:19am UTC](https://discuss.elastic.co/t/facet-and-filteredquery/3893/1 "2011-02-07T06:19:21Z")

</div>

Hello,

I'm trying to integrate facets on filtered query. Seems like the facet  
ignores the query. Here is my code:

TermFilterBuilder tfb = FilterBuilders.termFilter( "cId", 1234);

XContentQueryBuilder matchAllFiltered =  
QueryBuilders.filteredQuery( QueryBuilders.matchAllQuery(), tfb );

SearchResponse response =  
searchRequestBuilder.setSearchType( SearchType.DEFAULT ).setQuery( matchAllFiltered ).addFacet( FacetBuilders.termsFacet( "facet1" ).field( "counter" ).size( 1000000 ).global( false ).order( ComparatorType.COUNT ) ).execute().actionGet();

The results are identical to the case where setQuery() is set with  
matchAllQuery(). After I added facetFilter( tfb ) to the facet the  
results were as expected (at least as I expected :-)). Can you explain  
please why the facet ignored the query although global set to false?

Thanks!

---

<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:** [February 7, 2011, 6:17pm UTC](https://discuss.elastic.co/t/facet-and-filteredquery/3893/2 "2011-02-07T18:17:49Z")

</div>

That should not be the case. Gist a test case and I can have a look.  
On Monday, February 7, 2011 at 8:19 AM, barak wrote:

> Hello,
> 
> I'm trying to integrate facets on filtered query. Seems like the facet  
> ignores the query. Here is my code:
> 
> TermFilterBuilder tfb = FilterBuilders.termFilter( "cId", 1234);
> 
> XContentQueryBuilder matchAllFiltered =  
> QueryBuilders.filteredQuery( QueryBuilders.matchAllQuery(), tfb );
> 
> SearchResponse response =  
> searchRequestBuilder.setSearchType( SearchType.DEFAULT ).setQuery( matchAllFiltered ).addFacet( FacetBuilders.termsFacet( "facet1" ).field( "counter" ).size( 1000000 ).global( false ).order( ComparatorType.COUNT ) ).execute().actionGet();
> 
> The results are identical to the case where setQuery() is set with  
> matchAllQuery(). After I added facetFilter( tfb ) to the facet the  
> results were as expected (at least as I expected :-)). Can you explain  
> please why the facet ignored the query although global set to false?
> 
> Thanks!

---

<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, 4:12am UTC](https://discuss.elastic.co/t/facet-and-filteredquery/3893/3 "2017-07-06T04:12:27Z")

</div>


