Search in sources :

Example 1 with Aggregate

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

the class TypedKeysTest method testAdditionalProperties.

@Test
public void testAdditionalProperties() {
    Aggregate avg1 = AvgAggregate.of(_1 -> _1.value(1.0))._toAggregate();
    Aggregate avg2 = AvgAggregate.of(_1 -> _1.value(2.0))._toAggregate();
    Aggregate aggregate = StringTermsAggregate.of(_0 -> _0.sumOtherDocCount(1).buckets(b -> b.array(ListBuilder.of(StringTermsBucket.Builder::new).add(_1 -> _1.key("key_1").docCount(1).aggregations(MapBuilder.of("bar", avg1))).add(_1 -> _1.key("key_2").docCount(2).aggregations(MapBuilder.of("bar", avg2))).build())))._toAggregate();
    SearchResponse<Void> resp = new SearchResponse.Builder<Void>().aggregations("foo", aggregate).took(1).shards(_1 -> _1.successful(1).failed(0).total(1)).hits(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)).hits(Collections.emptyList())).timedOut(false).build();
    String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]}," + "\"aggregations\":{\"sterms#foo\":{\"buckets\":[" + "{\"avg#bar\":{\"value\":1.0},\"doc_count\":1,\"key\":\"key_1\"}," + "{\"avg#bar\":{\"value\":2.0},\"doc_count\":2,\"key\":\"key_2\"}" + "],\"sum_other_doc_count\":1}}}";
    assertEquals(json, toJson(resp));
    resp = fromJson(json, SearchResponse.createSearchResponseDeserializer(JsonpDeserializer.voidDeserializer()));
    StringTermsAggregate foo = resp.aggregations().get("foo").sterms();
    assertEquals(1, foo.sumOtherDocCount());
    assertEquals(1, foo.buckets().array().get(0).docCount());
    assertEquals("key_1", foo.buckets().array().get(0).key());
    assertEquals(1.0, foo.buckets().array().get(0).aggregations().get("bar").avg().value(), 0.01);
    assertEquals("key_2", foo.buckets().array().get(1).key());
    assertEquals(2.0, foo.buckets().array().get(1).aggregations().get("bar").avg().value(), 0.01);
}
Also used : Aggregate(co.elastic.clients.elasticsearch._types.aggregations.Aggregate) AvgAggregate(co.elastic.clients.elasticsearch._types.aggregations.AvgAggregate) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) StringTermsBucket(co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket) TotalHitsRelation(co.elastic.clients.elasticsearch.core.search.TotalHitsRelation) ListBuilder(co.elastic.clients.util.ListBuilder) Test(org.junit.Test) StringTermsAggregate(co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) MapBuilder(co.elastic.clients.util.MapBuilder) Collections(java.util.Collections) JsonpDeserializer(co.elastic.clients.json.JsonpDeserializer) StringTermsAggregate(co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) ListBuilder(co.elastic.clients.util.ListBuilder) MapBuilder(co.elastic.clients.util.MapBuilder) StringTermsBucket(co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket) Aggregate(co.elastic.clients.elasticsearch._types.aggregations.Aggregate) AvgAggregate(co.elastic.clients.elasticsearch._types.aggregations.AvgAggregate) StringTermsAggregate(co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) Test(org.junit.Test)

Example 2 with Aggregate

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

the class TypedKeysTest method testAdditionalProperties.

