# Date Histogram Display like "1 hour ago", "2 days ago", etc

**URL:** https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861
**Category:** Elasticsearch
**Created:** [July 14, 2011, 2:59pm UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861 "2011-07-14T14:59:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Raul\_Jr\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_jr_martinez/32/1286_2.png) [@Raul\_Jr\_Martinez](https://discuss.elastic.co/u/Raul_Jr_Martinez)
#### Post date: [July 14, 2011, 2:59pm UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861/1 "2011-07-14T14:59:20Z")

</div>

Hello,

I've got no experience writing "script" in ElasticSearch. I have a  
datehistogram facets and it's giving me the facet values as long int.

Using "script", is there a way to return the following facet values?

30 minutes ago  
1 hour ago  
5 hours ago  
1 day ago  
6 days ago  
1 month ago

Can this be done using the script parameter?

Thanks so much!

Raul

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [July 14, 2011, 3:06pm UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861/2 "2011-07-14T15:06:56Z")

</div>

Hi Raul

> I've got no experience writing "script" in Elasticsearch. I have a  
> datehistogram facets and it's giving me the facet values as long int.
> 
> Using "script", is there a way to return the following facet values?
> 
> 30 minutes ago  
> 1 hour ago  
> 5 hours ago  
> 1 day ago  
> 6 days ago  
> 1 month ago
> 
> Can this be done using the script parameter?

The best way to do this would be to use a range facet, and to pass it a  
separate range for each of the periods you list above:

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

clint

---

<div class="post-metadata">

### Author: ![Raul\_Jr\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_jr_martinez/32/1286_2.png) [@Raul\_Jr\_Martinez](https://discuss.elastic.co/u/Raul_Jr_Martinez)
#### Post date: [July 14, 2011, 3:43pm UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861/3 "2011-07-14T15:43:43Z")

</div>

Hi Clint,

Thanks for the insights. Yes, I realized that range is the appropriate  
facet type for this solution and create a decoration at application  
level to say if its 30 minutes, 1 day, 1 week etc.

I'm taking the idea from the sample query in the documentaton (http://  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html))  
{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"range1" : {  
"range" : {  
"key\_script" : "doc['date'].date.minuteOfHour",  
"value\_script" : "doc['num1'].value",  
"ranges" : [  
{ "to" : 50 },  
{ "from" : 20, "to" : 70 },  
{ "from" : 70, "to" : 120 },  
{ "from" : 150 }  
]  
}  
}  
}  
}

Thanks  
Raul

On Jul 14, 11:06 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Raul
> 
> > I've got no experience writing "script" in Elasticsearch. I have a  
> > datehistogram facets and it's giving me the facet values as long int.
> 
> > Using "script", is there a way to return the following facet values?
> 
> > 30 minutes ago  
> > 1 hour ago  
> > 5 hours ago  
> > 1 day ago  
> > 6 days ago  
> > 1 month ago
> 
> > Can this be done using the script parameter?
> 
> The best way to do this would be to use a range facet, and to pass it a  
> separate range for each of the periods you list above:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/range-)...
> 
> clint

---

<div class="post-metadata">

### Author: ![Raul\_Jr\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_jr_martinez/32/1286_2.png) [@Raul\_Jr\_Martinez](https://discuss.elastic.co/u/Raul_Jr_Martinez)
#### Post date: [July 14, 2011, 4:04pm UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861/4 "2011-07-14T16:04:18Z")

</div>

Hi Clint,

Well, I respond too quickly. I'm not really comprehending the  
key\_script and value\_script and what value should I be using for these  
two parameters..

Would it be okay if you can provide an example for this? Appreciated.

Thanks,  
raul

On Jul 14, 11:43 pm, rmartinez [jun...@gmail.com](mailto:jun...@gmail.com) wrote:

