Skip to content

Commit

Permalink
Fix bug in NestedUtils.partitionByChildren() (#97970) (#97986)
Browse files Browse the repository at this point in the history
If multiple fields appeared between two child scopes, the following children
would be incorrectly assigned to the parent scope.
  • Loading branch information
romseygeek committed Jul 26, 2023
1 parent fbdb9cd commit ce1a67b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
22 changes: 10 additions & 12 deletions server/src/main/java/org/elasticsearch/search/NestedUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,18 @@ public static <T> Map<String, List<T>> partitionByChildren(
String currentInputName = pathFunction.apply(currentInput);
assert currentInputName.startsWith(scope);

// Find all the inputs that sort before the first child, and add them to the current scope entry
while (currentInputName.compareTo(currentChild) < 0) {
output.get(scope).add(currentInput);
if (inputIterator.hasNext() == false) {
return output;
}
currentInput = inputIterator.next();
currentInputName = pathFunction.apply(currentInput);
assert currentInputName.startsWith(scope);
}

// Iterate through all the children
while (currentChild != null) {
if (currentInputName.startsWith(currentChild + ".")) {
if (currentInputName.compareTo(currentChild) < 0) {
// If we sort before the current child, then we sit in the parent scope
output.get(scope).add(currentInput);
if (inputIterator.hasNext() == false) {
return output;
}
currentInput = inputIterator.next();
currentInputName = pathFunction.apply(currentInput);
assert currentInputName.startsWith(scope);
} else if (currentInputName.startsWith(currentChild + ".")) {
// If this input sits under the current child, add it to that child scope
// and then get the next input
output.get(currentChild).add(currentInput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ public void testPartitionByChild() {
"child1.grandchild",
"child1.grandchild2",
"child11",
"child12",
"child2.grandchild",
"frog"
);
Map<String, List<String>> partitioned = NestedUtils.partitionByChildren("", children, inputs, s -> s);
assertEquals(
org.elasticsearch.core.Map.of(
"",
org.elasticsearch.core.List.of("a", "b", "child11", "frog"),
org.elasticsearch.core.List.of("a", "b", "child11", "child12", "frog"),
"child1",
org.elasticsearch.core.List.of("child1.grandchild", "child1.grandchild2"),
"child2",
Expand Down

0 comments on commit ce1a67b

Please sign in to comment.