Skip to content
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 @@ -332,10 +332,7 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
Nested nested = null;
if (isUnmapped == false) {
if (nestedSort != null) {
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on last level of nested sort");
}
validateMaxChildrenExistOnlyInTopLevelNestedSort(context, nestedSort);
nested = resolveNested(context, nestedSort);
} else {
validateMissingNestedPath(context, fieldName);
Expand All @@ -362,6 +359,18 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
return new SortFieldAndFormat(field, fieldType.docValueFormat(null, null));
}

/**
* Throws an exception if max children is not located at top level nested sort.
*/
static void validateMaxChildrenExistOnlyInTopLevelNestedSort(QueryShardContext context, NestedSortBuilder nestedSort) {
for (NestedSortBuilder child = nestedSort.getNestedSort(); child != null; child = child.getNestedSort()) {
if (child.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on top level of nested sort");
}
}
}

/**
* Throws an exception if the provided <code>field</code> requires a nested context.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.query.QueryShardException;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;

Expand All @@ -64,6 +63,7 @@
import java.util.Objects;

import static org.elasticsearch.search.sort.FieldSortBuilder.validateMissingNestedPath;
import static org.elasticsearch.search.sort.FieldSortBuilder.validateMaxChildrenExistOnlyInTopLevelNestedSort;
import static org.elasticsearch.search.sort.NestedSortBuilder.NESTED_FIELD;

/**
Expand Down Expand Up @@ -536,10 +536,7 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {

Nested nested = null;
if (nestedSort != null) {
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on last level of nested sort");
}
validateMaxChildrenExistOnlyInTopLevelNestedSort(context, nestedSort);
nested = resolveNested(context, nestedSort);
} else {
validateMissingNestedPath(context, fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.Objects;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.search.sort.FieldSortBuilder.validateMaxChildrenExistOnlyInTopLevelNestedSort;
import static org.elasticsearch.search.sort.NestedSortBuilder.NESTED_FIELD;

/**
Expand Down Expand Up @@ -242,10 +243,7 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {

Nested nested = null;
if (nestedSort != null) {
if (nestedSort.getNestedSort() != null && nestedSort.getMaxChildren() != Integer.MAX_VALUE) {
throw new QueryShardException(context,
"max_children is only supported on last level of nested sort");
}
validateMaxChildrenExistOnlyInTopLevelNestedSort(context, nestedSort);
nested = resolveNested(context, nestedSort);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,20 @@ public void testNestedSort() throws IOException, InterruptedException, Execution
.endObject()
.endObject()
.endObject()
.startObject("bar")
.field("type", "nested")
.startObject("properties")
.startObject("foo")
.field("type", "text")
.field("fielddata", true)
.startObject("fields")
.startObject("sub")
.field("type", "keyword")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
Expand Down Expand Up @@ -1473,6 +1487,22 @@ public void testNestedSort() throws IOException, InterruptedException, Execution
assertThat(hits[0].getSortValues()[0], is("bar"));
assertThat(hits[1].getSortValues()[0], is("abc"));

{
SearchPhaseExecutionException exc = expectThrows(SearchPhaseExecutionException.class,
() -> client().prepareSearch()
.setQuery(matchAllQuery())
.addSort(SortBuilders
.fieldSort("nested.bar.foo")
.setNestedSort(new NestedSortBuilder("nested")
.setNestedSort(new NestedSortBuilder("nested.bar")
.setMaxChildren(1)))
.order(SortOrder.DESC))
.get()
);
assertThat(exc.toString(),
containsString("max_children is only supported on top level of nested sort"));
}

// We sort on nested sub field
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
Expand Down