Search in sources :

Example 1 with Aggregate

use of org.opensearch.client.opensearch._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)1 Test (org.junit.Test)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