# FacetFilter for DateHistogramFacet

**URL:** <https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636>\
**Category:** Elasticsearch\
**Created:** [February 8, 2012, 2:56pm UTC](https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636 "2012-02-08T14:56:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thanuja\_gandhi](https://avatars.discourse-cdn.com/v4/letter/t/dc4da7/32.png) [@thanuja\_gandhi](https://discuss.elastic.co/u/thanuja_gandhi)\
**Post date:** [February 8, 2012, 2:56pm UTC](https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636/1 "2012-02-08T14:56:02Z")

</div>

Hi All,  
I am facing two problems in my code:  
1.  
I am using date histogram facet for implementing time chart. While  
applying multi-filter for the facet I am facing error like Failed to  
execute.  
My Code is working when I am using  
searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("1").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(queryFilter(termQuery("mediatypeid","1"))));

But if I place this code inside a for loop for changing mediatypeid  
value like  
String mediatypeArray[i]={1,2,3,4};  
for(int i=0; i\<mediatypeArray[i];i++)  
{  
searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("daterange"+mediatypeArray[i]).facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(queryFilter(termQuery("mediatypeid",mediatypeArray[i]))));  
}

The above mediatypeArray contains a set of mediatype ids

And also I cant able to apply filter for this facet.

1. I need a time line graph for number of posts for particular date  
I need a facet like

"date e.g(1328054400000)" : {  
"\_type" : "date\_histogram",  
"entries" : [ {  
"term" : 1,  
"count" : 4  
}]  
}

but I am getting like this,  
"print" : {  
"\_type" : "date\_histogram",  
"entries" : [ {  
"time" : 1328054400000,  
"count" : 4  
}, {  
"time" : 1328140800000,  
"count" : 1  
}, {  
"time" : 1328227200000,  
"count" : 1  
}, {  
"time" : 1328400000000,  
"count" : 1  
}, {  
"time" : 1328486400000,  
"count" : 4  
}, {  
"time" : 1328572800000,  
"count" : 3  
}, {  
"time" : 1328659200000,  
"count" : 1  
} ]  
}

Could you please help in this?.Hope I will resolve my problem soon.  
Thanks in advance,

Thanuja

---

<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 9, 2012, 8:08am UTC](https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636/2 "2012-02-09T08:08:35Z")

</div>

What is the failure that you are getting?

Also, in your code, you set the facet filter twice on the same date histogram, why? Note, make sure to use relevant filter, and not a query filter wrapping a query (i.e. use term filter, instead of a query filter wrapping a term query).

On Wednesday, February 8, 2012 at 4:56 PM, thanuja wrote:

> Hi All,  
> I am facing two problems in my code:  
> 1.  
> I am using date histogram facet for implementing time chart. While  
> applying multi-filter for the facet I am facing error like Failed to  
> execute.  
> My Code is working when I am using  
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("1").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(queryFilter(termQuery("mediatypeid","1"))));
> 
> But if I place this code inside a for loop for changing mediatypeid  
> value like  
> String mediatypeArray[i]={1,2,3,4};  
> for(int i=0; i\<mediatypeArray[i];i++)  
> {  
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("daterange"+mediatypeArray[i]).facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(queryFilter(termQuery("mediatypeid",mediatypeArray[i]))));  
> }
> 
> The above mediatypeArray contains a set of mediatype ids
> 
> And also I cant able to apply filter for this facet.
> 
> 1. I need a time line graph for number of posts for particular date  
> I need a facet like
> 
> "date e.g(1328054400000)" : {  
> "\_type" : "date\_histogram",  
> "entries" : [ {  
> "term" : 1,  
> "count" : 4  
> }]  
> }
> 
> but I am getting like this,  
> "print" : {  
> "\_type" : "date\_histogram",  
> "entries" : [ {  
> "time" : 1328054400000,  
> "count" : 4  
> }, {  
> "time" : 1328140800000,  
> "count" : 1  
> }, {  
> "time" : 1328227200000,  
> "count" : 1  
> }, {  
> "time" : 1328400000000,  
> "count" : 1  
> }, {  
> "time" : 1328486400000,  
> "count" : 4  
> }, {  
> "time" : 1328572800000,  
> "count" : 3  
> }, {  
> "time" : 1328659200000,  
> "count" : 1  
> } ]  
> }
> 
> Could you please help in this?.Hope I will resolve my problem soon.  
> Thanks in advance,
> 
> Thanuja

---

<div class="post-metadata">

