# Aggregations with dates & scripts

**URL:** https://discuss.elastic.co/t/aggregations-with-dates-scripts/21071
**Category:** Elasticsearch
**Created:** [December 3, 2014, 10:47pm UTC](https://discuss.elastic.co/t/aggregations-with-dates-scripts/21071 "2014-12-03T22:47:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Guillaume\_Alleon](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@Guillaume\_Alleon](https://discuss.elastic.co/u/Guillaume_Alleon)
#### Post date: [December 3, 2014, 10:47pm UTC](https://discuss.elastic.co/t/aggregations-with-dates-scripts/21071/1 "2014-12-03T22:47:40Z")

</div>

Hi

I have a mapping which is having the following 2 properties:  
"start\_date": {  
"format": "dateOptionalTime",  
"type": "date"  
},  
"end\_date": {  
"format": "dateOptionalTime",  
"type": "date"  
},

I want to create an histogram for durations with 1 hour intervals by doing  
this:

curl -XGET '[http://localhost:9200/myindex/travels/\_search\_search?pretty](http://localhost:9200/myindex/travels/_search_search?pretty)' -d  
'

{

```
"aggs": {

    "duration": {

        "histogram": {

            "interval": 1,

            "script": "(doc['end_date']-doc['start_date'])/3600"

        }

    }

}

```

}'

I got the following error:

GroovyScriptExecutionException[MissingPropertyException[No such property:  
end\_date for class: Script1]]; }{[B4xwr8FCSnOLkrNAVhgHMg][gequest][2]:  
QueryPhaseExecutionException[[myindex][2]:  
query[ConstantScore(cache(\_type:travels))],from[0],size[10]: Query Failed  
[Failed to execute main query]]; nested:  
GroovyScriptExecutionException[MissingPropertyException[No such property:  
end\_date for class: Script1]]; }

I have the same error for start\_date.

Any idea what I am doing wrong ?

Cheers

Guillaume

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6ca86563-5881-4004-af35-a258841ec326%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6ca86563-5881-4004-af35-a258841ec326%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)
#### Post date: [December 6, 2014, 12:44pm UTC](https://discuss.elastic.co/t/aggregations-with-dates-scripts/21071/2 "2014-12-06T12:44:52Z")

</div>

Hi Guillaume,

