Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorting on nested field with unmapped #42451

Merged
merged 4 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test with a nested query ?


// 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