# Filters are not considered while calculating facets

**URL:** <https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036>\
**Category:** Elasticsearch\
**Created:** [March 17, 2012, 10:46am UTC](https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036 "2012-03-17T10:46:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![canavar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canavar/32/2950_2.png) [@canavar](https://discuss.elastic.co/u/canavar)\
**Post date:** [March 17, 2012, 10:46am UTC](https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036/1 "2012-03-17T10:46:26Z")

</div>

Hi,

I am trying to create a date\_histogram facet. While calculating the values,  
ES considers only the results of the query but not the filters I provided.  
Is this the expected behavior. The reason for I am using filters is to be  
able to use scripts. Can I use scripts in queries? Below is the Java code.  
Thanks for any help.

```
                .prepareSearch("sessions")

                .setQuery(QueryBuilders.fieldQuery("username", username

```

))

```
                .setFilter(FilterBuilders

```

.andFilter(FilterBuilders.scriptFilter("doc['date'].date.monthOfYear ==  
month").addParam("month", month),

FilterBuilders.scriptFilter("doc['date'].date.year == year").addParam("year",  
year)))

```
                .setFacets(jsonBuilder().startObject().field("histo1"

```

).startObject().field("date\_histogram").startObject().field("field", "date")

```
                                   .field("interval", "day"

```

).endObject().endObject().endObject()).execute().actionGet();

---

<div class="post-metadata">

**Author:** ![egaumer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egaumer/32/2365_2.png) [@egaumer](https://discuss.elastic.co/u/egaumer)\
**Post date:** [March 17, 2012, 11:26am UTC](https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036/2 "2012-03-17T11:26:58Z")

</div>

You need to apply your filters to the query using a filteredQuery.  
Something more along the lines of...

```
    filteredQuery(
        fieldQuery("username", username),        
        andFilter(
            scriptFilter("doc['date'].date.monthOfYear == month")
                .addParam("month", month),
            scriptFilter("doc['date'].date.year == year")
                .addParam("year", year)
        )
    ) 

```

See:

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

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

On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:

> Hi,
> 
> I am trying to create a date\_histogram facet. While calculating the  
> values, ES considers only the results of the query but not the filters I  
> provided. Is this the expected behavior. The reason for I am using filters  
> is to be able to use scripts. Can I use scripts in queries? Below is the  
> Java code. Thanks for any help.
> 
> ```
> .prepareSearch("sessions")
> 
> .setQuery(QueryBuilders.fieldQuery("username", 
> 
> ```
> 
> username))
> 
> ```
> .setFilter(FilterBuilders
> 
> ```
> 
> .andFilter(FilterBuilders.scriptFilter("doc['date'].date.monthOfYear ==  
> month").addParam("month", month),
> 
> FilterBuilders.scriptFilter("doc['date'].date.year == year").addParam(  
> "year", year)))
> 
> ```
> .setFacets(jsonBuilder().startObject().field("histo1"
> 
> ```
> 
> ).startObject().field("date\_histogram").startObject().field("field",  
> "date")
> 
> ```
> .field("interval", "day"
> 
> ```
> 
> ).endObject().endObject().endObject()).execute().actionGet();

---

<div class="post-metadata">

**Author:** ![canavar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canavar/32/2950_2.png) [@canavar](https://discuss.elastic.co/u/canavar)\
**Post date:** [March 17, 2012, 11:33am UTC](https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036/3 "2012-03-17T11:33:18Z")

</div>

Thanks. I actually tried that but no results returned. I will try it again.  
17 Mar 2012 13:27 tarihinde "egaumer" [egaumer@gmail.com](mailto:egaumer@gmail.com) yazdı:

> You need to apply your filters to the query using a filteredQuery.  
> Something more along the lines of...
> 
> ```
> filteredQuery(
> fieldQuery("username", username),
> andFilter(
> scriptFilter("doc['date'].date.monthOfYear == month")
> .addParam("month", month),
> scriptFilter("doc['date'].date.year == year")
> .addParam("year", year)
> )
> )
> 
> ```
> 
> See:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/query-dsl.html)
> 
> On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:
> 
> > Hi,
> > 
> > I am trying to create a date\_histogram facet. While calculating the  
> > values, ES considers only the results of the query but not the filters I  
> > provided. Is this the expected behavior. The reason for I am using filters  
> > is to be able to use scripts. Can I use scripts in queries? Below is the  
> > Java code. Thanks for any help.
> > 
> > ```
> > .prepareSearch("sessions")
> > 
> > .setQuery(QueryBuilders.**fieldQuery("username",
> > 
> > ```
> > 
> > username))
> > 
> > ```
> > .setFilter(FilterBuilders
> > 
> > .andFilter(FilterBuilders.**
> > 
> > ```
> > 
> > scriptFilter("doc['date'].\*\*date.monthOfYear == month").addParam("month",  
> > month),
> > 
> > FilterBuilders.scriptFilter("\*\*doc['date'].date.year == year").addParam(  
> > "year", year)))
> > 
> > ```
> > .setFacets(jsonBuilder().**startObject().field(
> > 
> > ```
> > 
> > "histo1").\*\*startObject().field("date\_**histogram").startObject().**  
> > field("field", "date")
> > 
> > ```
> > .field("interval", "day"
> > 
> > ```
> > 
> > ).endObject().endObject()\*\*.endObject()).execute().\*\*actionGet();

---

<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:** [March 20, 2012, 9:48am UTC](https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036/4 "2012-03-20T09:48:08Z")

</div>

Adding a filter on the actual search request will not apply to facets  
(intentionally), more info here:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/filter.html).

