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'..
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
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.