Skip to content

Commit 59fb5b6

Browse files
authored
Use IndexSettings in DateFieldMapper.Builder constructor (#136758)
Instead of passing five separate parameters which are all available from one IndexSettings object, just pass that one object.
1 parent 9ce05e5 commit 59fb5b6

File tree

5 files changed

+77
-153
lines changed

5 files changed

+77
-153
lines changed

modules/data-streams/src/test/java/org/elasticsearch/datastreams/DataStreamGetWriteIndexTests.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.elasticsearch.env.Environment;
4040
import org.elasticsearch.index.Index;
4141
import org.elasticsearch.index.IndexSettingProviders;
42+
import org.elasticsearch.index.IndexSettings;
4243
import org.elasticsearch.index.IndexVersion;
4344
import org.elasticsearch.index.mapper.DateFieldMapper;
4445
import org.elasticsearch.index.mapper.MapperBuilderContext;
@@ -212,6 +213,16 @@ public void testPickingBackingIndicesNanoTimestamp() throws Exception {
212213
}
213214
}
214215

216+
private static final IndexSettings DEFAULT_INDEX_SETTINGS = new IndexSettings(
217+
IndexMetadata.builder("_na_")
218+
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()))
219+
.numberOfShards(1)
220+
.numberOfReplicas(0)
221+
.creationDate(System.currentTimeMillis())
222+
.build(),
223+
Settings.EMPTY
224+
);
225+
215226
@Before
216227
public void setup() throws Exception {
217228
testThreadPool = new TestThreadPool(getTestName());
@@ -224,8 +235,7 @@ public void setup() throws Exception {
224235
DateFieldMapper.Resolution.MILLISECONDS,
225236
null,
226237
ScriptCompiler.NONE,
227-
false,
228-
IndexVersion.current()
238+
DEFAULT_INDEX_SETTINGS
229239
).build(MapperBuilderContext.root(false, false));
230240
RootObjectMapper.Builder root = new RootObjectMapper.Builder("_doc", ObjectMapper.Defaults.SUBOBJECTS);
231241
root.add(
@@ -234,9 +244,8 @@ public void setup() throws Exception {
234244
DateFieldMapper.Resolution.MILLISECONDS,
235245
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
236246
ScriptCompiler.NONE,
237-
true,
238-
IndexVersion.current()
239-
)
247+
DEFAULT_INDEX_SETTINGS
248+
).ignoreMalformed(true)
240249
);
241250
MetadataFieldMapper dtfm = DataStreamTestHelper.getDataStreamTimestampFieldMapper();
242251
Mapping mapping = new Mapping(

server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

Lines changed: 31 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.elasticsearch.features.NodeFeature;
3939
import org.elasticsearch.index.IndexMode;
4040
import org.elasticsearch.index.IndexSettings;
41-
import org.elasticsearch.index.IndexSortConfig;
4241
import org.elasticsearch.index.IndexVersion;
4342
import org.elasticsearch.index.IndexVersions;
4443
import org.elasticsearch.index.fielddata.FieldDataContext;
@@ -287,47 +286,25 @@ public static final class Builder extends FieldMapper.Builder {
287286
private final Resolution resolution;
288287
private final IndexVersion indexCreatedVersion;
289288
private final ScriptCompiler scriptCompiler;
290-
private final IndexMode indexMode;
291-
private final IndexSortConfig indexSortConfig;
292-
private final boolean useDocValuesSkipper;
289+
private final IndexSettings indexSettings;
293290

294291
public Builder(
295292
String name,
296293
Resolution resolution,
297294
DateFormatter dateFormatter,
298295
ScriptCompiler scriptCompiler,
299-
boolean ignoreMalformedByDefault,
300-
IndexVersion indexCreatedVersion
301-
) {
302-
this(
303-
name,
304-
resolution,
305-
dateFormatter,
306-
scriptCompiler,
307-
ignoreMalformedByDefault,
308-
IndexMode.STANDARD,
309-
null,
310-
indexCreatedVersion,
311-
false
312-
);
313-
}
314-
315-
public Builder(
316-
String name,
317-
Resolution resolution,
318-
DateFormatter dateFormatter,
319-
ScriptCompiler scriptCompiler,
320-
boolean ignoreMalformedByDefault,
321-
IndexMode indexMode,
322-
IndexSortConfig indexSortConfig,
323-
IndexVersion indexCreatedVersion,
324-
boolean useDocValuesSkipper
296+
IndexSettings indexSettings
325297
) {
326298
super(name);
327299
this.resolution = resolution;
328-
this.indexCreatedVersion = indexCreatedVersion;
300+
this.indexCreatedVersion = indexSettings.getIndexVersionCreated();
329301
this.scriptCompiler = Objects.requireNonNull(scriptCompiler);
330-
this.ignoreMalformed = Parameter.boolParam("ignore_malformed", true, m -> toType(m).ignoreMalformed, ignoreMalformedByDefault);
302+
this.ignoreMalformed = Parameter.boolParam(
303+
"ignore_malformed",
304+
true,
305+
m -> toType(m).ignoreMalformed,
306+
FieldMapper.IGNORE_MALFORMED_SETTING.get(indexSettings.getSettings())
307+
);
331308

332309
this.script.precludesParameters(nullValue, ignoreMalformed);
333310
addScriptValidation(script, index, docValues);
@@ -345,9 +322,12 @@ public Builder(
345322
this.format.setValue(dateFormatter.pattern());
346323
this.locale.setValue(dateFormatter.locale());
347324
}
348-
this.indexMode = indexMode;
349-
this.indexSortConfig = indexSortConfig;
350-
this.useDocValuesSkipper = useDocValuesSkipper;
325+
this.indexSettings = indexSettings;
326+
}
327+
328+
public Builder ignoreMalformed(boolean ignoreMalformed) {
329+
this.ignoreMalformed.setValue(ignoreMalformed);
330+
return this;
351331
}
352332

353333
DateFormatter buildFormatter() {
@@ -419,14 +399,7 @@ private Long parseNullValue(DateFieldType fieldType) {
419399
}
420400

421401
private IndexType indexType(String fullFieldName) {
422-
boolean hasDocValuesSkipper = shouldUseDocValuesSkipper(
423-
indexCreatedVersion,
424-
useDocValuesSkipper,
425-
docValues.getValue(),
426-
indexMode,
427-
indexSortConfig,
428-
fullFieldName
429-
);
402+
boolean hasDocValuesSkipper = shouldUseDocValuesSkipper(indexSettings, docValues.getValue(), fullFieldName);
430403
if (hasDocValuesSkipper) {
431404
return IndexType.skippers();
432405
}
@@ -467,42 +440,17 @@ public DateFieldMapper build(MapperBuilderContext context) {
467440
nullTimestamp,
468441
resolution,
469442
context.isSourceSynthetic(),
470-
indexMode,
471-
indexSortConfig,
472-
indexType.hasDocValuesSkipper(),
473443
this
474444
);
475445
}
476446
}
477447

478448
public static final TypeParser MILLIS_PARSER = createTypeParserWithLegacySupport((n, c) -> {
479-
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
480-
return new Builder(
481-
n,
482-
Resolution.MILLISECONDS,
483-
c.getDateFormatter(),
484-
c.scriptCompiler(),
485-
ignoreMalformedByDefault,
486-
c.getIndexSettings().getMode(),
487-
c.getIndexSettings().getIndexSortConfig(),
488-
c.indexVersionCreated(),
489-
IndexSettings.USE_DOC_VALUES_SKIPPER.get(c.getSettings())
490-
);
449+
return new Builder(n, Resolution.MILLISECONDS, c.getDateFormatter(), c.scriptCompiler(), c.getIndexSettings());
491450
});
492451

493452
public static final TypeParser NANOS_PARSER = createTypeParserWithLegacySupport((n, c) -> {
494-
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
495-
return new Builder(
496-
n,
497-
Resolution.NANOSECONDS,
498-
c.getDateFormatter(),
499-
c.scriptCompiler(),
500-
ignoreMalformedByDefault,
501-
c.getIndexSettings().getMode(),
502-
c.getIndexSettings().getIndexSortConfig(),
503-
c.indexVersionCreated(),
504-
IndexSettings.USE_DOC_VALUES_SKIPPER.get(c.getSettings())
505-
);
453+
return new Builder(n, Resolution.NANOSECONDS, c.getDateFormatter(), c.scriptCompiler(), c.getIndexSettings());
506454
});
507455

508456
public static final class DateFieldType extends MappedFieldType {
@@ -1114,17 +1062,12 @@ public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
11141062
private final Resolution resolution;
11151063
private final boolean isSourceSynthetic;
11161064

1117-
private final boolean ignoreMalformedByDefault;
1118-
private final IndexVersion indexCreatedVersion;
1119-
11201065
private final Script script;
11211066
private final ScriptCompiler scriptCompiler;
11221067
private final FieldValues<Long> scriptValues;
11231068

11241069
private final boolean isDataStreamTimestampField;
1125-
private final IndexMode indexMode;
1126-
private final IndexSortConfig indexSortConfig;
1127-
private final boolean hasDocValuesSkipper;
1070+
private final IndexSettings indexSettings;
11281071

11291072
private DateFieldMapper(
11301073
String leafName,
@@ -1133,9 +1076,6 @@ private DateFieldMapper(
11331076
Long nullValue,
11341077
Resolution resolution,
11351078
boolean isSourceSynthetic,
1136-
IndexMode indexMode,
1137-
IndexSortConfig indexSortConfig,
1138-
boolean hasDocValuesSkipper,
11391079
Builder builder
11401080
) {
11411081
super(leafName, mappedFieldType, builderParams);
@@ -1149,15 +1089,11 @@ private DateFieldMapper(
11491089
this.nullValue = nullValue;
11501090
this.resolution = resolution;
11511091
this.isSourceSynthetic = isSourceSynthetic;
1152-
this.ignoreMalformedByDefault = builder.ignoreMalformed.getDefaultValue();
1153-
this.indexCreatedVersion = builder.indexCreatedVersion;
11541092
this.script = builder.script.get();
11551093
this.scriptCompiler = builder.scriptCompiler;
11561094
this.scriptValues = builder.scriptValues();
11571095
this.isDataStreamTimestampField = mappedFieldType.name().equals(DataStreamTimestampFieldMapper.DEFAULT_PATH);
1158-
this.indexMode = indexMode;
1159-
this.indexSortConfig = indexSortConfig;
1160-
this.hasDocValuesSkipper = hasDocValuesSkipper;
1096+
this.indexSettings = builder.indexSettings;
11611097
}
11621098

11631099
/**
@@ -1168,46 +1104,25 @@ private DateFieldMapper(
11681104
* field has doc values enabled. Additionally, the index mode must be {@link IndexMode#LOGSDB} or {@link IndexMode#TIME_SERIES}, and
11691105
* the index sorting configuration must include the {@code @timestamp} field.
11701106
*
1171-
* @param indexCreatedVersion The version of the index when it was created.
1172-
* @param useDocValuesSkipper Whether the doc values skipper feature is enabled via the {@code index.mapping.use_doc_values_skipper}
1173-
* setting.
1174-
* @param hasDocValues Whether the field has doc values enabled.
1175-
* @param indexMode The index mode, which must be {@link IndexMode#LOGSDB} or {@link IndexMode#TIME_SERIES}.
1176-
* @param indexSortConfig The index sorting configuration, which must include the {@code @timestamp} field.
1177-
* @param fullFieldName The full name of the field being checked, expected to be {@code @timestamp}.
1107+
* @param indexSettings The index settings of the parent index
1108+
* @param hasDocValues Whether the field has doc values enabled.
1109+
* @param fullFieldName The full name of the field being checked, expected to be {@code @timestamp}.
11781110
* @return {@code true} if the doc values skipper should be used, {@code false} otherwise.
11791111
*/
11801112

1181-
private static boolean shouldUseDocValuesSkipper(
1182-
final IndexVersion indexCreatedVersion,
1183-
boolean useDocValuesSkipper,
1184-
boolean hasDocValues,
1185-
final IndexMode indexMode,
1186-
final IndexSortConfig indexSortConfig,
1187-
final String fullFieldName
1188-
) {
1189-
return indexCreatedVersion.onOrAfter(IndexVersions.REENABLED_TIMESTAMP_DOC_VALUES_SPARSE_INDEX)
1190-
&& useDocValuesSkipper
1113+
private static boolean shouldUseDocValuesSkipper(IndexSettings indexSettings, boolean hasDocValues, final String fullFieldName) {
1114+
return indexSettings.getIndexVersionCreated().onOrAfter(IndexVersions.REENABLED_TIMESTAMP_DOC_VALUES_SPARSE_INDEX)
1115+
&& indexSettings.useDocValuesSkipper()
11911116
&& hasDocValues
1192-
&& (IndexMode.LOGSDB.equals(indexMode) || IndexMode.TIME_SERIES.equals(indexMode))
1193-
&& indexSortConfig != null
1194-
&& indexSortConfig.hasSortOnField(fullFieldName)
1117+
&& (IndexMode.LOGSDB.equals(indexSettings.getMode()) || IndexMode.TIME_SERIES.equals(indexSettings.getMode()))
1118+
&& indexSettings.getIndexSortConfig() != null
1119+
&& indexSettings.getIndexSortConfig().hasSortOnField(fullFieldName)
11951120
&& DataStreamTimestampFieldMapper.DEFAULT_PATH.equals(fullFieldName);
11961121
}
11971122

11981123
@Override
11991124
public FieldMapper.Builder getMergeBuilder() {
1200-
return new Builder(
1201-
leafName(),
1202-
resolution,
1203-
null,
1204-
scriptCompiler,
1205-
ignoreMalformedByDefault,
1206-
indexMode,
1207-
indexSortConfig,
1208-
indexCreatedVersion,
1209-
hasDocValuesSkipper
1210-
).init(this);
1125+
return new Builder(leafName(), resolution, null, scriptCompiler, indexSettings).init(this);
12111126
}
12121127

12131128
@Override
@@ -1277,7 +1192,7 @@ private void indexValue(DocumentParserContext context, long timestamp) {
12771192
DataStreamTimestampFieldMapper.storeTimestampValueForReuse(context.doc(), timestamp);
12781193
}
12791194

1280-
if (hasDocValuesSkipper && hasDocValues) {
1195+
if (fieldType().hasDocValuesSkipper()) {
12811196
context.doc().add(SortedNumericDocValuesField.indexedField(fieldType().name(), timestamp));
12821197
} else if (indexed && hasDocValues) {
12831198
context.doc().add(new LongField(fieldType().name(), timestamp, Field.Store.NO));

server/src/main/java/org/elasticsearch/index/mapper/DynamicFieldsBuilder.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.elasticsearch.common.CheckedSupplier;
1515
import org.elasticsearch.common.settings.Settings;
1616
import org.elasticsearch.common.time.DateFormatter;
17-
import org.elasticsearch.index.IndexSettings;
1817
import org.elasticsearch.index.mapper.ObjectMapper.Dynamic;
1918
import org.elasticsearch.script.ScriptCompiler;
2019
import org.elasticsearch.xcontent.XContentParser;
@@ -412,11 +411,7 @@ public boolean newDynamicDateField(DocumentParserContext context, String name, D
412411
DateFieldMapper.Resolution.MILLISECONDS,
413412
dateTimeFormatter,
414413
ScriptCompiler.NONE,
415-
ignoreMalformed,
416-
context.indexSettings().getMode(),
417-
context.indexSettings().getIndexSortConfig(),
418-
context.indexSettings().getIndexVersionCreated(),
419-
IndexSettings.USE_DOC_VALUES_SKIPPER.get(context.indexSettings().getSettings())
414+
context.indexSettings()
420415
),
421416
context
422417
);

server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import static org.hamcrest.Matchers.equalTo;
5757
import static org.hamcrest.Matchers.greaterThan;
5858
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
59-
import static org.hamcrest.Matchers.in;
6059
import static org.hamcrest.Matchers.instanceOf;
6160
import static org.hamcrest.Matchers.lessThan;
6261
import static org.hamcrest.Matchers.lessThanOrEqualTo;
@@ -756,19 +755,28 @@ public void testLegacyField() throws Exception {
756755
assertNotEquals(DEFAULT_DATE_TIME_FORMATTER, ((DateFieldType) service.fieldType("mydate")).dateTimeFormatter);
757756
}
758757

758+
private static IndexSettings buildIndexSettings(IndexVersion indexVersion) {
759+
return new IndexSettings(
760+
new IndexMetadata.Builder("index").settings(indexSettings(indexVersion, 1, 1).build()).build(),
761+
Settings.EMPTY
762+
);
763+
}
764+
759765
public void testLegacyDateFormatName() {
766+
767+
// BWC compatible index, e.g 7.x
768+
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(
769+
random(),
770+
IndexVersions.V_7_0_0,
771+
IndexVersionUtils.getPreviousVersion(IndexVersions.V_8_0_0)
772+
);
773+
760774
DateFieldMapper.Builder builder = new DateFieldMapper.Builder(
761775
"format",
762776
DateFieldMapper.Resolution.MILLISECONDS,
763777
null,
764778
mock(ScriptService.class),
765-
true,
766-
// BWC compatible index, e.g 7.x
767-
IndexVersionUtils.randomVersionBetween(
768-
random(),
769-
IndexVersions.V_7_0_0,
770-
IndexVersionUtils.getPreviousVersion(IndexVersions.V_8_0_0)
771-
)
779+
buildIndexSettings(indexVersion)
772780
);
773781

774782
// Check that we allow the use of camel case date formats on 7.x indices
@@ -785,8 +793,7 @@ public void testLegacyDateFormatName() {
785793
DateFieldMapper.Resolution.MILLISECONDS,
786794
null,
787795
mock(ScriptService.class),
788-
true,
789-
IndexVersion.current()
796+
buildIndexSettings(IndexVersion.current())
790797
);
791798

792799
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)