diff --git a/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index 2e738ba2cf0..fae1ca8d1f4 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -183,7 +183,7 @@ public class DateFieldMapper extends NumberFieldMapper { } } - protected final FormatDateTimeFormatter dateTimeFormatter; + protected FormatDateTimeFormatter dateTimeFormatter; // Triggers rounding up of the upper bound for range queries and filters if // set to true. @@ -444,6 +444,7 @@ public class DateFieldMapper extends NumberFieldMapper { } if (!mergeContext.mergeFlags().simulate()) { this.nullValue = ((DateFieldMapper) mergeWith).nullValue; + this.dateTimeFormatter = ((DateFieldMapper) mergeWith).dateTimeFormatter; } } diff --git a/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java index 9757e82459d..95dff302ce3 100644 --- a/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java @@ -30,6 +30,7 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.*; import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.elasticsearch.index.mapper.core.LongFieldMapper; @@ -40,12 +41,9 @@ import org.joda.time.DateTimeZone; import org.junit.Test; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; +import java.util.*; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; public class SimpleDateMappingTests extends ElasticsearchTestCase { @@ -289,4 +287,50 @@ public class SimpleDateMappingTests extends ElasticsearchTestCase { assertThat(e.getCause(), instanceOf(MapperParsingException.class)); } } + + @Test + public void testThatMergingWorks() throws Exception { + String initialMapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties") + .startObject("field").field("type", "date") + .field("format", "EEE MMM dd HH:mm:ss.S Z yyyy||EEE MMM dd HH:mm:ss.SSS Z yyyy") + .endObject() + .endObject() + .endObject().endObject().string(); + + String updatedMapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties") + .startObject("field") + .field("type", "date") + .field("format", "EEE MMM dd HH:mm:ss.S Z yyyy||EEE MMM dd HH:mm:ss.SSS Z yyyy||yyyy-MM-dd'T'HH:mm:ss.SSSZZ") + .endObject() + .endObject() + .endObject().endObject().string(); + + DocumentMapper defaultMapper = mapper(initialMapping); + DocumentMapper mergeMapper = mapper(updatedMapping); + + assertThat(defaultMapper.mappers().name("field").mapper(), is(instanceOf(DateFieldMapper.class))); + DateFieldMapper initialDateFieldMapper = (DateFieldMapper) defaultMapper.mappers().name("field").mapper(); + Map config = getConfigurationViaXContent(initialDateFieldMapper); + assertThat(config.get("format"), is("EEE MMM dd HH:mm:ss.S Z yyyy||EEE MMM dd HH:mm:ss.SSS Z yyyy")); + + DocumentMapper.MergeResult mergeResult = defaultMapper.merge(mergeMapper, DocumentMapper.MergeFlags.mergeFlags().simulate(false)); + + assertThat("Merging resulting in conflicts: " + Arrays.asList(mergeResult.conflicts()), mergeResult.hasConflicts(), is(false)); + assertThat(defaultMapper.mappers().name("field").mapper(), is(instanceOf(DateFieldMapper.class))); + + DateFieldMapper mergedFieldMapper = (DateFieldMapper) defaultMapper.mappers().name("field").mapper(); + Map mergedConfig = getConfigurationViaXContent(mergedFieldMapper); + assertThat(mergedConfig.get("format"), is("EEE MMM dd HH:mm:ss.S Z yyyy||EEE MMM dd HH:mm:ss.SSS Z yyyy||yyyy-MM-dd'T'HH:mm:ss.SSSZZ")); + } + + private Map getConfigurationViaXContent(DateFieldMapper dateFieldMapper) throws IOException { + XContentBuilder builder = JsonXContent.contentBuilder().startObject(); + dateFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); + Map dateFieldMapperMap = JsonXContent.jsonXContent.createParser(builder.string()).mapAndClose(); + assertThat(dateFieldMapperMap, hasKey("field")); + assertThat(dateFieldMapperMap.get("field"), is(instanceOf(Map.class))); + return (Map) dateFieldMapperMap.get("field"); + } } \ No newline at end of file