Skip to content

Commit ce1a67b

Browse files
authored
Fix bug in NestedUtils.partitionByChildren() (#97970) (#97986)
If multiple fields appeared between two child scopes, the following children would be incorrectly assigned to the parent scope.
1 parent fbdb9cd commit ce1a67b

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

server/src/main/java/org/elasticsearch/search/NestedUtils.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,18 @@ public static <T> Map<String, List<T>> partitionByChildren(
7171
String currentInputName = pathFunction.apply(currentInput);
7272
assert currentInputName.startsWith(scope);
7373

74-
// Find all the inputs that sort before the first child, and add them to the current scope entry
75-
while (currentInputName.compareTo(currentChild) < 0) {
76-
output.get(scope).add(currentInput);
77-
if (inputIterator.hasNext() == false) {
78-
return output;
79-
}
80-
currentInput = inputIterator.next();
81-
currentInputName = pathFunction.apply(currentInput);
82-
assert currentInputName.startsWith(scope);
83-
}
84-
8574
// Iterate through all the children
8675
while (currentChild != null) {
87-
if (currentInputName.startsWith(currentChild + ".")) {
76+
if (currentInputName.compareTo(currentChild) < 0) {
77+
// If we sort before the current child, then we sit in the parent scope
78+
output.get(scope).add(currentInput);
79+
if (inputIterator.hasNext() == false) {
80+
return output;
81+
}
82+
currentInput = inputIterator.next();
83+
currentInputName = pathFunction.apply(currentInput);
84+
assert currentInputName.startsWith(scope);
85+
} else if (currentInputName.startsWith(currentChild + ".")) {
8886
// If this input sits under the current child, add it to that child scope
8987
// and then get the next input
9088
output.get(currentChild).add(currentInput);

server/src/test/java/org/elasticsearch/search/NestedUtilsTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ public void testPartitionByChild() {
2323
"child1.grandchild",
2424
"child1.grandchild2",
2525
"child11",
26+
"child12",
2627
"child2.grandchild",
2728
"frog"
2829
);
2930
Map<String, List<String>> partitioned = NestedUtils.partitionByChildren("", children, inputs, s -> s);
3031
assertEquals(
3132
org.elasticsearch.core.Map.of(
3233
"",
33-
org.elasticsearch.core.List.of("a", "b", "child11", "frog"),
34+
org.elasticsearch.core.List.of("a", "b", "child11", "child12", "frog"),
3435
"child1",
3536
org.elasticsearch.core.List.of("child1.grandchild", "child1.grandchild2"),
3637
"child2",

0 commit comments

Comments
 (0)