Skip to content

Allow disabling ignore_malformed on data stream's timestamp field #72406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ public void doValidate(MappingLookup lookup) {
configuredSettings.remove("type");
configuredSettings.remove("meta");
configuredSettings.remove("format");

// ignoring malformed values is disallowed (see previous check),
// however if `index.mapping.ignore_malformed` has been set to true then
// there is no way to disable ignore_malformed for the timestamp field mapper,
// other then not using 'index.mapping.ignore_malformed' at all.
// So by ignoring the ignore_malformed here, we allow index.mapping.ignore_malformed
// index setting to be set to true and then turned off for the timestamp field mapper.
// (ignore_malformed will here always be false, otherwise previous check would have failed)
Object value = configuredSettings.remove("ignore_malformed");
assert value == null || Boolean.FALSE.equals(value);

// All other configured attributes are not allowed:
if (configuredSettings.isEmpty() == false) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
*/
package org.elasticsearch.xpack.datastreams.mapper;

import org.elasticsearch.Version;
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperException;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MetadataMapperTestCase;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.plugins.Plugin;
Expand All @@ -20,6 +25,8 @@
import java.util.List;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

public class DataStreamTimestampFieldMapperTests extends MetadataMapperTestCase {

Expand Down Expand Up @@ -145,4 +152,30 @@ public void testValidateNotDisallowedAttribute() {
})));
assertThat(e.getMessage(), equalTo("data stream timestamp field [@timestamp] has disallowed attributes: [store]"));
}

public void testValidateDefaultIgnoreMalformed() throws Exception {
Settings indexSettings = Settings.builder().put(FieldMapper.IGNORE_MALFORMED_SETTING.getKey(), true).build();
Exception e = expectThrows(
IllegalArgumentException.class,
() -> createMapperService(Version.CURRENT, indexSettings, () -> true, timestampMapping(true, b -> {
b.startObject("@timestamp");
b.field("type", "date");
b.endObject();
}))
);
assertThat(
e.getMessage(),
equalTo("data stream timestamp field [@timestamp] has disallowed [ignore_malformed] attribute specified")
);

MapperService mapperService = createMapperService(Version.CURRENT, indexSettings, () -> true, timestampMapping(true, b -> {
b.startObject("@timestamp");
b.field("type", "date");
b.field("ignore_malformed", false);
b.endObject();
}));
assertThat(mapperService, notNullValue());
assertThat(mapperService.documentMapper().mappers().getMapper("@timestamp"), notNullValue());
assertThat(((DateFieldMapper) mapperService.documentMapper().mappers().getMapper("@timestamp")).getIgnoreMalformed(), is(false));
}
}