On Sat, Mar 17, 2012 at 1:33 PM, canavar [fehmican.saglam@gmail.com](mailto:fehmican.saglam@gmail.com) wrote:

> Thanks. I actually tried that but no results returned. I will try it again.  
> 17 Mar 2012 13:27 tarihinde "egaumer" [egaumer@gmail.com](mailto:egaumer@gmail.com) yazdı:
> 
> You need to apply your filters to the query using a filteredQuery.
> 
> > Something more along the lines of...
> > 
> > ```
> > filteredQuery(
> > fieldQuery("username", username),
> > andFilter(
> > scriptFilter("doc['date'].date.monthOfYear == month")
> > .addParam("month", month),
> > scriptFilter("doc['date'].date.year == year")
> > .addParam("year", year)
> > )
> > )
> > 
> > ```
> > 
> > See:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/query-dsl.html)
> > 
> > On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:
> > 
> > > Hi,
> > > 
> > > I am trying to create a date\_histogram facet. While calculating the  
> > > values, ES considers only the results of the query but not the filters I  
> > > provided. Is this the expected behavior. The reason for I am using filters  
> > > is to be able to use scripts. Can I use scripts in queries? Below is the  
> > > Java code. Thanks for any help.
> > > 
> > > ```
> > > .prepareSearch("sessions")
> > > 
> > > .setQuery(QueryBuilders.**fieldQuery("username",
> > > 
> > > ```
> > > 
> > > username))
> > > 
> > > ```
> > > .setFilter(FilterBuilders
> > > 
> > > .andFilter(FilterBuilders.**
> > > 
> > > ```
> > > 
> > > scriptFilter("doc['date'].\*\*date.monthOfYear == month").addParam("month",  
> > > month),
> > > 
> > > FilterBuilders.scriptFilter("\*\*doc['date'].date.year == year").addParam(  
> > > "year", year)))
> > > 
> > > ```
> > > .setFacets(jsonBuilder().**startObject().field(
> > > 
> > > ```
> > > 
> > > "histo1").\*\*startObject().field("date\_**histogram").startObject().**  
> > > field("field", "date")
> > > 
> > > ```
> > > .field("interval", "day"
> > > 
> > > ```
> > > 
> > > ).endObject().endObject()\*\*.endObject()).execute().\*\*actionGet();

---

<div class="post-metadata">

**Author:** ![canavar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canavar/32/2950_2.png) [@canavar](https://discuss.elastic.co/u/canavar)\
**Post date:** [March 20, 2012, 1:22pm UTC](https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036/5 "2012-03-20T13:22:24Z")

</div>

Thanks egaumer and Shay. It just worked with a filteredQuery.  
20 Mar 2012 11:48 tarihinde "Shay Banon" [kimchy@gmail.com](mailto:kimchy@gmail.com) yazdı:

> Adding a filter on the actual search request will not apply to facets  
> (intentionally), more info here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/filter.html).
> 
> On Sat, Mar 17, 2012 at 1:33 PM, canavar [fehmican.saglam@gmail.com](mailto:fehmican.saglam@gmail.com)wrote:
> 
> > Thanks. I actually tried that but no results returned. I will try it  
> > again.  
> > 17 Mar 2012 13:27 tarihinde "egaumer" [egaumer@gmail.com](mailto:egaumer@gmail.com) yazdı:
> > 
> > You need to apply your filters to the query using a filteredQuery.
> > 
> > > Something more along the lines of...
> > > 
> > > ```
> > > filteredQuery(
> > > fieldQuery("username", username),
> > > andFilter(
> > > scriptFilter("doc['date'].date.monthOfYear == month")
> > > .addParam("month", month),
> > > scriptFilter("doc['date'].date.year == year")
> > > .addParam("year", year)
> > > )
> > > )
> > > 
> > > ```
> > > 
> > > See:
> > > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)
> > > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/query-dsl.html)
> > > 
> > > On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I am trying to create a date\_histogram facet. While calculating the  
> > > > values, ES considers only the results of the query but not the filters I  
> > > > provided. Is this the expected behavior. The reason for I am using filters  
> > > > is to be able to use scripts. Can I use scripts in queries? Below is the  
> > > > Java code. Thanks for any help.
> > > > 
> > > > ```
> > > > .prepareSearch("sessions")
> > > > 
> > > > .setQuery(QueryBuilders.**fieldQuery("username",
> > > > 
> > > > ```
> > > > 
> > > > username))
> > > > 
> > > > ```
> > > > .setFilter(FilterBuilders
> > > > 
> > > > .andFilter(FilterBuilders.**
> > > > 
> > > > ```
> > > > 
> > > > scriptFilter("doc['date'].\*\*date.monthOfYear == month").addParam(  
> > > > "month", month),
> > > > 
> > > > FilterBuilders.scriptFilter("\*\*doc['date'].date.year == year"  
> > > > ).addParam("year", year)))
> > > > 
> > > > ```
> > > > .setFacets(jsonBuilder().**startObject().field(
> > > > 
> > > > ```
> > > > 
> > > > "histo1").\*\*startObject().field("date\_**histogram").startObject().**  
> > > > field("field", "date")
> > > > 
> > > > ```
> > > > .field("interval", "day"
> > > > 
> > > > ```
> > > > 
> > > > ).endObject().endObject()\*\*.endObject()).execute().\*\*actionGet();

---

<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:35am UTC](https://discuss.elastic.co/t/filters-are-not-considered-while-calculating-facets/7036/6 "2017-07-06T03:35:27Z")

</div>


