Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/137047.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 137047
summary: Reject invalid `reverse_nested` aggs
area: Aggregations
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ protected AggregatorFactory doBuild(AggregationContext context, AggregatorFactor
throw new IllegalArgumentException("Reverse nested aggregation [" + name + "] can only be used inside a [nested] aggregation");
}

if (path != null) {
NestedObjectMapper currentParent = context.nestedScope().getObjectMapper();
if (currentParent != null) {
String parentPath = currentParent.fullPath();
if (parentPath.equals(path) == false && parentPath.startsWith(path + ".") == false) {
throw new IllegalArgumentException(
"Reverse nested path [" + path + "] is not a parent of the current nested scope [" + parentPath + "]"
);
}
}
}

NestedObjectMapper nestedMapper = null;
if (path != null) {
nestedMapper = context.nestedLookup().getNestedMappers().get(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,29 @@ public void testNestedUnderTerms() throws IOException {
protected List<ObjectMapper> objectMappers() {
return NestedAggregatorTests.MOCK_OBJECT_MAPPERS;
}

public void testErrorOnInvalidReverseNestedWithPath() throws IOException {
AggregationBuilder aggregationBuilder = nested("n1", NESTED_OBJECT).subAggregation(
nested("n2", NESTED_OBJECT + ".child").subAggregation(
reverseNested("r1").path(NESTED_OBJECT).subAggregation(reverseNested("r2").path(NESTED_OBJECT + ".child"))
)
);

try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = newRandomIndexWriterWithLogDocMergePolicy(directory)) {
// No docs needed
}
try (DirectoryReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
IllegalArgumentException e = assertThrows(
IllegalArgumentException.class,
() -> searchAndReduce(indexReader, new AggTestConfig(aggregationBuilder))
);
assertThat(
e.getMessage(),
equalTo("Reverse nested path [nested_object.child] is not a parent of the current nested scope [nested_object]")
);

}
}
}
}