# JAVA API: get the oldest document within a time range

**URL:** https://discuss.elastic.co/t/java-api-get-the-oldest-document-within-a-time-range/53311
**Category:** Elasticsearch
**Created:** [June 20, 2016, 1:01pm UTC](https://discuss.elastic.co/t/java-api-get-the-oldest-document-within-a-time-range/53311 "2016-06-20T13:01:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)
#### Post date: [June 20, 2016, 1:01pm UTC](https://discuss.elastic.co/t/java-api-get-the-oldest-document-within-a-time-range/53311/1 "2016-06-20T13:01:25Z")

</div>

Hello everyone,

I can't manage to query with the java api while I can do it with logstash for instance.  
Here is the context: I have an index where documents have fields called 'size' and 'time'. What I would like to do is to get the oldest document within a time range. Do you know how to do that with the java API ?

I know that i have to use the function rangeQuery("time").from("xxx").to("yyyy")  
to set the time range, but i don't find any function 'sort'..

Thank you for your attention and your help.

S

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [June 20, 2016, 1:20pm UTC](https://discuss.elastic.co/t/java-api-get-the-oldest-document-within-a-time-range/53311/2 "2016-06-20T13:20:05Z")

</div>

It should look like `.addSort("field", SortOrder.ASC)`. [Here](https://github.com/elastic/elasticsearch/blob/2.x/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java#L72) is an example from the tests.

---

<div class="post-metadata">

### Author: ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)
#### Post date: [June 21, 2016, 8:26am UTC](https://discuss.elastic.co/t/java-api-get-the-oldest-document-within-a-time-range/53311/3 "2016-06-21T08:26:18Z")

</div>

Thanks Nik.  
Actually, in my log I also have the field device. So i would like to get my specific value for every device. Tha'ts why I use sub aggregation but it does not work.

**Here is the code:**  
SearchResponse response1 = (SearchResponse) client.prepareSearch(index)  
.setQuery(QueryBuilders.matchAllQuery())  
.setPostFilter(QueryBuilders.rangeQuery("time").from("xxx").to("yyy"))  
.addAggregation(AggregationBuilders.terms("perDevice").field("macAddress")  
.subAggregation(AggregationBuilders.sum("sum\_size").field("size"))  
.subAggregation(AggregationBuilders.sum("sum\_size").field("size"))  
.subAggregation((BytesReference) QueryBuilders.termsLookupQuery("size"))  
.addSort("time", SortOrder.ASC)  
.execute()  
.actionGet();

**Error:**  
_Exception in thread "main" java.lang.ClassCastException: org.elasticsearch.index.query.TermsLookupQueryBuilder cannot be cast to org.elasticsearch.common.bytes.BytesReference_

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [June 21, 2016, 1:30pm UTC](https://discuss.elastic.co/t/java-api-get-the-oldest-document-within-a-time-range/53311/4 "2016-06-21T13:30:43Z")

</div>

Maybe you need the [min](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-min-bucket-aggregation.html) bucket pipeline aggregation? I don't know aggregations super well but it seems more likely than a sort.

---

<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 5, 2017, 10:41pm UTC](https://discuss.elastic.co/t/java-api-get-the-oldest-document-within-a-time-range/53311/5 "2017-07-05T22:41:45Z")

</div>


