From 574454d6567229747388a65c6e81754ec867df38 Mon Sep 17 00:00:00 2001 From: Siva Date: Wed, 28 Dec 2022 17:15:37 +0530 Subject: [PATCH 1/4] make ChildrenAggregate as a SingleBucketAggregate Signed-off-by: Siva --- .../aggregations/ChildrenAggregate.java | 8 +- .../aggregations/ChildrenAggregateBucket.java | 103 ------------------ 2 files changed, 3 insertions(+), 108 deletions(-) delete mode 100644 java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregateBucket.java diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregate.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregate.java index 6c456b0ecd..d38c40004c 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregate.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregate.java @@ -47,7 +47,7 @@ // typedef: _types.aggregations.ChildrenAggregate @JsonpDeserializable -public class ChildrenAggregate extends MultiBucketAggregateBase implements AggregateVariant { +public class ChildrenAggregate extends SingleBucketAggregateBase implements AggregateVariant { // --------------------------------------------------------------------------------------------- private ChildrenAggregate(Builder builder) { @@ -73,7 +73,7 @@ public Aggregate.Kind _aggregateKind() { * Builder for {@link ChildrenAggregate}. */ - public static class Builder extends MultiBucketAggregateBase.AbstractBuilder + public static class Builder extends SingleBucketAggregateBase.AbstractBuilder implements ObjectBuilder { @Override @@ -89,7 +89,6 @@ protected Builder self() { */ public ChildrenAggregate build() { _checkSingleUse(); - super.tBucketSerializer(null); return new ChildrenAggregate(this); } @@ -104,8 +103,7 @@ public ChildrenAggregate build() { .lazy(Builder::new, ChildrenAggregate::setupChildrenAggregateDeserializer); protected static void setupChildrenAggregateDeserializer(ObjectDeserializer op) { - MultiBucketAggregateBase.setupMultiBucketAggregateBaseDeserializer(op, ChildrenAggregateBucket._DESERIALIZER); - + SingleBucketAggregateBase.setupSingleBucketAggregateBaseDeserializer(op); } } diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregateBucket.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregateBucket.java deleted file mode 100644 index 65b75cd29d..0000000000 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregateBucket.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -//---------------------------------------------------- -// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST. -//---------------------------------------------------- - -package org.opensearch.client.opensearch._types.aggregations; - -import org.opensearch.client.json.JsonpDeserializable; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.ObjectBuilderDeserializer; -import org.opensearch.client.json.ObjectDeserializer; -import org.opensearch.client.util.ObjectBuilder; - -import java.util.function.Function; - -// typedef: _types.aggregations.ChildrenAggregateBucket - -@JsonpDeserializable -public class ChildrenAggregateBucket extends MultiBucketBase { - // --------------------------------------------------------------------------------------------- - - private ChildrenAggregateBucket(Builder builder) { - super(builder); - - } - - public static ChildrenAggregateBucket of(Function> fn) { - return fn.apply(new Builder()).build(); - } - - // --------------------------------------------------------------------------------------------- - - /** - * Builder for {@link ChildrenAggregateBucket}. - */ - - public static class Builder extends MultiBucketBase.AbstractBuilder - implements - ObjectBuilder { - @Override - protected Builder self() { - return this; - } - - /** - * Builds a {@link ChildrenAggregateBucket}. - * - * @throws NullPointerException - * if some of the required fields are null. - */ - public ChildrenAggregateBucket build() { - _checkSingleUse(); - - return new ChildrenAggregateBucket(this); - } - } - - // --------------------------------------------------------------------------------------------- - - /** - * Json deserializer for {@link ChildrenAggregateBucket} - */ - public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer - .lazy(Builder::new, ChildrenAggregateBucket::setupChildrenAggregateBucketDeserializer); - - protected static void setupChildrenAggregateBucketDeserializer( - ObjectDeserializer op) { - setupMultiBucketBaseDeserializer(op); - - } - -} From 46e0ce28bb297cf7c52c60664b11ea5e2b794652 Mon Sep 17 00:00:00 2001 From: Siva Date: Wed, 28 Dec 2022 17:44:15 +0530 Subject: [PATCH 2/4] update CHANGELOG.md Signed-off-by: Siva --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff6a78fdc0..1631fe0e4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,9 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed - Removed support for unsupported dynamic_templates in bulk ([#276](https://github.com/opensearch-project/opensearch-java/pull/276)) + ### Fixed +- Make ChildrenAggregate as a SingleBucketAggregate ([#306](https://github.com/opensearch-project/opensearch-java/pull/306)) ### Security From 1cdba1678e14b1ed9240ec84fc4473080bdd4b9d Mon Sep 17 00:00:00 2001 From: Siva Date: Thu, 29 Dec 2022 12:25:06 +0530 Subject: [PATCH 3/4] add ChildAggregation test Signed-off-by: Siva --- .../opensearch/integTest/RequestTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java index 4d6ae2d739..d098ed3ff0 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java @@ -374,6 +374,28 @@ public void testSubAggregation() throws IOException { } + @Test + public void testChildAggregation() throws IOException { + + String index = "test_child"; + String question = "question"; + String answer = "answer"; + Map> relations = Collections.singletonMap(question, Collections.singletonList(answer)); + + highLevelClient().indices().create(c -> c.index(index).mappings(m -> m.properties("join", p -> p.join(j -> j.relations(relations))))); + + highLevelClient().index(i -> i.index(index).id("1").document(new Question("exists")).refresh(Refresh.True)); + + highLevelClient().index(i -> i.index(index).id("2").routing("1").document(new Answer("true", "1")).refresh(Refresh.True)); + highLevelClient().index(i -> i.index(index).id("3").routing("1").document(new Answer("false", "1")).refresh(Refresh.True)); + + SearchRequest searchRequest = SearchRequest.of(r -> r.index(index).size(0).aggregations(answer, a -> a.children(c -> c.type(answer)))); + + SearchResponse searchResponse = highLevelClient().search(searchRequest, Void.class); + + assertEquals(2, searchResponse.aggregations().get(answer).children().docCount()); + } + @Test public void testGetMapping() throws Exception { // See also VariantsTest.testNestedTaggedUnionWithDefaultTag() @@ -469,4 +491,48 @@ public void setPrice(double price) { this.price = price; } } + + public static class Join { + public String name; + public String parent; + + Join() {} + + Join(String name) { + this.name = name; + } + + Join(String name, String parent) { + this.name = name; + this.parent = parent; + } + } + + public static class Question { + public String title; + public Join join; + + Question() {} + + Question(String title) { + this.title = title; + this.join = new Join("question"); + } + } + + public static class Answer { + public String body; + public Join join; + + Answer() {} + + Answer(String body) { + this.body = body; + } + + Answer(String body, String parent) { + this.body = body; + this.join = new Join("answer", parent); + } + } } \ No newline at end of file From 3ba78317bf3ff73a38a4525325cfe09d4522354a Mon Sep 17 00:00:00 2001 From: Siva Date: Thu, 29 Dec 2022 21:25:08 +0530 Subject: [PATCH 4/4] fix CheckStyle violations for ChildAggregation test Signed-off-by: Siva --- .../client/opensearch/integTest/RequestTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java index d098ed3ff0..4196884453 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/RequestTest.java @@ -380,16 +380,15 @@ public void testChildAggregation() throws IOException { String index = "test_child"; String question = "question"; String answer = "answer"; - Map> relations = Collections.singletonMap(question, Collections.singletonList(answer)); - - highLevelClient().indices().create(c -> c.index(index).mappings(m -> m.properties("join", p -> p.join(j -> j.relations(relations))))); + highLevelClient().indices().create(c -> c.index(index).mappings(m -> m.properties("join", p -> p + .join(j -> j.relations(Collections.singletonMap(question, Collections.singletonList(answer))))))); highLevelClient().index(i -> i.index(index).id("1").document(new Question("exists")).refresh(Refresh.True)); - highLevelClient().index(i -> i.index(index).id("2").routing("1").document(new Answer("true", "1")).refresh(Refresh.True)); highLevelClient().index(i -> i.index(index).id("3").routing("1").document(new Answer("false", "1")).refresh(Refresh.True)); - SearchRequest searchRequest = SearchRequest.of(r -> r.index(index).size(0).aggregations(answer, a -> a.children(c -> c.type(answer)))); + SearchRequest searchRequest = SearchRequest.of(r -> r.index(index).size(0) + .aggregations(answer, a -> a.children(c -> c.type(answer)))); SearchResponse searchResponse = highLevelClient().search(searchRequest, Void.class);