Skip to content

Commit

Permalink
Fix sorting on nested field with unmapped (#42451)
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: #33644
  • Loading branch information
matriv authored May 24, 2019
1 parent 3907a6d commit 631142d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,10 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
return SORT_DOC;
}
} else {
boolean isUnmapped = false;
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 == false) {
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,22 @@ 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);

// nestedQuery
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.addSort(SortBuilders.fieldSort("nested.foo").unmappedType("keyword")
.setNestedSort(new NestedSortBuilder("nested").setFilter(QueryBuilders.termQuery("nested.foo", "abc"))))
.get();
assertNoFailures(searchResponse);
}

public void testSortMVField() throws Exception {
Expand Down

0 comments on commit 631142d

Please sign in to comment.