> Hi Clint,
> 
> Thanks for the insights. Yes, I realized that range is the appropriate  
> facet type for this solution and create a decoration at application  
> level to say if its 30 minutes, 1 day, 1 week etc.
> 
> I'm taking the idea from the sample query in the documentaton ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html))  
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "facets" : {  
> "range1" : {  
> "range" : {  
> "key\_script" : "doc['date'].date.minuteOfHour",  
> "value\_script" : "doc['num1'].value",  
> "ranges" : [  
> { "to" : 50 },  
> { "from" : 20, "to" : 70 },  
> { "from" : 70, "to" : 120 },  
> { "from" : 150 }  
> ]  
> }  
> }  
> }
> 
> }
> 
> Thanks  
> Raul
> 
> On Jul 14, 11:06 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:
> 
> > Hi Raul
> 
> > > I've got no experience writing "script" in Elasticsearch. I have a  
> > > datehistogram facets and it's giving me the facet values as long int.
> 
> > > Using "script", is there a way to return the following facet values?
> 
> > > 30 minutes ago  
> > > 1 hour ago  
> > > 5 hours ago  
> > > 1 day ago  
> > > 6 days ago  
> > > 1 month ago
> 
> > > Can this be done using the script parameter?
> 
> > The best way to do this would be to use a range facet, and to pass it a  
> > separate range for each of the periods you list above:
> 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/range-)...
> 
> > clint

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [July 14, 2011, 4:35pm UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861/5 "2011-07-14T16:35:37Z")

</div>

Hi Raul

> Well, I respond too quickly. I'm not really comprehending the  
> key\_script and value\_script and what value should I be using for these  
> two parameters..

I'd just provide the datetime values from my app, so:

curl -XGET '[http://127.0.0.1:9200/\_search?pretty=1](http://127.0.0.1:9200/_search?pretty=1)' -d '  
{  
"facets" : {  
"my\_range" : {  
"range" : {  
"created" : [  
{ "from" : "2011-07-14 12:00:00" },  
{  
"to" : "2011-07-14 12:00:00",  
"from" : "2011-07-14 11:00:00"  
}  
,  
{ etc }  
]  
}  
}  
},  
"size" : 0  
}  
'

alternatively, you can use milliseconds since the epoch time, eg:

1310661229000 == Thu Jul 14 16:33:45 2011 GMT

so 30 mins before that would be:  
1310661229000 - (30 \* 60 \* 1000)

etc

---

<div class="post-metadata">

### Author: ![Raul\_Jr\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_jr_martinez/32/1286_2.png) [@Raul\_Jr\_Martinez](https://discuss.elastic.co/u/Raul_Jr_Martinez)
#### Post date: [July 14, 2011, 6:25pm UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861/6 "2011-07-14T18:25:58Z")

</div>

Thanks Clint,

The use of milliseconds works for me.

Here's what I came up with (some PHP code!)

> <https://gist.github.com/juneym/1083059>

On Jul 15, 12:35 am, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Raul
> 
> > Well, I respond too quickly. I'm not really comprehending the  
> > key\_script and value\_script and what value should I be using for these  
> > two parameters..
> 
> I'd just provide the datetime values from my app, so:
> 
> curl -XGET '[http://127.0.0.1:9200/\_search?pretty=1](http://127.0.0.1:9200/_search?pretty=1)' -d '  
> {  
> "facets" : {  
> "my\_range" : {  
> "range" : {  
> "created" : [  
> { "from" : "2011-07-14 12:00:00" },  
> {  
> "to" : "2011-07-14 12:00:00",  
> "from" : "2011-07-14 11:00:00"  
> }  
> ,  
> { etc }  
> ]  
> }  
> }  
> },  
> "size" : 0}
> 
> '
> 
> alternatively, you can use milliseconds since the epoch time, eg:
> 
> 1310661229000 == Thu Jul 14 16:33:45 2011 GMT
> 
> so 30 mins before that would be:  
> 1310661229000 - (30 \* 60 \* 1000)
> 
> etc

---

<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:00am UTC](https://discuss.elastic.co/t/date-histogram-display-like-1-hour-ago-2-days-ago-etc/4861/7 "2017-07-06T04:00:37Z")

</div>


