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
20 changes: 10 additions & 10 deletions server/src/main/java/org/elasticsearch/common/lucene/Lucene.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public static void writeSortType(StreamOutput out, SortField.Type sortType) thro
* Returns the generic version of the provided {@link SortField} that
* can be used to merge documents coming from different shards.
*/
private static SortField rewriteMergeSortField(SortField sortField) {
public static SortField rewriteMergeSortField(SortField sortField) {
if (sortField.getClass() == GEO_DISTANCE_SORT_TYPE_CLASS) {
SortField newSortField = new SortField(sortField.getField(), SortField.Type.DOUBLE);
newSortField.setMissingValue(sortField.getMissingValue());
Expand All @@ -642,6 +642,13 @@ private static SortField rewriteMergeSortField(SortField sortField) {
);
newSortField.setMissingValue(sortField.getMissingValue());
return newSortField;
} else if (sortField.getComparatorSource() instanceof IndexFieldData.XFieldComparatorSource fcs) {
SortField newSortField = new SortField(sortField.getField(), fcs.reducedType(), sortField.getReverse());
Object missingValue = fcs.missingValue(sortField.getReverse());
if (missingValue != null) {
newSortField.setMissingValue(missingValue);
}
return newSortField;
} else if (sortField.getClass() == ShardDocSortField.class) {
return new SortField(sortField.getField(), SortField.Type.LONG, sortField.getReverse());
} else {
Expand All @@ -655,15 +662,8 @@ static void writeSortField(StreamOutput out, SortField sortField) throws IOExcep
throw new IllegalArgumentException("Cannot serialize SortField impl [" + sortField + "]");
}
out.writeOptionalString(sortField.getField());
if (sortField.getComparatorSource() != null) {
IndexFieldData.XFieldComparatorSource comparatorSource = (IndexFieldData.XFieldComparatorSource) sortField
.getComparatorSource();
writeSortType(out, comparatorSource.reducedType());
writeMissingValue(out, comparatorSource.missingValue(sortField.getReverse()));
} else {
writeSortType(out, sortField.getType());
writeMissingValue(out, sortField.getMissingValue());
}
writeSortType(out, sortField.getType());
writeMissingValue(out, sortField.getMissingValue());
out.writeBoolean(sortField.getReverse());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,22 @@ public void testEqualsAndHashcode() {
}

protected final SearchExecutionContext createMockSearchExecutionContext() {
return createMockSearchExecutionContext(null);
return createMockSearchExecutionContext(null, IndexVersion.current());
}

protected final SearchExecutionContext createMockSearchExecutionContext(IndexVersion indexVersion) {
return createMockSearchExecutionContext(null, indexVersion);
}

protected final SearchExecutionContext createMockSearchExecutionContext(IndexSearcher searcher) {
return createMockSearchExecutionContext(searcher, IndexVersion.current());
}

protected final SearchExecutionContext createMockSearchExecutionContext(IndexSearcher searcher, IndexVersion indexVersion) {
Index index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_");
IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(
index,
Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()).build()
Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, indexVersion).build()
);
BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(idxSettings, mock(BitsetFilterCache.Listener.class));
BiFunction<MappedFieldType, FieldDataContext, IndexFieldData<?>> indexFieldDataLookup = (fieldType, fdc) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.search.AssertingIndexSearcher;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource;
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested;
import org.elasticsearch.index.mapper.DateFieldMapper;
Expand All @@ -48,6 +51,7 @@
import org.elasticsearch.search.MultiValueMode;
import org.elasticsearch.search.SearchSortValuesAndFormats;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.index.IndexVersionUtils;
import org.elasticsearch.xcontent.XContentParseException;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.json.JsonXContent;
Expand Down Expand Up @@ -657,4 +661,40 @@ public void testIsBottomSortShardDisjoint() throws Exception {
protected FieldSortBuilder fromXContent(XContentParser parser, String fieldName) throws IOException {
return FieldSortBuilder.fromXContent(parser, fieldName);
}

public void testIntRewritesToLong() throws Exception {

assertIntegerSortRewrite(
IndexVersionUtils.randomPreviousCompatibleVersion(random(), IndexVersions.INDEX_INT_SORT_INT_TYPE_8_19),
SortField.Type.LONG
);
assertIntegerSortRewrite(
IndexVersionUtils.randomVersionBetween(
random(),
IndexVersions.INDEX_INT_SORT_INT_TYPE_8_19,
IndexVersionUtils.getPreviousVersion(IndexVersions.UPGRADE_TO_LUCENE_10_0_0)
),
SortField.Type.INT
);
assertIntegerSortRewrite(
IndexVersionUtils.randomVersionBetween(
random(),
IndexVersions.UPGRADE_TO_LUCENE_10_0_0,
IndexVersionUtils.getPreviousVersion(IndexVersions.INDEX_INT_SORT_INT_TYPE)
),
SortField.Type.LONG
);
assertIntegerSortRewrite(
IndexVersionUtils.randomVersionBetween(random(), IndexVersions.INDEX_INT_SORT_INT_TYPE, IndexVersion.current()),
SortField.Type.INT
);
assertIntegerSortRewrite(IndexVersion.current(), SortField.Type.INT);
}

private void assertIntegerSortRewrite(IndexVersion version, SortField.Type expectedType) throws IOException {
FieldSortBuilder builder = new FieldSortBuilder("custom-integer");
SortFieldAndFormat sff = builder.build(createMockSearchExecutionContext(version));
SortField sf = Lucene.rewriteMergeSortField(sff.field());
assertThat(sf.getType(), equalTo(expectedType));
}
}