Skip to content

Commit 162ff3a

Browse files
committed
Fix sorting on nested field with unmapped
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: elastic#33644
1 parent 13dc1cf commit 162ff3a

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

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

+17-13
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ private static NumericType resolveNumericType(String value) {
366366

367367
@Override
368368
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
369+
boolean isUnmapped = false;
369370
if (DOC_FIELD_NAME.equals(fieldName)) {
370371
if (order == SortOrder.DESC) {
371372
return SORT_DOC_REVERSE;
@@ -375,6 +376,7 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
375376
} else {
376377
MappedFieldType fieldType = context.fieldMapper(fieldName);
377378
if (fieldType == null) {
379+
isUnmapped = true;
378380
if (unmappedType != null) {
379381
fieldType = context.getMapperService().unmappedFieldType(unmappedType);
380382
} else {
@@ -392,20 +394,22 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
392394
localSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
393395
}
394396

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

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

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

+8
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,14 @@ 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);
906914
}
907915

908916
public void testSortMVField() throws Exception {

0 commit comments

Comments
 (0)