From 4f96b3637643ea2b9e4ddf58b1858c04aea27388 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Thu, 4 Apr 2013 15:55:12 +0200 Subject: [PATCH] Returning configuration of root field mappers toXContent method only if they are enabled --- .../mapper/internal/IndexFieldMapper.java | 2 +- .../mapper/internal/SizeFieldMapper.java | 2 +- .../index/mapper/internal/TTLFieldMapper.java | 2 +- .../mapper/internal/TimestampFieldMapper.java | 24 ++++++++++--------- .../timestamp/TimestampMappingTests.java | 17 +++++++++++++ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java index 245f41545c1..56e75650d73 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java @@ -192,7 +192,7 @@ public class IndexFieldMapper extends AbstractFieldMapper implements Int return builder; } builder.startObject(CONTENT_TYPE); - if (fieldType().stored() != Defaults.FIELD_TYPE.stored()) { + if (fieldType().stored() != Defaults.FIELD_TYPE.stored() && enabledState.enabled) { builder.field("store", fieldType().stored()); } if (enabledState != Defaults.ENABLED_STATE) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java index 9ca97498ac2..e6d9a8efe13 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java @@ -160,7 +160,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { if (enabledState != Defaults.ENABLED_STATE) { builder.field("enabled", enabledState.enabled); } - if (fieldType().stored() != Defaults.SIZE_FIELD_TYPE.stored()) { + if (fieldType().stored() != Defaults.SIZE_FIELD_TYPE.stored() && enabledState.enabled) { builder.field("store", fieldType().stored()); } builder.endObject(); diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java index c29bea23aaa..941c5068001 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java @@ -221,7 +221,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R if (enabledState != Defaults.ENABLED_STATE) { builder.field("enabled", enabledState.enabled); } - if (defaultTTL != Defaults.DEFAULT) { + if (defaultTTL != Defaults.DEFAULT && enabledState.enabled) { builder.field("default", defaultTTL); } builder.endObject(); diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java index a265532f902..252faf20bcd 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java @@ -221,20 +221,22 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap return builder; } builder.startObject(CONTENT_TYPE); - if (fieldType.indexed() != Defaults.FIELD_TYPE.indexed()) { - builder.field("index", fieldType.indexed()); - } - if (fieldType.stored() != Defaults.FIELD_TYPE.stored()) { - builder.field("store", fieldType.stored()); - } if (enabledState != Defaults.ENABLED) { builder.field("enabled", enabledState.enabled); } - if (path != Defaults.PATH) { - builder.field("path", path); - } - if (!dateTimeFormatter.format().equals(Defaults.DATE_TIME_FORMATTER.format())) { - builder.field("format", dateTimeFormatter.format()); + if (enabledState.enabled) { + if (fieldType.indexed() != Defaults.FIELD_TYPE.indexed()) { + builder.field("index", fieldType.indexed()); + } + if (fieldType.stored() != Defaults.FIELD_TYPE.stored()) { + builder.field("store", fieldType.stored()); + } + if (path != Defaults.PATH) { + builder.field("path", path); + } + if (!dateTimeFormatter.format().equals(Defaults.DATE_TIME_FORMATTER.format())) { + builder.field("format", dateTimeFormatter.format()); + } } builder.endObject(); return builder; diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/timestamp/TimestampMappingTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/timestamp/TimestampMappingTests.java index 87e903d0b82..a7ba2bc575d 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/timestamp/TimestampMappingTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/timestamp/TimestampMappingTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.test.unit.index.mapper.timestamp; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.ParsedDocument; @@ -113,4 +114,20 @@ public class TimestampMappingTests { assertThat(enabledMapper.timestampFieldMapper().enabled(), is(false)); } + + @Test + public void testThatDisablingFieldMapperDoesNotReturnAnyUselessInfo() throws Exception { + boolean inversedStoreSetting = !TimestampFieldMapper.Defaults.FIELD_TYPE.stored(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("_timestamp").field("enabled", false).field("store", inversedStoreSetting).endObject() + .endObject().endObject().string(); + + DocumentMapper mapper = MapperTests.newParser().parse(mapping); + + XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); + mapper.timestampFieldMapper().toXContent(builder, null); + builder.endObject(); + + assertThat(builder.string(), is(String.format("{\"%s\":{}}", TimestampFieldMapper.NAME))); + } } \ No newline at end of file