# Range Facet with Dates

**URL:** <https://discuss.elastic.co/t/range-facet-with-dates/11660>\
**Category:** Elasticsearch\
**Created:** [April 22, 2013, 7:56pm UTC](https://discuss.elastic.co/t/range-facet-with-dates/11660 "2013-04-22T19:56:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TeeTim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/teetim/32/2370_2.png) [@TeeTim](https://discuss.elastic.co/u/TeeTim)\
**Post date:** [April 22, 2013, 7:56pm UTC](https://discuss.elastic.co/t/range-facet-with-dates/11660/1 "2013-04-22T19:56:33Z")

</div>

On a field query where my document contains a collection of samples, each  
with a collection time, I only want to count the number of samples where  
the collection time is between a pair of dates. The goal is to return an  
accurate number of collected samples within a time range. Will using a  
Range Facet do the trick?

The website states:  
\*  
\*

_"The range facet support also providing the range as string formatted  
dates."_

- 
- 

Does this mean that the Java API supports something like this on field  
searches:

DateTime fromTime = new DateTime( filterOptions.fromTime() );  
DateTime toTime = new DateTime( filterOptions.toTime() );  
RangeFacetBuilder sampleRange = FacetBuilders.rangeFacet( "sampleTimeRange"  
)  
.field( "samples.collected" )  
.addRange(fromTime.toString(), toTime.toString() );

Or should I stick to using addRange(long,long) version of the API?

Much thanks for you insight.

Tim Sheridan

--  
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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 22, 2013, 9:09pm UTC](https://discuss.elastic.co/t/range-facet-with-dates/11660/2 "2013-04-22T21:09:43Z")

</div>

As far as I remember it, you can write something like:  
RangeFacetBuilder sampleRange = FacetBuilders.rangeFacet( "sampleTimeRange" )  
.field( "samples.collected" )  
.addRange("2013-01-01", "2014" );

That said, if you need only one range, you perhaps don't need facet at all but only use the \_count API and apply a RangeFilter.

Does it help?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 22 avr. 2013 à 21:56, Tim Sheridan [tim.sheridan@me.com](mailto:tim.sheridan@me.com) a écrit :

> On a field query where my document contains a collection of samples, each with a collection time, I only want to count the number of samples where the collection time is between a pair of dates. The goal is to return an accurate number of collected samples within a time range. Will using a Range Facet do the trick?
> 
> The website states:
> 
> "The range facet support also providing the range as string formatted dates."
> 
> Does this mean that the Java API supports something like this on field searches:
> 
> DateTime fromTime = new DateTime( filterOptions.fromTime() );  
> DateTime toTime = new DateTime( filterOptions.toTime() );  
> RangeFacetBuilder sampleRange = FacetBuilders.rangeFacet( "sampleTimeRange" )  
> .field( "samples.collected" )  
> .addRange(fromTime.toString(), toTime.toString() );
> 
> Or should I stick to using addRange(long,long) version of the API?
> 
> Much thanks for you insight.
> 
> Tim Sheridan
> 
> --  
> 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:** ![sslhj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sslhj/32/119856_2.png) [@sslhj](https://discuss.elastic.co/u/sslhj)\
**Post date:** [April 23, 2013, 12:39am UTC](https://discuss.elastic.co/t/range-facet-with-dates/11660/3 "2013-04-23T00:39:00Z")

</div>

hi,  
"addRange(fromTime.toString(), toTime.toString() )" is ok, but the pattern of Date and Time need to be "yyyy-MM-dd'T'HH:mm:ss" or "yyyy-MM-dd"

---

<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:40am UTC](https://discuss.elastic.co/t/range-facet-with-dates/11660/4 "2017-07-06T02:40:01Z")

</div>


