# Date\_historgram facet issue

**URL:** <https://discuss.elastic.co/t/date-historgram-facet-issue/15585>\
**Category:** Elasticsearch\
**Created:** [February 4, 2014, 10:06am UTC](https://discuss.elastic.co/t/date-historgram-facet-issue/15585 "2014-02-04T10:06:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuel\_merlet1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samuel_merlet1/32/1242_2.png) [@samuel\_merlet1](https://discuss.elastic.co/u/samuel_merlet1)\
**Post date:** [February 4, 2014, 10:06am UTC](https://discuss.elastic.co/t/date-historgram-facet-issue/15585/1 "2014-02-04T10:06:56Z")

</div>

Hi  
i have some documents like this

{  
date : "2014-01-01",  
"periods" : {  
{ "start" : 0 , "duration": 55 },  
{ "start" : 1 , "duration": 55 },

```
{ "start" : 2 , "duration": 55 },

{ "start" : 3 , "duration": 55 },

etc...

```

}  
}

I do a query on a date range with a statistical facet to get the sum of all  
duration

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"range": {  
"date": {  
"gte": "2014-01-01",  
"lte": "2014-01-11"  
}  
}  
}  
}  
},  
"facets": {  
"stat1": {  
"statistical": {  
"field": "duration"  
},  
"nested": "periods"  
}  
}  
}

this works perfect .  
Now i need to get the sum of duration for each day , so i tried by using  
the date\_historgram , but i have no luck with it ( i guess because my  
periods is a nested object )

here is my try

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"range": {  
"date": {  
"gte": "2013-01-10",  
"lte": "2013-01-11"  
}  
}  
}  
}  
},  
"facets": {  
"stat1": {  
"statistical": {  
"field": "duration"  
},  
"nested": "periods"  
},  
"stat2": {  
"date\_histogram": {  
"key\_field": "date",  
"value\_field": "duration",  
"interval": "day"  
},  
"nested": "periods"  
}  
}  
}

Anyone can help me on this ? Thanks

--  
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/3a65022d-1787-4d1a-aaca-d0f493f54070%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3a65022d-1787-4d1a-aaca-d0f493f54070%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![samuel\_merlet1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samuel_merlet1/32/1242_2.png) [@samuel\_merlet1](https://discuss.elastic.co/u/samuel_merlet1)\
**Post date:** [February 6, 2014, 4:42pm UTC](https://discuss.elastic.co/t/date-historgram-facet-issue/15585/2 "2014-02-06T16:42:57Z")

</div>

Just for helping someone if needed , this could be done using aggregations  
of ES 1.x

like this

{  
"size": 0,  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
}  
}  
},  
"aggs": {  
"nest\_global": {  
"nested": {  
"path": "periods"  
},  
"aggs": {  
"stat2": {  
"stats": {  
"field": "duration"  
}  
}  
}  
},  
"par\_date": {  
"date\_histogram": {  
"field": "date",  
"interval": "day"  
},  
"aggs": {  
"nest\_par\_date": {  
"nested": {  
"path": "periods"  
},  
"aggs": {  
"stat2": {  
"stats": {  
"field": "duration"  
}  
}  
}  
}  
}  
}  
}  
}

Thanks to Erwan D from Elastic French Group !

Le mardi 4 février 2014 11:06:56 UTC+1, Samuel Merlet a écrit :

> Hi  
> i have some documents like this
> 
> {  
> date : "2014-01-01",  
> "periods" : {  
> { "start" : 0 , "duration": 55 },  
> { "start" : 1 , "duration": 55 },
> 
> ```
> { "start" : 2 , "duration": 55 },
> 
> { "start" : 3 , "duration": 55 },
> 
> etc...
> 
> ```
> 
> }  
> }
> 
> I do a query on a date range with a statistical facet to get the sum of  
> all duration
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "range": {  
> "date": {  
> "gte": "2014-01-01",  
> "lte": "2014-01-11"  
> }  
> }  
> }  
> }  
> },  
> "facets": {  
> "stat1": {  
> "statistical": {  
> "field": "duration"  
> },  
> "nested": "periods"  
> }  
> }  
> }
> 
> this works perfect .  
> Now i need to get the sum of duration for each day , so i tried by using  
> the date\_historgram , but i have no luck with it ( i guess because my  
> periods is a nested object )
> 
> here is my try
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "range": {  
> "date": {  
> "gte": "2013-01-10",  
> "lte": "2013-01-11"  
> }  
> }  
> }  
> }  
> },  
> "facets": {  
> "stat1": {  
> "statistical": {  
> "field": "duration"  
> },  
> "nested": "periods"  
> },  
> "stat2": {  
> "date\_histogram": {  
> "key\_field": "date",  
> "value\_field": "duration",  
> "interval": "day"  
> },  
> "nested": "periods"  
> }  
> }  
> }
> 
> Anyone can help me on this ? Thanks

--  
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/a58ae6f1-1fa4-43d5-b62a-4c6dd436b2d5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a58ae6f1-1fa4-43d5-b62a-4c6dd436b2d5%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:51am UTC](https://discuss.elastic.co/t/date-historgram-facet-issue/15585/3 "2017-07-06T01:51:55Z")

</div>


