Search in sources :

Example 1 with HistogramAggregate

use of co.elastic.clients.elasticsearch._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());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) HistogramAggregate(org.opensearch.client.opensearch._types.aggregations.HistogramAggregate) OperationType(org.opensearch.client.opensearch.core.bulk.OperationType) Property(org.opensearch.client.opensearch._types.mapping.Property) IndexResponse(org.opensearch.client.opensearch.core.IndexResponse) Map(java.util.Map) Refresh(org.opensearch.client.opensearch._types.Refresh) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) RequestItem(org.opensearch.client.opensearch.core.msearch.RequestItem) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IndexState(org.opensearch.client.opensearch.indices.IndexState) Test(org.junit.Test) IOException(java.io.IOException) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) ClearScrollResponse(org.opensearch.client.opensearch.core.ClearScrollResponse) MsearchResponse(org.opensearch.client.opensearch.core.MsearchResponse) GetIndicesSettingsResponse(org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse) ModelTestCase(org.opensearch.client.opensearch.model.ModelTestCase) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) NodesResponse(org.opensearch.client.opensearch.cat.NodesResponse) GetResponse(org.opensearch.client.opensearch.core.GetResponse) Collections(java.util.Collections) HistogramAggregate(org.opensearch.client.opensearch._types.aggregations.HistogramAggregate) Test(org.junit.Test)

Example 2 with HistogramAggregate

use of co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate in project elasticsearch-java by elastic.

the class RequestTest method testSearchAggregation.

@Test
public void testSearchAggregation() throws IOException {
    client.create(_1 -> _1.index("products").id("A").document(new Product(5)).refresh(Refresh.True));
    client.create(_1 -> _1.index("products").id("B").document(new Product(15)).refresh(Refresh.True));
    client.create(_1 -> _1.index("products").id("C").document(new Product(25)).refresh(Refresh.True));
    SearchResponse<Product> searchResponse = client.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());
}
Also used : CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) BeforeClass(org.junit.BeforeClass) RequestItem(co.elastic.clients.elasticsearch.core.msearch.RequestItem) CompletableFuture(java.util.concurrent.CompletableFuture) IndexState(co.elastic.clients.elasticsearch.indices.IndexState) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) DateTime(co.elastic.clients.util.DateTime) Map(java.util.Map) OperationType(co.elastic.clients.elasticsearch.core.bulk.OperationType) ModelTestCase(co.elastic.clients.elasticsearch.model.ModelTestCase) Property(co.elastic.clients.elasticsearch._types.mapping.Property) Refresh(co.elastic.clients.elasticsearch._types.Refresh) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) HistogramAggregate(co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate) NodesResponse(co.elastic.clients.elasticsearch.cat.NodesResponse) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ElasticsearchTestServer(co.elastic.clients.elasticsearch.ElasticsearchTestServer) Test(org.junit.Test) IOException(java.io.IOException) GetIndexResponse(co.elastic.clients.elasticsearch.indices.GetIndexResponse) Instant(java.time.Instant) GetIndicesSettingsResponse(co.elastic.clients.elasticsearch.indices.GetIndicesSettingsResponse) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) MsearchResponse(co.elastic.clients.elasticsearch.core.MsearchResponse) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClosePointInTimeResponse(co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) DiskUsageResponse(co.elastic.clients.elasticsearch.indices.DiskUsageResponse) ClearScrollResponse(co.elastic.clients.elasticsearch.core.ClearScrollResponse) Assert(org.junit.Assert) Collections(java.util.Collections) IndexResponse(co.elastic.clients.elasticsearch.core.IndexResponse) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) HistogramAggregate(co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 Collections (java.util.Collections)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 Test (org.junit.Test)2 ElasticsearchAsyncClient (co.elastic.clients.elasticsearch.ElasticsearchAsyncClient)1 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)1 ElasticsearchTestServer (co.elastic.clients.elasticsearch.ElasticsearchTestServer)1 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)1 Refresh (co.elastic.clients.elasticsearch._types.Refresh)1 HistogramAggregate (co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate)1 Property (co.elastic.clients.elasticsearch._types.mapping.Property)1 NodesResponse (co.elastic.clients.elasticsearch.cat.NodesResponse)1 BulkResponse (co.elastic.clients.elasticsearch.core.BulkResponse)1 ClearScrollResponse (co.elastic.clients.elasticsearch.core.ClearScrollResponse)1 ClosePointInTimeResponse (co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse)1 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)1 IndexResponse (co.elastic.clients.elasticsearch.core.IndexResponse)1