# Stats aggregation, return documents at min/max

**URL:** <https://discuss.elastic.co/t/stats-aggregation-return-documents-at-min-max/18081>\
**Category:** Elasticsearch\
**Created:** [June 12, 2014, 11:08pm UTC](https://discuss.elastic.co/t/stats-aggregation-return-documents-at-min-max/18081 "2014-06-12T23:08:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Isla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_isla/32/1484_2.png) [@Dan\_Isla](https://discuss.elastic.co/u/Dan_Isla)\
**Post date:** [June 12, 2014, 11:08pm UTC](https://discuss.elastic.co/t/stats-aggregation-return-documents-at-min-max/18081/1 "2014-06-12T23:08:13Z")

</div>

I'm trying to combine a date\_histogram agg with a stats agg to find the  
min/max within a bucket of documents, so far so good, but now I need a  
field from the document where the Min/Max was actually found.

My index basically has millions of x,y datapoints that I want to search for  
and plot.

What I want are the 2 documents from each bucket where the 'y' value was a  
min and a max.

This query successfully gave me buckets divided by 300s intervals and the  
min/max y values within those buckets. Problem is, I have no way of linking  
those min/max values to their corresponding 'x' value for plotting.

{  
"size": 0,  
"aggs" : {  
"vals" : {  
"filter" : { "term" : { "component" : "data\_to\_plot" } },  
"aggs" : {  
"values\_over\_time" : {  
"date\_histogram" : {  
"field" : "x",  
"interval" : "300s"  
},  
"aggs": {  
"stats\_y": {"stats": {"field": "y"} },

```
                }
            }
        }
    }
}          

```

}

Any suggestions?

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/bfa7f7d9-6d8c-493d-bcea-5acae891ab07%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bfa7f7d9-6d8c-493d-bcea-5acae891ab07%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Dan\_Isla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_isla/32/1484_2.png) [@Dan\_Isla](https://discuss.elastic.co/u/Dan_Isla)\
**Post date:** [June 18, 2014, 12:15am UTC](https://discuss.elastic.co/t/stats-aggregation-return-documents-at-min-max/18081/2 "2014-06-18T00:15:58Z")

</div>

I was able to solve my own problem using a terms sub-aggregation on the 'x'  
values in the interval bucket.

{  
"size": 0,  
"aggs" : {  
"vals": {  
"filter": {"term" : { "component" : "data\_to\_plot" }},  
"aggs": {  
"values\_over\_time" : {  
"date\_histogram" : {  
"field" : "time\_seconds",  
"interval" : "1500s"  
},  
"aggs": {  
"time\_y\_min": {  
"terms": {  
"field": "time\_seconds",  
"order": {"y\_min": "asc"},  
"size": 1  
},  
"aggs": {  
"y\_min": {"min": {"field": "y" } }  
}  
},  
"time\_y\_max": {  
"terms": {  
"field": "time\_seconds",  
"order": {"y\_max": "desc"},  
"size": 1  
},  
"aggs": {  
"y\_max": {"max": {"field": "y" } }  
}  
}  
}  
}  
}  
}  
}  
}

On Thursday, June 12, 2014 4:08:13 PM UTC-7, Dan Isla wrote:

> I'm trying to combine a date\_histogram agg with a stats agg to find the  
> min/max within a bucket of documents, so far so good, but now I need a  
> field from the document where the Min/Max was actually found.
> 
> My index basically has millions of x,y datapoints that I want to search  
> for and plot.
> 
> What I want are the 2 documents from each bucket where the 'y' value was a  
> min and a max.
> 
> This query successfully gave me buckets divided by 300s intervals and the  
> min/max y values within those buckets. Problem is, I have no way of linking  
> those min/max values to their corresponding 'x' value for plotting.
> 
> {  
> "size": 0,  
> "aggs" : {  
> "vals" : {  
> "filter" : { "term" : { "component" : "data\_to\_plot" } },  
> "aggs" : {  
> "values\_over\_time" : {  
> "date\_histogram" : {  
> "field" : "x",  
> "interval" : "300s"  
> },  
> "aggs": {  
> "stats\_y": {"stats": {"field": "y"} },
> 
> ```
> }
> }
> }
> }
> }          
> 
> ```
> 
> }
> 
> Any suggestions?
> 
> 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/764262f4-41a9-4e1d-9485-ac1fa904825a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/764262f4-41a9-4e1d-9485-ac1fa904825a%40googlegroups.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, 1:21am UTC](https://discuss.elastic.co/t/stats-aggregation-return-documents-at-min-max/18081/3 "2017-07-06T01:21:39Z")

</div>


