Skip to content

Commit 9fe91ff

Browse files
Provide defaults for index sort settings (#135886)
This PR configures default values for the index settings index.sort.field, index.sort.order, index.sort.mode, and index.sort.missing. Fixes #129062
1 parent 79e53bf commit 9fe91ff

File tree

19 files changed

+1173
-95
lines changed

19 files changed

+1173
-95
lines changed

docs/changelog/135886.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 135886
2+
summary: Provide defaults for index sort settings
3+
area: Mapping
4+
type: bug
5+
issues:
6+
- 129062

rest-api-spec/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ tasks.named("yamlRestCompatTestTransform").configure ({ task ->
9898
task.skipTest("update/100_synthetic_source/stored text", "synthetic recovery source means _recovery_source field will not be present")
9999
task.skipTest("logsdb/10_settings/start time not allowed in logs mode", "we don't validate for index_mode=tsdb when setting start_date/end_date anymore")
100100
task.skipTest("logsdb/10_settings/end time not allowed in logs mode", "we don't validate for index_mode=tsdb when setting start_date/end_date anymore")
101+
task.skipTest("logsdb/10_settings/override sort mode settings", "we changed the error message")
102+
task.skipTest("logsdb/10_settings/override sort missing settings", "we changed the error message")
103+
task.skipTest("logsdb/10_settings/override sort order settings", "we changed the error message")
101104
task.skipTest("tsdb/10_settings/set start_time and end_time without timeseries mode", "we don't validate for index_mode=tsdb when setting start_date/end_date anymore")
102105
task.skipTest("tsdb/10_settings/set start_time, end_time and routing_path via put settings api without time_series mode", "we don't validate for index_mode=tsdb when setting start_date/end_date anymore")
103106
// Expected deprecation warning to compat yaml tests:

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/logsdb/10_settings.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ override sort order settings:
276276
type: text
277277

278278
- match: { error.type: "illegal_argument_exception" }
279-
- match: { error.reason: "index.sort.fields:[] index.sort.order:[asc, asc], size mismatch" }
279+
- match: { error.reason: "setting [index.sort.order] requires [index.sort.field] to be configured" }
280280

281281
---
282282
override sort missing settings:
@@ -312,7 +312,7 @@ override sort missing settings:
312312
type: text
313313

314314
- match: { error.type: "illegal_argument_exception" }
315-
- match: { error.reason: "index.sort.fields:[] index.sort.missing:[_last, _first], size mismatch" }
315+
- match: { error.reason: "setting [index.sort.missing] requires [index.sort.field] to be configured" }
316316

317317
---
318318
override sort mode settings:
@@ -348,7 +348,7 @@ override sort mode settings:
348348
type: text
349349

350350
- match: { error.type: "illegal_argument_exception" }
351-
- match: { error.reason: "index.sort.fields:[] index.sort.mode:[MAX, MAX], size mismatch" }
351+
- match: { error.reason: "setting [index.sort.mode] requires [index.sort.field] to be configured" }
352352

353353
---
354354
override sort field using nested field type in sorting:

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/tsdb/10_settings.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,29 @@ check time_series empty time bound value:
576576
"@timestamp": "2021-09-26T03:09:52.123456789Z",
577577
"metricset": "pod"
578578
}
579+
580+
---
581+
default sort field:
582+
- requires:
583+
cluster_features: [ "mapper.provide_index_sort_setting_defaults" ]
584+
reason: "testing index sort setting defaults"
585+
586+
- do:
587+
indices.create:
588+
index: test_index
589+
body:
590+
settings:
591+
index:
592+
mode: time_series
593+
routing_path: foo
594+
time_series:
595+
start_time: 2021-04-28T00:00:00Z
596+
end_time: 2021-04-29T00:00:00Z
597+
598+
- do:
599+
indices.get_settings:
600+
index: test_index
601+
include_defaults: true
602+
- match: { .test_index.settings.index.mode: time_series }
603+
- match: { .test_index.defaults.index.sort.field: [ "_tsid", "@timestamp" ] }
604+
- match: { .test_index.defaults.index.sort.order: [ "asc", "desc" ] }

server/src/main/java/org/elasticsearch/common/settings/Setting.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,11 +1821,19 @@ public static Setting<List<String>> stringListSetting(String key, Property... pr
18211821
}
18221822

18231823
public static Setting<List<String>> stringListSetting(String key, List<String> defValue, Property... properties) {
1824-
return new ListSetting<>(key, null, s -> defValue, s -> parseableStringToList(s, Function.identity()), v -> {}, properties) {
1824+
return stringListSettingWithDefaultProvider(key, s -> defValue, properties);
1825+
}
1826+
1827+
public static Setting<List<String>> stringListSettingWithDefaultProvider(
1828+
String key,
1829+
Function<Settings, List<String>> defValueProvider,
1830+
Property... properties
1831+
) {
1832+
return new ListSetting<>(key, null, defValueProvider, s -> parseableStringToList(s, Function.identity()), v -> {}, properties) {
18251833
@Override
18261834
public List<String> get(Settings settings) {
18271835
checkDeprecation(settings);
1828-
return settings.getAsList(getKey(), defValue);
1836+
return settings.getAsList(getKey(), defValueProvider.apply(settings));
18291837
}
18301838
};
18311839
}
@@ -1852,6 +1860,15 @@ public static <T> Setting<List<T>> listSetting(
18521860
return listSetting(key, null, singleValueParser, s -> defaultStringValue, properties);
18531861
}
18541862

1863+
public static <T> Setting<List<T>> listSetting(
1864+
final String key,
1865+
final Function<Settings, List<String>> defaultStringValueProvider,
1866+
final Function<String, T> singleValueParser,
1867+
final Property... properties
1868+
) {
1869+
return listSetting(key, null, singleValueParser, defaultStringValueProvider, properties);
1870+
}
1871+
18551872
public static <T> Setting<List<T>> listSetting(
18561873
final String key,
18571874
final List<String> defaultStringValue,
@@ -1981,7 +1998,7 @@ public void diff(Settings.Builder builder, Settings source, Settings defaultSett
19811998
if (exists(source) == false) {
19821999
List<String> asList = defaultSettings.getAsList(getKey(), null);
19832000
if (asList == null) {
1984-
builder.putList(getKey(), defaultStringValue.apply(defaultSettings));
2001+
builder.putList(getKey(), defaultStringValue.apply(source));
19852002
} else {
19862003
builder.putList(getKey(), asList);
19872004
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ void validateWithOtherSettings(Map<Setting<?>, Object> settings) {
140140
if (settings.get(IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING) != Integer.valueOf(1)) {
141141
throw new IllegalArgumentException(error(IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING));
142142
}
143+
144+
var settingsWithIndexMode = Settings.builder().put(IndexSettings.MODE.getKey(), getName()).build();
145+
143146
for (Setting<?> unsupported : TIME_SERIES_UNSUPPORTED) {
144-
if (false == Objects.equals(unsupported.getDefault(Settings.EMPTY), settings.get(unsupported))) {
147+
if (false == Objects.equals(unsupported.getDefault(settingsWithIndexMode), settings.get(unsupported))) {
145148
throw new IllegalArgumentException(error(unsupported));
146149
}
147150
}

0 commit comments

Comments
 (0)