use of org.opensearch.client.opensearch._types.aggregations.HistogramAggregate in project opensearch-java by opensearch-project.
the class RequestTest method testSearchAggregation.
@Test
public void testSearchAggregation() throws IOException {
highLevelClient().create(_1 -> _1.index("products").id("A").document(new Product(5)).refresh(Refresh.True));
highLevelClient().create(_1 -> _1.index("products").id("B").document(new Product(15)).refresh(Refresh.True));
highLevelClient().create(_1 -> _1.index("products").id("C").document(new Product(25)).refresh(Refresh.True));
SearchResponse<Product> searchResponse = highLevelClient().search(_1 -> _1.index("products").size(0).aggregations("prices", _3 -> _3.histogram(_4 -> _4.field("price").interval(10.0)).aggregations("average", _5 -> _5.avg(_6 -> _6.field("price")))), Product.class);
HistogramAggregate prices = searchResponse.aggregations().get("prices").histogram();
assertEquals(3, prices.buckets().array().size());
assertEquals(1, prices.buckets().array().get(0).docCount());
assertEquals(5.0, prices.buckets().array().get(0).aggregations().get("average").avg().value(), 0.01);
// We've set "size" to zero
assertEquals(0, searchResponse.hits().hits().size());
}
Aggregations