Returning configuration of root field mappers toXContent method only if they are enabled

This commit is contained in:
Alexander Reelsen 2013-04-04 15:55:12 +02:00
parent fbdf89c636
commit 4f96b36376
5 changed files with 33 additions and 14 deletions

View File

@ -192,7 +192,7 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> 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) {

View File

@ -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();

View File

@ -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();

View File

@ -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;

View File

@ -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)));
}
}