@Test
public void testAdditionalProperties() {
    Aggregate avg1 = AvgAggregate.of(_1 -> _1.value(1.0))._toAggregate();
    Aggregate avg2 = AvgAggregate.of(_1 -> _1.value(2.0))._toAggregate();
    Aggregate aggregate = StringTermsAggregate.of(_0 -> _0.sumOtherDocCount(1).buckets(b -> b.array(ListBuilder.of(StringTermsBucket.Builder::new).add(_1 -> _1.key("key_1").docCount(1).aggregations(MapBuilder.of("bar", avg1))).add(_1 -> _1.key("key_2").docCount(2).aggregations(MapBuilder.of("bar", avg2))).build())))._toAggregate();
    SearchResponse<Void> resp = new SearchResponse.Builder<Void>().aggregations("foo", aggregate).took(1).shards(_1 -> _1.successful(1).failed(0).total(1)).hits(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)).hits(Collections.emptyList())).timedOut(false).build();
    String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]}," + "\"aggregations\":{\"sterms#foo\":{\"buckets\":[" + "{\"avg#bar\":{\"value\":1.0},\"doc_count\":1,\"key\":\"key_1\"}," + "{\"avg#bar\":{\"value\":2.0},\"doc_count\":2,\"key\":\"key_2\"}" + "],\"sum_other_doc_count\":1}}}";
    assertEquals(json, toJson(resp));
    resp = fromJson(json, SearchResponse.createSearchResponseDeserializer(JsonpDeserializer.voidDeserializer()));
    StringTermsAggregate foo = resp.aggregations().get("foo").sterms();
    assertEquals(1, foo.sumOtherDocCount());
    assertEquals(1, foo.buckets().array().get(0).docCount());
    assertEquals("key_1", foo.buckets().array().get(0).key());
    assertEquals(1.0, foo.buckets().array().get(0).aggregations().get("bar").avg().value(), 0.01);
    assertEquals("key_2", foo.buckets().array().get(1).key());
    assertEquals(2.0, foo.buckets().array().get(1).aggregations().get("bar").avg().value(), 0.01);
}
Also used : StringTermsAggregate(org.opensearch.client.opensearch._types.aggregations.StringTermsAggregate) TotalHitsRelation(org.opensearch.client.opensearch.core.search.TotalHitsRelation) JsonpDeserializer(org.opensearch.client.json.JsonpDeserializer) ListBuilder(org.opensearch.client.util.ListBuilder) MapBuilder(org.opensearch.client.util.MapBuilder) Test(org.junit.Test) Aggregate(org.opensearch.client.opensearch._types.aggregations.Aggregate) AvgAggregate(org.opensearch.client.opensearch._types.aggregations.AvgAggregate) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) Collections(java.util.Collections) StringTermsBucket(org.opensearch.client.opensearch._types.aggregations.StringTermsBucket) StringTermsAggregate(org.opensearch.client.opensearch._types.aggregations.StringTermsAggregate) ListBuilder(org.opensearch.client.util.ListBuilder) MapBuilder(org.opensearch.client.util.MapBuilder) StringTermsBucket(org.opensearch.client.opensearch._types.aggregations.StringTermsBucket) StringTermsAggregate(org.opensearch.client.opensearch._types.aggregations.StringTermsAggregate) Aggregate(org.opensearch.client.opensearch._types.aggregations.Aggregate) AvgAggregate(org.opensearch.client.opensearch._types.aggregations.AvgAggregate) Test(org.junit.Test)

Aggregations

Collections (java.util.Collections)2 Test (org.junit.Test)2 Aggregate (co.elastic.clients.elasticsearch._types.aggregations.Aggregate)1 AvgAggregate (co.elastic.clients.elasticsearch._types.aggregations.AvgAggregate)1 StringTermsAggregate (co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate)1 StringTermsBucket (co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket)1 SearchResponse (co.elastic.clients.elasticsearch.core.SearchResponse)1 TotalHitsRelation (co.elastic.clients.elasticsearch.core.search.TotalHitsRelation)1 JsonpDeserializer (co.elastic.clients.json.JsonpDeserializer)1 ListBuilder (co.elastic.clients.util.ListBuilder)1 MapBuilder (co.elastic.clients.util.MapBuilder)1 JsonpDeserializer (org.opensearch.client.json.JsonpDeserializer)1 Aggregate (org.opensearch.client.opensearch._types.aggregations.Aggregate)1 AvgAggregate (org.opensearch.client.opensearch._types.aggregations.AvgAggregate)1 StringTermsAggregate (org.opensearch.client.opensearch._types.aggregations.StringTermsAggregate)1 StringTermsBucket (org.opensearch.client.opensearch._types.aggregations.StringTermsBucket)1 SearchResponse (org.opensearch.client.opensearch.core.SearchResponse)1 TotalHitsRelation (org.opensearch.client.opensearch.core.search.TotalHitsRelation)1 ListBuilder (org.opensearch.client.util.ListBuilder)1 MapBuilder (org.opensearch.client.util.MapBuilder)1