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

Add max_children limit to nested sort #33587

Merged
merged 10 commits into from
Oct 5, 2018
3 changes: 3 additions & 0 deletions docs/reference/search/request/sort.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ field support has a `nested` sort option with the following properties:
should match with in order for its field values to be taken into account
by sorting. Common case is to repeat the query / filter inside the
nested filter or query. By default no `nested_filter` is active.
`max_children`::
erayarslan marked this conversation as resolved.
Show resolved Hide resolved
The maximum number of children to consider per root document
when picking the sort value. Defaults to unlimited.
`nested`::
Same as top-level `nested` but applies to another nested path within the
current nested object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
final class DateField {
// no instance
private DateField() {}

// supported variables
static final String VALUE_VARIABLE = "value";
static final String EMPTY_VARIABLE = "empty";
static final String LENGTH_VARIABLE = "length";

// supported methods
static final String GETVALUE_METHOD = "getValue";
static final String ISEMPTY_METHOD = "isEmpty";
Expand All @@ -47,15 +47,15 @@ private DateField() {}
static final String MEDIAN_METHOD = "median";
static final String SUM_METHOD = "sum";
static final String COUNT_METHOD = "count";

// date-specific
static final String GET_YEAR_METHOD = "getYear";
static final String GET_MONTH_METHOD = "getMonth";
static final String GET_DAY_OF_MONTH_METHOD = "getDayOfMonth";
static final String GET_HOUR_OF_DAY_METHOD = "getHourOfDay";
static final String GET_MINUTES_METHOD = "getMinutes";
static final String GET_SECONDS_METHOD = "getSeconds";

static ValueSource getVariable(IndexFieldData<?> fieldData, String fieldName, String variable) {
switch (variable) {
case VALUE_VARIABLE:
Expand All @@ -68,7 +68,7 @@ static ValueSource getVariable(IndexFieldData<?> fieldData, String fieldName, St
throw new IllegalArgumentException("Member variable [" + variable + "] does not exist for date field [" + fieldName + "].");
}
}

static ValueSource getMethod(IndexFieldData<?> fieldData, String fieldName, String method) {
switch (method) {
case GETVALUE_METHOD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
final class NumericField {
// no instance
private NumericField() {}

// supported variables
static final String VALUE_VARIABLE = "value";
static final String EMPTY_VARIABLE = "empty";
static final String LENGTH_VARIABLE = "length";

// supported methods
static final String GETVALUE_METHOD = "getValue";
static final String ISEMPTY_METHOD = "isEmpty";
Expand All @@ -45,7 +45,7 @@ private NumericField() {}
static final String MEDIAN_METHOD = "median";
static final String SUM_METHOD = "sum";
static final String COUNT_METHOD = "count";

static ValueSource getVariable(IndexFieldData<?> fieldData, String fieldName, String variable) {
switch (variable) {
case VALUE_VARIABLE:
Expand All @@ -55,11 +55,11 @@ static ValueSource getVariable(IndexFieldData<?> fieldData, String fieldName, St
case LENGTH_VARIABLE:
return new CountMethodValueSource(fieldData);
default:
throw new IllegalArgumentException("Member variable [" + variable + "] does not exist for " +
throw new IllegalArgumentException("Member variable [" + variable + "] does not exist for " +
"numeric field [" + fieldName + "].");
}
}

static ValueSource getMethod(IndexFieldData<?> fieldData, String fieldName, String method) {
switch (method) {
case GETVALUE_METHOD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.search.MultiValueMode;
import org.elasticsearch.search.sort.NestedSortBuilder;

import java.io.IOException;

Expand Down Expand Up @@ -129,10 +130,12 @@ public static class Nested {

private final BitSetProducer rootFilter;
private final Query innerQuery;
private final NestedSortBuilder nestedSort;

public Nested(BitSetProducer rootFilter, Query innerQuery) {
public Nested(BitSetProducer rootFilter, Query innerQuery, NestedSortBuilder nestedSort) {
this.rootFilter = rootFilter;
this.innerQuery = innerQuery;
this.nestedSort = nestedSort;
}

public Query getInnerQuery() {
Expand All @@ -143,6 +146,8 @@ public BitSetProducer getRootFilter() {
return rootFilter;
}

public NestedSortBuilder getNestedSort() { return nestedSort; }

/**
* Get a {@link BitDocIdSet} that matches the root documents.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ protected SortedDocValues getSortedDocValues(LeafReaderContext context, String f
} else {
final BitSet rootDocs = nested.rootDocs(context);
final DocIdSetIterator innerDocs = nested.innerDocs(context);
selectedValues = sortMode.select(values, rootDocs, innerDocs);
final int maxChildren = nested.getNestedSort() != null ?
nested.getNestedSort().getMaxChildren() : Integer.MAX_VALUE;
selectedValues = sortMode.select(values, rootDocs, innerDocs, maxChildren);
}
if (sortMissingFirst(missingValue) || sortMissingLast(missingValue)) {
return selectedValues;
Expand Down Expand Up @@ -119,7 +121,8 @@ protected BinaryDocValues getBinaryDocValues(LeafReaderContext context, String f
} else {
final BitSet rootDocs = nested.rootDocs(context);
final DocIdSetIterator innerDocs = nested.innerDocs(context);
selectedValues = sortMode.select(values, missingBytes, rootDocs, innerDocs, context.reader().maxDoc());
final int maxChildren = nested.getNestedSort() != null ? nested.getNestedSort().getMaxChildren() : Integer.MAX_VALUE;
selectedValues = sortMode.select(values, missingBytes, rootDocs, innerDocs, context.reader().maxDoc(), maxChildren);
}
return selectedValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ protected NumericDocValues getNumericDocValues(LeafReaderContext context, String
} else {
final BitSet rootDocs = nested.rootDocs(context);
final DocIdSetIterator innerDocs = nested.innerDocs(context);
selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc());
final int maxChildren = nested.getNestedSort() != null ? nested.getNestedSort().getMaxChildren() : Integer.MAX_VALUE;
selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc(), maxChildren);
}
return selectedValues.getRawDoubleValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ protected NumericDocValues getNumericDocValues(LeafReaderContext context, String
} else {
final BitSet rootDocs = nested.rootDocs(context);
final DocIdSetIterator innerDocs = nested.innerDocs(context);
selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc());
final int maxChildren = nested.getNestedSort() != null ? nested.getNestedSort().getMaxChildren() : Integer.MAX_VALUE;
selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc(), maxChildren);
}
return selectedValues.getRawFloatValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ protected NumericDocValues getNumericDocValues(LeafReaderContext context, String
} else {
final BitSet rootDocs = nested.rootDocs(context);
final DocIdSetIterator innerDocs = nested.innerDocs(context);
selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc());
final int maxChildren = nested.getNestedSort() != null ? nested.getNestedSort().getMaxChildren() : Integer.MAX_VALUE;
selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc(), maxChildren);
}
return selectedValues;
}
Expand Down
Loading