diff --git a/core/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java index d3c9afd2f96..f270ae74ca9 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java @@ -405,12 +405,14 @@ public class RangeFieldMapper extends FieldMapper { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - if (includeDefaults || (fieldType().dateTimeFormatter() != null - && fieldType().dateTimeFormatter().format().equals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format()) == false)) { + if (fieldType().rangeType == RangeType.DATE + && (includeDefaults || (fieldType().dateTimeFormatter() != null + && fieldType().dateTimeFormatter().format().equals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format()) == false))) { builder.field("format", fieldType().dateTimeFormatter().format()); } - if (includeDefaults || (fieldType().dateTimeFormatter() != null - && fieldType().dateTimeFormatter().locale() != Locale.ROOT)) { + if (fieldType().rangeType == RangeType.DATE + && (includeDefaults || (fieldType().dateTimeFormatter() != null + && fieldType().dateTimeFormatter().locale() != Locale.ROOT))) { builder.field("locale", fieldType().dateTimeFormatter().locale()); } if (includeDefaults || coerce.explicit()) { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java index 33dfd900769..c065888f911 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java @@ -20,12 +20,14 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; import java.util.Arrays; import java.util.HashSet; +import java.util.Locale; import static org.elasticsearch.index.query.RangeQueryBuilder.GT_FIELD; import static org.elasticsearch.index.query.RangeQueryBuilder.GTE_FIELD; @@ -317,4 +319,23 @@ public class RangeFieldMapperTests extends AbstractNumericFieldMapperTestCase { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, runnable); assertThat(e.getMessage(), containsString("should not define a dateTimeFormatter")); } + + public void testSerializeDefaults() throws Exception { + for (String type : TYPES) { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("field").field("type", type).endObject().endObject() + .endObject().endObject().string(); + + DocumentMapper docMapper = parser.parse("type", new CompressedXContent(mapping)); + RangeFieldMapper mapper = (RangeFieldMapper) docMapper.root().getMapper("field"); + XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); + mapper.doXContentBody(builder, true, ToXContent.EMPTY_PARAMS); + String got = builder.endObject().string(); + + // if type is date_range we check that the mapper contains the default format and locale + // otherwise it should not contain a locale or format + assertTrue(got, got.contains("\"format\":\"strict_date_optional_time||epoch_millis\"") == type.equals("date_range")); + assertTrue(got, got.contains("\"locale\":" + "\"" + Locale.ROOT + "\"") == type.equals("date_range")); + } + } }