Skip to content

Commit

Permalink
Fix sorting on nested field with unmapped
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matriv committed May 23, 2019
1 parent 13dc1cf commit 162ff3a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ private static NumericType resolveNumericType(String value) {

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
boolean isUnmapped = false;
if (DOC_FIELD_NAME.equals(fieldName)) {
if (order == SortOrder.DESC) {
return SORT_DOC_REVERSE;
Expand All @@ -375,6 +376,7 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
} else {
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType == null) {
isUnmapped = true;
if (unmappedType != null) {
fieldType = context.getMapperService().unmappedFieldType(unmappedType);
} else {
Expand All @@ -392,20 +394,22 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
localSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
}

final Nested nested;
if (nestedSort != null) {
if (context.indexVersionCreated().before(Version.V_6_5_0) && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on v6.5.0 or higher");
}
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on last level of nested sort");
Nested nested = null;
if (!isUnmapped) {
if (nestedSort != null) {
if (context.indexVersionCreated().before(Version.V_6_5_0) && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on v6.5.0 or higher");
}
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on last level of nested sort");
}
// new nested sorts takes priority
nested = resolveNested(context, nestedSort);
} else {
nested = resolveNested(context, nestedPath, nestedFilter);
}
// new nested sorts takes priority
nested = resolveNested(context, nestedSort);
} else {
nested = resolveNested(context, nestedPath, nestedFilter);
}

IndexFieldData<?> fieldData = context.getForField(fieldType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,14 @@ public void testIgnoreUnmapped() throws Exception {
.addSort(SortBuilders.fieldSort("kkk").unmappedType("keyword"))
.get();
assertNoFailures(searchResponse);

// nested field
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.addSort(SortBuilders.fieldSort("nested.foo").unmappedType("keyword")
.setNestedSort(new NestedSortBuilder("nested").setNestedSort(new NestedSortBuilder("nested.foo"))))
.get();
assertNoFailures(searchResponse);
}

public void testSortMVField() throws Exception {
Expand Down

0 comments on commit 162ff3a

Please sign in to comment.