Skip to content

Commit b813076

Browse files
authored
Add back version based logic from IndexSortConfig
This change re-introduces pre-7.13 deprecation logging and silent handling of index sorting on alias fields. We need to still support this for v9 for read-only indices.
1 parent cc69e06 commit b813076

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

server/src/main/java/org/elasticsearch/index/IndexSortConfig.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.apache.lucene.search.SortedNumericSortField;
1515
import org.apache.lucene.search.SortedSetSortField;
1616
import org.elasticsearch.cluster.metadata.DataStream;
17+
import org.elasticsearch.common.logging.DeprecationCategory;
18+
import org.elasticsearch.common.logging.DeprecationLogger;
1719
import org.elasticsearch.common.settings.Setting;
1820
import org.elasticsearch.common.settings.Settings;
1921
import org.elasticsearch.index.fielddata.IndexFieldData;
@@ -53,6 +55,8 @@
5355
**/
5456
public final class IndexSortConfig {
5557

58+
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(IndexSortConfig.class);
59+
5660
/**
5761
* The list of field names
5862
*/
@@ -134,10 +138,14 @@ private static MultiValueMode parseMultiValueMode(String value) {
134138

135139
// visible for tests
136140
final FieldSortSpec[] sortSpecs;
141+
private final IndexVersion indexCreatedVersion;
142+
private final String indexName;
137143
private final IndexMode indexMode;
138144

139145
public IndexSortConfig(IndexSettings indexSettings) {
140146
final Settings settings = indexSettings.getSettings();
147+
this.indexCreatedVersion = indexSettings.getIndexVersionCreated();
148+
this.indexName = indexSettings.getIndex().getName();
141149
this.indexMode = indexSettings.getMode();
142150

143151
if (this.indexMode == IndexMode.TIME_SERIES) {
@@ -230,7 +238,22 @@ public Sort buildIndexSort(
230238
throw new IllegalArgumentException(err);
231239
}
232240
if (Objects.equals(ft.name(), sortSpec.field) == false) {
233-
throw new IllegalArgumentException("Cannot use alias [" + sortSpec.field + "] as an index sort field");
241+
if (this.indexCreatedVersion.onOrAfter(IndexVersions.V_7_13_0)) {
242+
throw new IllegalArgumentException("Cannot use alias [" + sortSpec.field + "] as an index sort field");
243+
} else {
244+
DEPRECATION_LOGGER.warn(
245+
DeprecationCategory.MAPPINGS,
246+
"index-sort-aliases",
247+
"Index sort for index ["
248+
+ indexName
249+
+ "] defined on field ["
250+
+ sortSpec.field
251+
+ "] which resolves to field ["
252+
+ ft.name()
253+
+ "]. "
254+
+ "You will not be able to define an index sort over aliased fields in new indexes"
255+
);
256+
}
234257
}
235258
boolean reverse = sortSpec.order == null ? false : (sortSpec.order == SortOrder.DESC);
236259
MultiValueMode mode = sortSpec.mode;

server/src/test/java/org/elasticsearch/index/IndexSortSettingsTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ public void testSortingAgainstAliases() {
160160
assertEquals("Cannot use alias [field] as an index sort field", e.getMessage());
161161
}
162162

163+
public void testSortingAgainstAliasesPre713() {
164+
IndexSettings indexSettings = indexSettings(
165+
Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersions.V_7_12_0).put("index.sort.field", "field").build()
166+
);
167+
MappedFieldType aliased = new KeywordFieldMapper.KeywordFieldType("aliased");
168+
Sort sort = buildIndexSort(indexSettings, Map.of("field", aliased));
169+
assertThat(sort.getSort(), arrayWithSize(1));
170+
assertThat(sort.getSort()[0].getField(), equalTo("aliased"));
171+
assertWarnings(
172+
"Index sort for index [test] defined on field [field] which resolves to field [aliased]. "
173+
+ "You will not be able to define an index sort over aliased fields in new indexes"
174+
);
175+
}
176+
163177
public void testTimeSeriesMode() {
164178
IndexSettings indexSettings = indexSettings(
165179
Settings.builder()

0 commit comments

Comments
 (0)