Skip to content

Commit be513ce

Browse files
committed
Fix sorting on nested field with unmapped (#42451)
Previously sorting on a missing nested field would fail with an Exception: `[nested_field] failed to find nested object under path [nested_path]` despite `unmapped_type` being set on the query. Fixes: #33644 (cherry picked from commit 631142d)
1 parent 9797257 commit be513ce

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,10 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
377377
return SORT_DOC;
378378
}
379379
} else {
380+
boolean isUnmapped = false;
380381
MappedFieldType fieldType = context.fieldMapper(fieldName);
381382
if (fieldType == null) {
383+
isUnmapped = true;
382384
if (unmappedType != null) {
383385
fieldType = context.getMapperService().unmappedFieldType(unmappedType);
384386
} else {
@@ -396,20 +398,22 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
396398
localSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
397399
}
398400

399-
final Nested nested;
400-
if (nestedSort != null) {
401-
if (context.indexVersionCreated().before(Version.V_6_5_0) && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
402-
throw new QueryShardException(context,
403-
"max_children is only supported on v6.5.0 or higher");
404-
}
405-
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
406-
throw new QueryShardException(context,
407-
"max_children is only supported on last level of nested sort");
401+
Nested nested = null;
402+
if (isUnmapped == false) {
403+
if (nestedSort != null) {
404+
if (context.indexVersionCreated().before(Version.V_6_5_0) && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
405+
throw new QueryShardException(context,
406+
"max_children is only supported on v6.5.0 or higher");
407+
}
408+
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
409+
throw new QueryShardException(context,
410+
"max_children is only supported on last level of nested sort");
411+
}
412+
// new nested sorts takes priority
413+
nested = resolveNested(context, nestedSort);
414+
} else {
415+
nested = resolveNested(context, nestedPath, nestedFilter);
408416
}
409-
// new nested sorts takes priority
410-
nested = resolveNested(context, nestedSort);
411-
} else {
412-
nested = resolveNested(context, nestedPath, nestedFilter);
413417
}
414418

415419
IndexFieldData<?> fieldData = context.getForField(fieldType);

server/src/test/java/org/elasticsearch/search/sort/FieldSortIT.java

+16
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,22 @@ public void testIgnoreUnmapped() throws Exception {
903903
.addSort(SortBuilders.fieldSort("kkk").unmappedType("keyword"))
904904
.get();
905905
assertNoFailures(searchResponse);
906+
907+
// nested field
908+
searchResponse = client().prepareSearch()
909+
.setQuery(matchAllQuery())
910+
.addSort(SortBuilders.fieldSort("nested.foo").unmappedType("keyword")
911+
.setNestedSort(new NestedSortBuilder("nested").setNestedSort(new NestedSortBuilder("nested.foo"))))
912+
.get();
913+
assertNoFailures(searchResponse);
914+
915+
// nestedQuery
916+
searchResponse = client().prepareSearch()
917+
.setQuery(matchAllQuery())
918+
.addSort(SortBuilders.fieldSort("nested.foo").unmappedType("keyword")
919+
.setNestedSort(new NestedSortBuilder("nested").setFilter(QueryBuilders.termQuery("nested.foo", "abc"))))
920+
.get();
921+
assertNoFailures(searchResponse);
906922
}
907923

908924
public void testSortMVField() throws Exception {

0 commit comments

Comments
 (0)