Skip to content

Commit

Permalink
Merge branch 'main' into kderusso/semantic-text-knn-query
Browse files Browse the repository at this point in the history
  • Loading branch information
kderusso authored Dec 18, 2024
2 parents a34be31 + b813076 commit 1f7b54f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.apache.lucene.search.SortedNumericSortField;
import org.apache.lucene.search.SortedSetSortField;
import org.elasticsearch.cluster.metadata.DataStream;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.fielddata.IndexFieldData;
Expand Down Expand Up @@ -53,6 +55,8 @@
**/
public final class IndexSortConfig {

private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(IndexSortConfig.class);

/**
* The list of field names
*/
Expand Down Expand Up @@ -134,10 +138,14 @@ private static MultiValueMode parseMultiValueMode(String value) {

// visible for tests
final FieldSortSpec[] sortSpecs;
private final IndexVersion indexCreatedVersion;
private final String indexName;
private final IndexMode indexMode;

public IndexSortConfig(IndexSettings indexSettings) {
final Settings settings = indexSettings.getSettings();
this.indexCreatedVersion = indexSettings.getIndexVersionCreated();
this.indexName = indexSettings.getIndex().getName();
this.indexMode = indexSettings.getMode();

if (this.indexMode == IndexMode.TIME_SERIES) {
Expand Down Expand Up @@ -230,7 +238,22 @@ public Sort buildIndexSort(
throw new IllegalArgumentException(err);
}
if (Objects.equals(ft.name(), sortSpec.field) == false) {
throw new IllegalArgumentException("Cannot use alias [" + sortSpec.field + "] as an index sort field");
if (this.indexCreatedVersion.onOrAfter(IndexVersions.V_7_13_0)) {
throw new IllegalArgumentException("Cannot use alias [" + sortSpec.field + "] as an index sort field");
} else {
DEPRECATION_LOGGER.warn(
DeprecationCategory.MAPPINGS,
"index-sort-aliases",
"Index sort for index ["
+ indexName
+ "] defined on field ["
+ sortSpec.field
+ "] which resolves to field ["
+ ft.name()
+ "]. "
+ "You will not be able to define an index sort over aliased fields in new indexes"
);
}
}
boolean reverse = sortSpec.order == null ? false : (sortSpec.order == SortOrder.DESC);
MultiValueMode mode = sortSpec.mode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ public void testSortingAgainstAliases() {
assertEquals("Cannot use alias [field] as an index sort field", e.getMessage());
}

public void testSortingAgainstAliasesPre713() {
IndexSettings indexSettings = indexSettings(
Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersions.V_7_12_0).put("index.sort.field", "field").build()
);
MappedFieldType aliased = new KeywordFieldMapper.KeywordFieldType("aliased");
Sort sort = buildIndexSort(indexSettings, Map.of("field", aliased));
assertThat(sort.getSort(), arrayWithSize(1));
assertThat(sort.getSort()[0].getField(), equalTo("aliased"));
assertWarnings(
"Index sort for index [test] defined on field [field] which resolves to field [aliased]. "
+ "You will not be able to define an index sort over aliased fields in new indexes"
);
}

public void testTimeSeriesMode() {
IndexSettings indexSettings = indexSettings(
Settings.builder()
Expand Down

0 comments on commit 1f7b54f

Please sign in to comment.