Skip to content

Commit

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

Signed-off-by: Siva <ychnagasivareddy@gmail.com>
  • Loading branch information
sivaGH3 committed Jan 5, 2023
1 parent a395e4f commit f09c337
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed

### Fixed
- Make ChildrenAggregate as a SingleBucketAggregate ([#306](https://github.com/opensearch-project/opensearch-java/pull/306))

### Security

Expand Down
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 @@ -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<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 @@ -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);
}
}
}

0 comments on commit f09c337

Please sign in to comment.