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 @@ -678,4 +678,9 @@ protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed)
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,9 @@ public void testLoadSyntheticSourceFromStringOrBytesRef() throws IOException {
}
}
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,9 @@ protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed)
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,9 @@ protected SyntheticSourceSupport syntheticSourceSupport(boolean syntheticSource)
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -649,4 +649,12 @@ private double encodeDecode(double value, double scalingFactor) {
private static double randomValue() {
return randomBoolean() ? randomDoubleBetween(-Double.MAX_VALUE, Double.MAX_VALUE, true) : randomFloat();
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of(
// TODO doubles currently disable pruning, can we re-enable?
new SortShortcutSupport(this::minimalMapping, this::writeField, false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -877,4 +877,9 @@ protected RandomIndexWriter indexWriterForSyntheticSource(Directory directory) t
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,9 @@ public void testAggregationsDocValuesDisabled() throws IOException {
}));
assertAggregatableConsistency(mapperService.fieldType("field"));
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,9 @@ protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed)
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,9 @@ protected void validateRoundTripReader(String syntheticSource, DirectoryReader r
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed)
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,9 @@ public int compareTo(BytesCompareUnsigned o) {
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,13 @@ public void execute() {
}
};
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of(
// TODO: boolean field mapper uses a numeric comparator but is indexed with Terms
// so skipping doesn't work here.
new SortShortcutSupport(this::minimalMapping, this::writeField, false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -985,4 +985,9 @@ protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed)
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -914,4 +914,24 @@ public void testSingletonLongBulkBlockReadingManyValues() throws Exception {
}
}
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of(
new SortShortcutSupport(b -> b.field("type", "date"), b -> b.field("field", "2025-10-30T00:00:00"), true),
new SortShortcutSupport(b -> b.field("type", "date_nanos"), b -> b.field("field", "2025-10-30T00:00:00"), true),
new SortShortcutSupport(
IndexVersion.fromId(5000099),
b -> b.field("type", "date"),
b -> b.field("field", "2025-10-30T00:00:00"),
false
),
new SortShortcutSupport(
IndexVersion.fromId(5000099),
b -> b.field("type", "date_nanos"),
b -> b.field("field", "2025-10-30T00:00:00"),
false
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,9 @@ public void testSyntheticSourceKeepArrays() {
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,13 @@ public void execute() {
protected String randomSyntheticSourceKeep() {
return "all";
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of(
// TODO - shortcuts are disabled here, can we enable them?
new SortShortcutSupport(this::minimalMapping, this::writeField, false),
new SortShortcutSupport(IndexVersion.fromId(5000099), this::minimalMapping, this::writeField, false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1219,4 +1219,9 @@ protected String randomSyntheticSourceKeep() {
// Only option all keeps array source in ignored source.
return randomFrom("all");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of(new SortShortcutSupport(this::minimalMapping, this::writeField, true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,9 @@ protected Object generateRandomInputValue(MappedFieldType ft) {
assumeFalse("DocValuesFetcher doesn't work", true);
return null;
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1776,4 +1776,8 @@ public void testNormsEnabledWhenIndexModeIsTsdb_bwcCheck() throws IOException {
assertThat(fieldType.omitNorms(), is(false));
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1016,4 +1016,9 @@ public void assertStoredFieldsEquals(String info, IndexReader leftReader, IndexR
assertFalse(info, rightIterator.hasNext());
}
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of(new SortShortcutSupport(this::minimalMapping, this::writeField, true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.util.List;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;

Expand Down Expand Up @@ -262,4 +263,9 @@ private void assertSyntheticVectors(String mapping, BytesReference source, XCont
}
}
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Pruning;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
Expand Down Expand Up @@ -64,6 +65,7 @@
import org.elasticsearch.script.ScriptFactory;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;
import org.elasticsearch.search.lookup.LeafStoredFieldsLookup;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.Source;
Expand Down Expand Up @@ -1697,4 +1699,68 @@ protected <T> T compileOtherScript(Script script, ScriptContext<T> context) {
*/
abstract ScriptFactory nonEmptyFieldScript();
}

/**
* Return a list of scenarios where sorts on this field should or should not produce
* competitive iterators. Field types that do not support sorting should return an
* empty List.
*/
protected abstract List<SortShortcutSupport> getSortShortcutSupport();

public record SortShortcutSupport(
IndexVersion indexVersion,
Settings settings,
CheckedConsumer<XContentBuilder, IOException> mappings,
CheckedConsumer<XContentBuilder, IOException> document,
boolean supportsShortcut
) {
public SortShortcutSupport(
CheckedConsumer<XContentBuilder, IOException> mappings,
CheckedConsumer<XContentBuilder, IOException> document,
boolean supportsShortcut
) {
this(IndexVersion.current(), SETTINGS, mappings, document, supportsShortcut);
}

public SortShortcutSupport(
IndexVersion indexVersion,
CheckedConsumer<XContentBuilder, IOException> mappings,
CheckedConsumer<XContentBuilder, IOException> document,
boolean supportsShortcut
) {
this(indexVersion, SETTINGS, mappings, document, supportsShortcut);
}
}

public final void testSortShortcuts() throws IOException {
List<SortShortcutSupport> tests = getSortShortcutSupport();
assumeTrue("Sort shortcuts not supported", tests != null && tests.isEmpty() == false);

for (SortShortcutSupport sortShortcutSupport : tests) {
MapperService mapperService = createMapperService(sortShortcutSupport.indexVersion(), sortShortcutSupport.settings, () -> true);
merge(mapperService, fieldMapping(sortShortcutSupport.mappings));
withLuceneIndex(mapperService, iw -> {
iw.addDocument(
mapperService.documentParser()
.parseDocument(source(sortShortcutSupport.document()), mapperService.mappingLookup())
.rootDoc()
);
}, reader -> {
IndexSearcher searcher = newSearcher(reader);
MappedFieldType ft = mapperService.fieldType("field");
SortField sortField = ft.fielddataBuilder(new FieldDataContext("", mapperService.getIndexSettings(), () -> {
throw new UnsupportedOperationException();
}, Set::of, MappedFieldType.FielddataOperation.SEARCH))
.build(null, null)
.sortField(getVersion(), null, MultiValueMode.MIN, null, false);
var comparator = sortField.getComparator(10, Pruning.GREATER_THAN_OR_EQUAL_TO);
var leafComparator = comparator.getLeafComparator(searcher.getLeafContexts().getFirst());
if (sortShortcutSupport.supportsShortcut) {
assertNotNull(leafComparator.competitiveIterator());
} else {
assertNull(leafComparator.competitiveIterator());
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.script.DoubleFieldScript;
import org.elasticsearch.script.LongFieldScript;
import org.elasticsearch.script.Script;
Expand Down Expand Up @@ -496,4 +497,12 @@ public List<SyntheticSourceInvalidExample> invalidExample() throws IOException {
return List.of();
}
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of(
new SortShortcutSupport(this::minimalMapping, this::writeField, true),
new SortShortcutSupport(IndexVersion.fromId(5000099), this::minimalMapping, this::writeField, false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,9 @@ public List<SyntheticSourceInvalidExample> invalidExample() throws IOException {
public void testSyntheticSourceKeepArrays() {
// The mapper expects to parse an array of values by default, it's not compatible with array of arrays.
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,9 @@ public void testInvalidOffsets() throws IOException {
})));
assertThat(exc.getCause().getCause().getCause().getMessage(), containsString("Illegal offsets"));
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2369,4 +2369,9 @@ private static void assertSparseFeatures(LuceneDocument doc, String fieldName, i
private void givenModelSettings(String inferenceId, MinimalServiceSettings modelSettings) {
when(globalModelRegistry.getMinimalServiceSettings(inferenceId)).thenReturn(modelSettings);
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,9 @@ public void testSyntheticSourceKeepArrays() {
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,9 @@ public void testSyntheticSourceKeepArrays() {
protected boolean supportsCopyTo() {
return false;
}

@Override
protected List<SortShortcutSupport> getSortShortcutSupport() {
return List.of();
}
}
Loading