**Author:** ![thanuja\_gandhi](https://avatars.discourse-cdn.com/v4/letter/t/dc4da7/32.png) [@thanuja\_gandhi](https://discuss.elastic.co/u/thanuja_gandhi)\
**Post date:** [February 9, 2012, 8:40am UTC](https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636/3 "2012-02-09T08:40:35Z")

</div>

Hi Shay,

Thanks for your immediate response.

I have changed my code like:

searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("1").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid","1")));

searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("2").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid","2")));

It is working fine......

But if I use inside for loop It is throwing exception

for(int i=0;i\<mediaTypeHistArray.length;i++)  
{  
searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("daterange"+mediaTypeHistArray[i]).facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid",mediaTypeHistArray[i])));  
}

org.elasticsearch.action.search.ReduceSearchPhaseException: Failed to  
execute phase [fetch], [reduce] Exception.

Is there any option to filter the facet using filterbuilder ?

Am I doing anything wrong? Why this code works fine for separate filters?  
and not inside for loop?. Please correct me if I am wrong .

Once again thank u very much for ur timely help Shay!

-Thanuja

---

<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 12, 2012, 11:01am UTC](https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636/4 "2012-02-12T11:01:04Z")

</div>

I still don't understand what working find for you means.., you call facetFilter twice on the same facet (it won't break things, the last one will simply be used). Can you check in the logs on the servers and gist the full failure.

On Thursday, February 9, 2012 at 10:40 AM, thanuja gandhi wrote:

> Hi Shay,
> 
> Thanks for your immediate response.
> 
> I have changed my code like:
> 
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("1").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid","1")));
> 
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("2").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid","2")));
> 
> It is working fine......
> 
> But if I use inside for loop It is throwing exception
> 
> for(int i=0;i\<mediaTypeHistArray.length;i++)  
> {  
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("daterange"+mediaTypeHistArray[i]).facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid",mediaTypeHistArray[i])));  
> }
> 
> org.elasticsearch.action.search.ReduceSearchPhaseException: Failed to execute phase [fetch], [reduce] Exception.
> 
> Is there any option to filter the facet using filterbuilder ?
> 
> Am I doing anything wrong? Why this code works fine for separate filters? and not inside for loop?. Please correct me if I am wrong .
> 
> Once again thank u very much for ur timely help Shay!
> 
> -Thanuja

---

<div class="post-metadata">

**Author:** ![thanuja\_gandhi](https://avatars.discourse-cdn.com/v4/letter/t/dc4da7/32.png) [@thanuja\_gandhi](https://discuss.elastic.co/u/thanuja_gandhi)\
**Post date:** [February 15, 2012, 4:26am UTC](https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636/5 "2012-02-15T04:26:58Z")

</div>

Hi Shay,

Thanks. I have got it clear from one of our ES User posts.

[http://groups.google.com/group/elasticsearch/browse\_thread/thread/b52819354e9bf3ba/09852b66cdf67cb5?lnk=gst&q=Rich#09852b66cdf67cb5](http://groups.google.com/group/elasticsearch/browse_thread/thread/b52819354e9bf3ba/09852b66cdf67cb5?lnk=gst&q=Rich#09852b66cdf67cb5)  
.

On Sun, Feb 12, 2012 at 4:31 PM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> I still don't understand what working find for you means.., you call  
> facetFilter twice on the same facet (it won't break things, the last one  
> will simply be used). Can you check in the logs on the servers and gist the  
> full failure.
> 
> On Thursday, February 9, 2012 at 10:40 AM, thanuja gandhi wrote:
> 
> Hi Shay,
> 
> Thanks for your immediate response.
> 
> I have changed my code like:
> 
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("1").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid","1")));
> 
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("2").facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid","2")));
> 
> It is working fine......
> 
> But if I use inside for loop It is throwing exception
> 
> for(int i=0;i\<mediaTypeHistArray.length;i++)  
> {
> 
> searchRequestBuilder.addFacet(FacetBuilders.dateHistogramFacet("daterange"+mediaTypeHistArray[i]).facetFilter(finalFilter).field("harvesttime").interval("day").facetFilter(termFilter("mediatypeid",mediaTypeHistArray[i])));  
> }
> 
> org.elasticsearch.action.search.ReduceSearchPhaseException: Failed to  
> execute phase [fetch], [reduce] Exception.
> 
> Is there any option to filter the facet using filterbuilder ?
> 
> Am I doing anything wrong? Why this code works fine for separate filters?  
> and not inside for loop?. Please correct me if I am wrong .
> 
> Once again thank u very much for ur timely help Shay!
> 
> -Thanuja

---

<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:39am UTC](https://discuss.elastic.co/t/facetfilter-for-datehistogramfacet/6636/6 "2017-07-06T03:39:18Z")

</div>


