Skip to content

Commit 4d2f709

Browse files
authored
Allow disabling ignore_malformed on data stream's timestamp field (#72406)
If `index.mapping.ignore_malformed` has been set to `true` then here is no way to overwrite that to `false` for a data stream's timestamp field. Before this commit, validation would fail that disallow the usage of `ignore_malformed` attribute on a data stream's timestamp field. This commit allows the usage of `ignore_malformed` attribute, so that `index.mapping.ignore_malformed` can be disabled for a data stream's timestamp field. The `ignore_malformed` attribute can only be set to false. This allows the following index template: ``` PUT /_index_template/filebeat { "index_patterns": [ "filebeat-*" ], "template": { "settings": { "index": { "mapping.ignore_malformed": true } }, "mappings": { "properties": { "@timestamp": { "type": "date", "ignore_malformed": false } } } }, "data_stream": {} } ``` Closes #71755
1 parent 17da103 commit 4d2f709

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

x-pack/plugin/data-streams/src/main/java/org/elasticsearch/xpack/datastreams/mapper/DataStreamTimestampFieldMapper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ public void doValidate(MappingLookup lookup) {
167167
configuredSettings.remove("type");
168168
configuredSettings.remove("meta");
169169
configuredSettings.remove("format");
170+
171+
// ignoring malformed values is disallowed (see previous check),
172+
// however if `index.mapping.ignore_malformed` has been set to true then
173+
// there is no way to disable ignore_malformed for the timestamp field mapper,
174+
// other then not using 'index.mapping.ignore_malformed' at all.
175+
// So by ignoring the ignore_malformed here, we allow index.mapping.ignore_malformed
176+
// index setting to be set to true and then turned off for the timestamp field mapper.
177+
// (ignore_malformed will here always be false, otherwise previous check would have failed)
178+
Object value = configuredSettings.remove("ignore_malformed");
179+
assert value == null || Boolean.FALSE.equals(value);
180+
170181
// All other configured attributes are not allowed:
171182
if (configuredSettings.isEmpty() == false) {
172183
throw new IllegalArgumentException(

x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/DataStreamTimestampFieldMapperTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
*/
77
package org.elasticsearch.xpack.datastreams.mapper;
88

9+
import org.elasticsearch.Version;
910
import org.elasticsearch.common.CheckedConsumer;
11+
import org.elasticsearch.common.settings.Settings;
1012
import org.elasticsearch.common.xcontent.XContentBuilder;
13+
import org.elasticsearch.index.mapper.DateFieldMapper;
1114
import org.elasticsearch.index.mapper.DocumentMapper;
15+
import org.elasticsearch.index.mapper.FieldMapper;
1216
import org.elasticsearch.index.mapper.MapperException;
17+
import org.elasticsearch.index.mapper.MapperService;
1318
import org.elasticsearch.index.mapper.MetadataMapperTestCase;
1419
import org.elasticsearch.index.mapper.ParsedDocument;
1520
import org.elasticsearch.plugins.Plugin;
@@ -20,6 +25,8 @@
2025
import java.util.List;
2126

2227
import static org.hamcrest.Matchers.equalTo;
28+
import static org.hamcrest.Matchers.is;
29+
import static org.hamcrest.Matchers.notNullValue;
2330

2431
public class DataStreamTimestampFieldMapperTests extends MetadataMapperTestCase {
2532

@@ -145,4 +152,30 @@ public void testValidateNotDisallowedAttribute() {
145152
})));
146153
assertThat(e.getMessage(), equalTo("data stream timestamp field [@timestamp] has disallowed attributes: [store]"));
147154
}
155+
156+
public void testValidateDefaultIgnoreMalformed() throws Exception {
157+
Settings indexSettings = Settings.builder().put(FieldMapper.IGNORE_MALFORMED_SETTING.getKey(), true).build();
158+
Exception e = expectThrows(
159+
IllegalArgumentException.class,
160+
() -> createMapperService(Version.CURRENT, indexSettings, () -> true, timestampMapping(true, b -> {
161+
b.startObject("@timestamp");
162+
b.field("type", "date");
163+
b.endObject();
164+
}))
165+
);
166+
assertThat(
167+
e.getMessage(),
168+
equalTo("data stream timestamp field [@timestamp] has disallowed [ignore_malformed] attribute specified")
169+
);
170+
171+
MapperService mapperService = createMapperService(Version.CURRENT, indexSettings, () -> true, timestampMapping(true, b -> {
172+
b.startObject("@timestamp");
173+
b.field("type", "date");
174+
b.field("ignore_malformed", false);
175+
b.endObject();
176+
}));
177+
assertThat(mapperService, notNullValue());
178+
assertThat(mapperService.documentMapper().mappers().getMapper("@timestamp"), notNullValue());
179+
assertThat(((DateFieldMapper) mapperService.documentMapper().mappers().getMapper("@timestamp")).getIgnoreMalformed(), is(false));
180+
}
148181
}

0 commit comments

Comments
 (0)