From 841c2d1e14d36e38d49220974c66a6677c4d9203 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 22 May 2013 21:14:19 +0200 Subject: [PATCH] Fix bug in DateFieldMapper where format is serialized instead of locale This fix adds a default serialization step in the SimpleDateMappingTests that parses the mapping, builds the mapper, serializes the mapper and rebuilds the actual mapper from the serialization result. The contained information must be equivalent to the original mapping. The fixed bug has no issue assigned to is since the code is unreleased yet. --- .../index/mapper/core/DateFieldMapper.java | 2 +- .../mapper/date/SimpleDateMappingTests.java | 27 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) 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 5c87b09cb8b..e20afeaa1e4 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -449,7 +449,7 @@ public class DateFieldMapper extends NumberFieldMapper { builder.field("numeric_resolution", timeUnit.name().toLowerCase(Locale.ROOT)); } if (dateTimeFormatter.locale() != null) { - builder.field("locale", dateTimeFormatter.format()); + builder.field("locale", dateTimeFormatter.locale()); } } diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/date/SimpleDateMappingTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/date/SimpleDateMappingTests.java index 6aaf45bbedb..275cebaa5d9 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/date/SimpleDateMappingTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/date/SimpleDateMappingTests.java @@ -38,7 +38,10 @@ import org.apache.lucene.search.NumericRangeFilter; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +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.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperParsingException; @@ -60,7 +63,7 @@ public class SimpleDateMappingTests { .startObject("properties").endObject() .endObject().endObject().string(); - DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); + DocumentMapper defaultMapper = mapper(mapping); defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() .startObject() @@ -124,8 +127,7 @@ public class SimpleDateMappingTests { .endObject() .endObject().endObject().string(); - DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); - + DocumentMapper defaultMapper = mapper(mapping); ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() .startObject() .field("date_field_en", "Wed, 06 Dec 2000 02:55:00 -0800") @@ -137,6 +139,17 @@ public class SimpleDateMappingTests { assertNumericTokensEqual(doc, defaultMapper, "date_field_en", "date_field_default"); } + private DocumentMapper mapper(String mapping) throws IOException { + // we serialize and deserialize the mapping to make sure serialization works just fine + DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); + builder.startObject(); + defaultMapper.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); + String rebuildMapping = builder.string(); + return MapperTests.newParser().parse(rebuildMapping); + } + private void assertNumericTokensEqual(ParsedDocument doc, DocumentMapper defaultMapper, String fieldA, String fieldB) throws IOException { assertThat(doc.rootDoc().getField(fieldA).tokenStream(defaultMapper.indexAnalyzer()), notNullValue()); assertThat(doc.rootDoc().getField(fieldB).tokenStream(defaultMapper.indexAnalyzer()), notNullValue()); @@ -165,7 +178,7 @@ public class SimpleDateMappingTests { .startObject("properties").startObject("date_field").field("type", "date").endObject().endObject() .endObject().endObject().string(); - DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); + DocumentMapper defaultMapper = mapper(mapping); long value = System.currentTimeMillis(); ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() @@ -184,7 +197,7 @@ public class SimpleDateMappingTests { .startObject("properties").startObject("date_field").field("type", "date").endObject().endObject() .endObject().endObject().string(); - DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); + DocumentMapper defaultMapper = mapper(mapping); ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() .startObject() @@ -204,7 +217,7 @@ public class SimpleDateMappingTests { .startObject("properties").startObject("date_field").field("type", "date").field("format", "HH:mm:ss").endObject().endObject() .endObject().endObject().string(); - DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); + DocumentMapper defaultMapper = mapper(mapping); ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() .startObject() @@ -230,7 +243,7 @@ public class SimpleDateMappingTests { .endObject() .endObject().endObject().string(); - DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); + DocumentMapper defaultMapper = mapper(mapping); ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() .startObject()