@@ -366,6 +366,7 @@ private static NumericType resolveNumericType(String value) {
366
366
367
367
@ Override
368
368
public SortFieldAndFormat build (QueryShardContext context ) throws IOException {
369
+ boolean isUnmapped = false ;
369
370
if (DOC_FIELD_NAME .equals (fieldName )) {
370
371
if (order == SortOrder .DESC ) {
371
372
return SORT_DOC_REVERSE ;
@@ -375,6 +376,7 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
375
376
} else {
376
377
MappedFieldType fieldType = context .fieldMapper (fieldName );
377
378
if (fieldType == null ) {
379
+ isUnmapped = true ;
378
380
if (unmappedType != null ) {
379
381
fieldType = context .getMapperService ().unmappedFieldType (unmappedType );
380
382
} else {
@@ -392,20 +394,22 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
392
394
localSortMode = reverse ? MultiValueMode .MAX : MultiValueMode .MIN ;
393
395
}
394
396
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 );
404
412
}
405
- // new nested sorts takes priority
406
- nested = resolveNested (context , nestedSort );
407
- } else {
408
- nested = resolveNested (context , nestedPath , nestedFilter );
409
413
}
410
414
411
415
IndexFieldData <?> fieldData = context .getForField (fieldType );
0 commit comments