Skip to content

Commit a9b1251

Browse files
romseygeekfzowl
authored andcommitted
Enable sort optimizations on fields with sparse indexes (elastic#137009)
Sort optimizations are currently only enabled on numeric fields with a backing points index. This re-enables the optimizations for numeric fields with no points index but with skippers on doc values.
1 parent 6b9d383 commit a9b1251

File tree

21 files changed

+91
-66
lines changed

21 files changed

+91
-66
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
476476
(dv, n) -> {
477477
throw new UnsupportedOperationException();
478478
},
479-
indexType.hasPoints()
479+
indexType
480480
).build(cache, breakerService);
481481
return new ScaledFloatIndexFieldData(scaledValues, scalingFactor, ScaledFloatDocValuesField::new);
482482
};
@@ -786,8 +786,8 @@ protected boolean sortRequiresCustomComparator() {
786786
}
787787

788788
@Override
789-
protected boolean isIndexed() {
790-
return false; // We don't know how to take advantage of the index with half floats anyway
789+
protected IndexType indexType() {
790+
return IndexType.NONE; // We don't know how to take advantage of the index with half floats anyway
791791
}
792792

793793
@Override

plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public String typeName() {
8585
@Override
8686
public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) {
8787
failIfNoDocValues();
88-
return new SortedNumericIndexFieldData.Builder(name(), NumericType.LONG, Murmur3DocValueField::new, false);
88+
return new SortedNumericIndexFieldData.Builder(name(), NumericType.LONG, Murmur3DocValueField::new, indexType);
8989
}
9090

9191
@Override

server/src/main/java/org/elasticsearch/index/fielddata/BooleanScriptFieldData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.LeafReaderContext;
1313
import org.elasticsearch.ExceptionsHelper;
1414
import org.elasticsearch.index.fielddata.plain.LeafLongFieldData;
15+
import org.elasticsearch.index.mapper.IndexType;
1516
import org.elasticsearch.indices.breaker.CircuitBreakerService;
1617
import org.elasticsearch.script.BooleanFieldScript;
1718
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
@@ -91,8 +92,8 @@ protected boolean sortRequiresCustomComparator() {
9192
}
9293

9394
@Override
94-
protected boolean isIndexed() {
95-
return false;
95+
protected IndexType indexType() {
96+
return IndexType.NONE;
9697
}
9798

9899
public static class BooleanScriptLeafFieldData extends LeafLongFieldData {

server/src/main/java/org/elasticsearch/index/fielddata/DateScriptFieldData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.LeafReaderContext;
1313
import org.elasticsearch.ExceptionsHelper;
1414
import org.elasticsearch.index.fielddata.plain.LeafLongFieldData;
15+
import org.elasticsearch.index.mapper.IndexType;
1516
import org.elasticsearch.indices.breaker.CircuitBreakerService;
1617
import org.elasticsearch.script.DateFieldScript;
1718
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
@@ -91,8 +92,8 @@ protected boolean sortRequiresCustomComparator() {
9192
}
9293

9394
@Override
94-
protected boolean isIndexed() {
95-
return false;
95+
protected IndexType indexType() {
96+
return IndexType.NONE;
9697
}
9798

9899
public static class DateScriptLeafFieldData extends LeafLongFieldData {

server/src/main/java/org/elasticsearch/index/fielddata/DoubleScriptFieldData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.LeafReaderContext;
1313
import org.elasticsearch.ExceptionsHelper;
1414
import org.elasticsearch.index.fielddata.plain.LeafDoubleFieldData;
15+
import org.elasticsearch.index.mapper.IndexType;
1516
import org.elasticsearch.indices.breaker.CircuitBreakerService;
1617
import org.elasticsearch.script.DoubleFieldScript;
1718
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
@@ -91,8 +92,8 @@ protected boolean sortRequiresCustomComparator() {
9192
}
9293

9394
@Override
94-
protected boolean isIndexed() {
95-
return false;
95+
protected IndexType indexType() {
96+
return IndexType.NONE;
9697
}
9798

9899
public static class DoubleScriptLeafFieldData extends LeafDoubleFieldData {

server/src/main/java/org/elasticsearch/index/fielddata/IndexNumericFieldData.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.index.fielddata.fieldcomparator.HalfFloatValuesComparatorSource;
2424
import org.elasticsearch.index.fielddata.fieldcomparator.IntValuesComparatorSource;
2525
import org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource;
26+
import org.elasticsearch.index.mapper.IndexType;
2627
import org.elasticsearch.search.DocValueFormat;
2728
import org.elasticsearch.search.MultiValueMode;
2829
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
@@ -105,7 +106,7 @@ public final SortField sortField(
105106
|| targetNumericType != getNumericType();
106107
if (sortRequiresCustomComparator() || requiresCustomComparator) {
107108
SortField sortField = new SortField(getFieldName(), source, reverse);
108-
sortField.setOptimizeSortWithPoints(requiresCustomComparator == false && isIndexed());
109+
sortField.setOptimizeSortWithPoints(requiresCustomComparator == false && canUseOptimizedSort(indexType()));
109110
return sortField;
110111
}
111112

@@ -114,10 +115,14 @@ public final SortField sortField(
114115
: SortedNumericSelector.Type.MIN;
115116
SortField sortField = new SortedNumericSortField(getFieldName(), getNumericType().sortFieldType, reverse, selectorType);
116117
sortField.setMissingValue(source.missingObject(missingValue, reverse));
117-
sortField.setOptimizeSortWithPoints(isIndexed());
118+
sortField.setOptimizeSortWithPoints(canUseOptimizedSort(indexType()));
118119
return sortField;
119120
}
120121

122+
private static boolean canUseOptimizedSort(IndexType indexType) {
123+
return indexType.hasPoints() || indexType.hasDocValuesSkipper();
124+
}
125+
121126
/**
122127
* Should sorting use a custom comparator source vs. rely on a Lucene {@link SortField}. Using a Lucene {@link SortField} when possible
123128
* is important because index sorting cannot be configured with a custom comparator, and because it gives better performance by
@@ -129,7 +134,7 @@ public final SortField sortField(
129134
/**
130135
* Return true if, and only if the field is indexed with points that match the content of doc values.
131136
*/
132-
protected abstract boolean isIndexed();
137+
protected abstract IndexType indexType();
133138

134139
@Override
135140
public final SortField sortField(Object missingValue, MultiValueMode sortMode, Nested nested, boolean reverse) {

server/src/main/java/org/elasticsearch/index/fielddata/LongScriptFieldData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.LeafReaderContext;
1313
import org.elasticsearch.ExceptionsHelper;
1414
import org.elasticsearch.index.fielddata.plain.LeafLongFieldData;
15+
import org.elasticsearch.index.mapper.IndexType;
1516
import org.elasticsearch.indices.breaker.CircuitBreakerService;
1617
import org.elasticsearch.script.LongFieldScript;
1718
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
@@ -91,8 +92,8 @@ protected boolean sortRequiresCustomComparator() {
9192
}
9293

9394
@Override
94-
protected boolean isIndexed() {
95-
return false;
95+
protected IndexType indexType() {
96+
return IndexType.NONE;
9697
}
9798

9899
public static class LongScriptLeafFieldData extends LeafLongFieldData {

server/src/main/java/org/elasticsearch/index/fielddata/plain/SortedDoublesIndexFieldData.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.index.fielddata.NumericDoubleValues;
2626
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
2727
import org.elasticsearch.index.fielddata.SortedNumericLongValues;
28+
import org.elasticsearch.index.mapper.IndexType;
2829
import org.elasticsearch.indices.breaker.CircuitBreakerService;
2930
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
3031
import org.elasticsearch.script.field.ToScriptFieldFactory;
@@ -44,47 +45,47 @@ public static class Builder implements IndexFieldData.Builder {
4445
private final NumericType numericType;
4546
private final ValuesSourceType valuesSourceType;
4647
protected final ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory;
47-
private final boolean indexed;
48+
private final IndexType indexType;
4849

4950
public Builder(
5051
String name,
5152
NumericType numericType,
5253
ValuesSourceType valuesSourceType,
5354
ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory,
54-
boolean indexed
55+
IndexType indexType
5556
) {
5657
this.name = name;
5758
this.numericType = numericType;
5859
this.valuesSourceType = valuesSourceType;
5960
this.toScriptFieldFactory = toScriptFieldFactory;
60-
this.indexed = indexed;
61+
this.indexType = indexType;
6162
}
6263

6364
@Override
6465
public SortedDoublesIndexFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService) {
65-
return new SortedDoublesIndexFieldData(name, numericType, valuesSourceType, toScriptFieldFactory, indexed);
66+
return new SortedDoublesIndexFieldData(name, numericType, valuesSourceType, toScriptFieldFactory, indexType);
6667
}
6768
}
6869

6970
private final NumericType numericType;
7071
protected final String fieldName;
7172
protected final ValuesSourceType valuesSourceType;
7273
protected final ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory;
73-
protected final boolean indexed;
74+
protected final IndexType indexType;
7475

7576
public SortedDoublesIndexFieldData(
7677
String fieldName,
7778
NumericType numericType,
7879
ValuesSourceType valuesSourceType,
7980
ToScriptFieldFactory<SortedNumericDoubleValues> toScriptFieldFactory,
80-
boolean indexed
81+
IndexType indexType
8182
) {
8283
this.fieldName = fieldName;
8384
this.numericType = Objects.requireNonNull(numericType);
8485
assert this.numericType.isFloatingPoint();
8586
this.valuesSourceType = valuesSourceType;
8687
this.toScriptFieldFactory = toScriptFieldFactory;
87-
this.indexed = indexed;
88+
this.indexType = indexType;
8889
}
8990

9091
@Override
@@ -103,8 +104,8 @@ protected boolean sortRequiresCustomComparator() {
103104
}
104105

105106
@Override
106-
public boolean isIndexed() {
107-
return indexed;
107+
public IndexType indexType() {
108+
return indexType;
108109
}
109110

110111
@Override

server/src/main/java/org/elasticsearch/index/fielddata/plain/SortedNumericIndexFieldData.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.index.fielddata.LeafNumericFieldData;
2424
import org.elasticsearch.index.fielddata.SortedNumericLongValues;
2525
import org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource;
26+
import org.elasticsearch.index.mapper.IndexType;
2627
import org.elasticsearch.indices.breaker.CircuitBreakerService;
2728
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
2829
import org.elasticsearch.script.field.ToScriptFieldFactory;
@@ -44,56 +45,56 @@ public static class Builder implements IndexFieldData.Builder {
4445
private final NumericType numericType;
4546
private final ValuesSourceType valuesSourceType;
4647
protected final ToScriptFieldFactory<SortedNumericLongValues> toScriptFieldFactory;
47-
private final boolean indexed;
48+
private final IndexType indexType;
4849

4950
public Builder(
5051
String name,
5152
NumericType numericType,
5253
ToScriptFieldFactory<SortedNumericLongValues> toScriptFieldFactory,
53-
boolean indexed
54+
IndexType indexType
5455
) {
55-
this(name, numericType, numericType.getValuesSourceType(), toScriptFieldFactory, indexed);
56+
this(name, numericType, numericType.getValuesSourceType(), toScriptFieldFactory, indexType);
5657
}
5758

5859
public Builder(
5960
String name,
6061
NumericType numericType,
6162
ValuesSourceType valuesSourceType,
6263
ToScriptFieldFactory<SortedNumericLongValues> toScriptFieldFactory,
63-
boolean indexed
64+
IndexType indexType
6465
) {
6566
this.name = name;
6667
this.numericType = numericType;
6768
this.valuesSourceType = valuesSourceType;
6869
this.toScriptFieldFactory = toScriptFieldFactory;
69-
this.indexed = indexed;
70+
this.indexType = indexType;
7071
}
7172

7273
@Override
7374
public SortedNumericIndexFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService) {
74-
return new SortedNumericIndexFieldData(name, numericType, valuesSourceType, toScriptFieldFactory, indexed);
75+
return new SortedNumericIndexFieldData(name, numericType, valuesSourceType, toScriptFieldFactory, indexType);
7576
}
7677
}
7778

7879
private final NumericType numericType;
7980
protected final String fieldName;
8081
protected final ValuesSourceType valuesSourceType;
8182
protected final ToScriptFieldFactory<SortedNumericLongValues> toScriptFieldFactory;
82-
protected final boolean indexed;
83+
protected final IndexType indexType;
8384

8485
public SortedNumericIndexFieldData(
8586
String fieldName,
8687
NumericType numericType,
8788
ValuesSourceType valuesSourceType,
8889
ToScriptFieldFactory<SortedNumericLongValues> toScriptFieldFactory,
89-
boolean indexed
90+
IndexType indexType
9091
) {
9192
this.fieldName = fieldName;
9293
this.numericType = Objects.requireNonNull(numericType);
9394
assert this.numericType.isFloatingPoint() == false;
9495
this.valuesSourceType = valuesSourceType;
9596
this.toScriptFieldFactory = toScriptFieldFactory;
96-
this.indexed = indexed;
97+
this.indexType = indexType;
9798
}
9899

99100
@Override
@@ -112,8 +113,8 @@ protected boolean sortRequiresCustomComparator() {
112113
}
113114

114115
@Override
115-
public boolean isIndexed() {
116-
return indexed;
116+
public IndexType indexType() {
117+
return indexType;
117118
}
118119

119120
@Override

server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,7 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
423423
}
424424

425425
if ((operation == FielddataOperation.SEARCH || operation == FielddataOperation.SCRIPT) && hasDocValues()) {
426-
// boolean fields are indexed, but not with points
427-
boolean indexed = false;
428-
return new SortedNumericIndexFieldData.Builder(name(), NumericType.BOOLEAN, BooleanDocValuesField::new, indexed);
426+
return new SortedNumericIndexFieldData.Builder(name(), NumericType.BOOLEAN, BooleanDocValuesField::new, indexType);
429427
}
430428

431429
if (operation == FielddataOperation.SCRIPT) {

0 commit comments

Comments
 (0)