Skip to content

Commit

Permalink
[Backport] [1.x] Fix: Make ChildrenAggregate as a SingleBucketAggrega…
Browse files Browse the repository at this point in the history
…te (#306) (#310)

Signed-off-by: Siva <ychnagasivareddy@gmail.com>

Signed-off-by: Siva <ychnagasivareddy@gmail.com>
  • Loading branch information
sivaGH3 authored Jan 4, 2023
1 parent 116a540 commit 2e715fa
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
// typedef: _types.aggregations.ChildrenAggregate

@JsonpDeserializable
public class ChildrenAggregate extends MultiBucketAggregateBase<ChildrenAggregateBucket> implements AggregateVariant {
public class ChildrenAggregate extends SingleBucketAggregateBase implements AggregateVariant {
// ---------------------------------------------------------------------------------------------

private ChildrenAggregate(Builder builder) {
Expand All @@ -73,7 +73,7 @@ public Aggregate.Kind _aggregateKind() {
* Builder for {@link ChildrenAggregate}.
*/

public static class Builder extends MultiBucketAggregateBase.AbstractBuilder<ChildrenAggregateBucket, Builder>
public static class Builder extends SingleBucketAggregateBase.AbstractBuilder<Builder>
implements
ObjectBuilder<ChildrenAggregate> {
@Override
Expand All @@ -89,7 +89,6 @@ protected Builder self() {
*/
public ChildrenAggregate build() {
_checkSingleUse();
super.tBucketSerializer(null);

return new ChildrenAggregate(this);
}
Expand All @@ -104,8 +103,7 @@ public ChildrenAggregate build() {
.lazy(Builder::new, ChildrenAggregate::setupChildrenAggregateDeserializer);

protected static void setupChildrenAggregateDeserializer(ObjectDeserializer<ChildrenAggregate.Builder> op) {
MultiBucketAggregateBase.setupMultiBucketAggregateBaseDeserializer(op, ChildrenAggregateBucket._DESERIALIZER);

SingleBucketAggregateBase.setupSingleBucketAggregateBaseDeserializer(op);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.opensearch.client.opensearch.core.GetResponse;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.MsearchResponse;
import org.opensearch.client.opensearch.core.SearchRequest;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.opensearch.core.bulk.OperationType;
import org.opensearch.client.opensearch.core.msearch.RequestItem;
Expand Down Expand Up @@ -318,6 +319,27 @@ public void testSearchAggregation() 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<Void> searchResponse = highLevelClient().search(searchRequest, Void.class);

assertEquals(2, searchResponse.aggregations().get(answer).children().docCount());
}

@Test
public void testGetMapping() throws Exception {
// See also VariantsTest.testNestedTaggedUnionWithDefaultTag()
Expand Down Expand Up @@ -407,4 +429,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);
}
}
}

0 comments on commit 2e715fa

Please sign in to comment.