You should use doc[‘end\_date’].value instead of doc[‘end\_date’] .  
See : [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#_document_fields)

And you should use 3600000 instead of 3600.  
Date type is a long number representing UTC milliseconds since the epoch.

See : [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#date)

I hope that those help you out.

* * *

Jun Ohtani  
[johtani@gmail.com](mailto:johtani@gmail.com)  
blog : [http://blog.johtani.info](http://blog.johtani.info)  
twitter : [http://twitter.com/johtani](http://twitter.com/johtani)

> 2014/12/04 7:47、Guillaume Alleon [guillaume.alleon@gmail.com](mailto:guillaume.alleon@gmail.com) のメール：
> 
> Hi
> 
> I have a mapping which is having the following 2 properties:  
> "start\_date": {  
> "format": "dateOptionalTime",  
> "type": "date"  
> },  
> "end\_date": {  
> "format": "dateOptionalTime",  
> "type": "date"  
> },
> 
> I want to create an histogram for durations with 1 hour intervals by doing this:
> 
> curl -XGET '[http://localhost:9200/myindex/travels/\_search\_search?pretty](http://localhost:9200/myindex/travels/_search_search?pretty)' -d '  
> {
> 
> ```
> "aggs": {
> 
> "duration": {
> 
> "histogram": {
> 
> "interval": 1,
> 
> "script": "(doc['end_date']-doc['start_date'])/3600"
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }'
> 
> I got the following error:
> 
> GroovyScriptExecutionException[MissingPropertyException[No such property: end\_date for class: Script1]]; }{[B4xwr8FCSnOLkrNAVhgHMg][gequest][2]: QueryPhaseExecutionException[[myindex][2]: query[ConstantScore(cache(\_type:travels))],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[MissingPropertyException[No such property: end\_date for class: Script1]]; }
> 
> I have the same error for start\_date.
> 
> Any idea what I am doing wrong ?
> 
> Cheers
> 
> Guillaume
> 
> --  
> 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).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6ca86563-5881-4004-af35-a258841ec326%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6ca86563-5881-4004-af35-a258841ec326%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/C3D0F8B9-CAA7-46A9-ACC0-505D6C0F22F8%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/C3D0F8B9-CAA7-46A9-ACC0-505D6C0F22F8%40gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Guillaume\_Alleon](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@Guillaume\_Alleon](https://discuss.elastic.co/u/Guillaume_Alleon)
#### Post date: [December 6, 2014, 6:42pm UTC](https://discuss.elastic.co/t/aggregations-with-dates-scripts/21071/3 "2014-12-06T18:42:15Z")

</div>

Hi Jun

Thanks a lot. That helped a lot. I also had to change:  
doc['end\_date'].value by doc["end\_date"]

Cheers  
Guillaume

On 6 December 2014 at 12:44, Jun Ohtani [johtani@gmail.com](mailto:johtani@gmail.com) wrote:

> Hi Guillaume,
> 
> You should use doc[‘end\_date’].value instead of doc[‘end\_date’] .  
> See :  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#_document_fields)
> 
> And you should use 3600000 instead of 3600.  
> Date type is a long number representing UTC milliseconds since the epoch.
> 
> See :  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#date)
> 
> I hope that those help you out.
> 
> * * *
> 
> Jun Ohtani  
> [johtani@gmail.com](mailto:johtani@gmail.com)  
> blog : [http://blog.johtani.info](http://blog.johtani.info)  
> twitter : [http://twitter.com/johtani](http://twitter.com/johtani)
> 
> > 2014/12/04 7:47、Guillaume Alleon [guillaume.alleon@gmail.com](mailto:guillaume.alleon@gmail.com) のメール：
> > 
> > Hi
> > 
> > I have a mapping which is having the following 2 properties:  
> > "start\_date": {  
> > "format": "dateOptionalTime",  
> > "type": "date"  
> > },  
> > "end\_date": {  
> > "format": "dateOptionalTime",  
> > "type": "date"  
> > },
> > 
> > I want to create an histogram for durations with 1 hour intervals by  
> > doing this:
> > 
> > curl -XGET '[http://localhost:9200/myindex/travels/\_search\_search?pretty](http://localhost:9200/myindex/travels/_search_search?pretty)'  
> > -d '  
> > {
> > 
> > ```
> > "aggs": {
> > 
> > "duration": {
> > 
> > "histogram": {
> > 
> > "interval": 1,
> > 
> > "script": "(doc['end_date']-doc['start_date'])/3600"
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > ```
> > 
> > }'
> > 
> > I got the following error:
> > 
> > GroovyScriptExecutionException[MissingPropertyException[No such  
> > property: end\_date for class: Script1]];  
> > }{[B4xwr8FCSnOLkrNAVhgHMg][gequest][2]:  
> > QueryPhaseExecutionException[[myindex][2]:  
> > query[ConstantScore(cache(\_type:travels))],from[0],size[10]: Query Failed  
> > [Failed to execute main query]]; nested:  
> > GroovyScriptExecutionException[MissingPropertyException[No such property:  
> > end\_date for class: Script1]]; }
> > 
> > I have the same error for start\_date.
> > 
> > Any idea what I am doing wrong ?
> > 
> > Cheers
> > 
> > Guillaume
> > 
> > --  
> > 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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/6ca86563-5881-4004-af35-a258841ec326%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6ca86563-5881-4004-af35-a258841ec326%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/EO8RWlNRJjA/unsubscribe](https://groups.google.com/d/topic/elasticsearch/EO8RWlNRJjA/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/C3D0F8B9-CAA7-46A9-ACC0-505D6C0F22F8%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/C3D0F8B9-CAA7-46A9-ACC0-505D6C0F22F8%40gmail.com)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
PGP KeyID: 2048R/EA31CFC9 [subkeys.pgp.net](http://subkeys.pgp.net)

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAE1e5yEfkKeghMRyahRA\_C4c0u7k7pcsmuX2Bv3nQgxzujY5Hw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAE1e5yEfkKeghMRyahRA_C4c0u7k7pcsmuX2Bv3nQgxzujY5Hw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:45am UTC](https://discuss.elastic.co/t/aggregations-with-dates-scripts/21071/4 "2017-07-06T00:45:17Z")

</div>


