From 503a554a55be1d49b64bcaaaacf80bcfa214c581 Mon Sep 17 00:00:00 2001 From: Siva Date: Mon, 2 Jan 2023 13:26:16 +0530 Subject: [PATCH] Fix: Make ChildrenAggregate as a SingleBucketAggregate (#306) * make ChildrenAggregate as a SingleBucketAggregate Signed-off-by: Siva * update CHANGELOG.md Signed-off-by: Siva * add ChildAggregation test Signed-off-by: Siva * fix CheckStyle violations for ChildAggregation test Signed-off-by: Siva Signed-off-by: Siva --- CHANGELOG.md | 2 + .../aggregations/ChildrenAggregate.java | 8 +- .../aggregations/ChildrenAggregateBucket.java | 103 ------------------ .../opensearch/integTest/RequestTest.java | 65 +++++++++++ 4 files changed, 70 insertions(+), 108 deletions(-) delete mode 100644 java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/ChildrenAggregateBucket.java 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 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); - - } - -} 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..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 @@ -374,6 +374,27 @@ public void testSubAggregation() throws IOException { } + @Test + public void testChildAggregation() throws IOException { + + String index = "test_child"; + String question = "question"; + String answer = "answer"; + + 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)))); + + 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 +490,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