Skip to content

Commit a57d85b

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 5f12c10 commit a57d85b

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
@@ -311,8 +311,10 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
311311
return SORT_DOC;
312312
}
313313
} else {
314+
boolean isUnmapped = false;
314315
MappedFieldType fieldType = context.fieldMapper(fieldName);
315316
if (fieldType == null) {
317+
isUnmapped = true;
316318
if (unmappedType != null) {
317319
fieldType = context.getMapperService().unmappedFieldType(unmappedType);
318320
} else {
@@ -330,20 +332,22 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
330332
localSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
331333
}
332334

333-
final Nested nested;
334-
if (nestedSort != null) {
335-
if (context.indexVersionCreated().before(Version.V_6_5_0) && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
336-
throw new QueryShardException(context,
337-
"max_children is only supported on v6.5.0 or higher");
338-
}
339-
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
340-
throw new QueryShardException(context,
341-
"max_children is only supported on last level of nested sort");
335+
Nested nested = null;
336+
if (isUnmapped == false) {
337+
if (nestedSort != null) {
338+
if (context.indexVersionCreated().before(Version.V_6_5_0) && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
339+
throw new QueryShardException(context,
340+
"max_children is only supported on v6.5.0 or higher");
341+
}
342+
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
343+
throw new QueryShardException(context,
344+
"max_children is only supported on last level of nested sort");
345+
}
346+
// new nested sorts takes priority
347+
nested = resolveNested(context, nestedSort);
348+
} else {
349+
nested = resolveNested(context, nestedPath, nestedFilter);
342350
}
343-
// new nested sorts takes priority
344-
nested = resolveNested(context, nestedSort);
345-
} else {
346-
nested = resolveNested(context, nestedPath, nestedFilter);
347351
}
348352

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

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

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

906922
public void testSortMVField() throws Exception {

0 commit comments

Comments
 (0)