Allowing to disable size field mapper after enabling

This commit is contained in:
Alexander Reelsen 2013-04-02 12:49:17 +02:00
parent e662e4d55d
commit 955788e9a5
3 changed files with 41 additions and 16 deletions

View File

@ -406,6 +406,10 @@ public class DocumentMapper implements ToXContent {
return rootMapper(IndexFieldMapper.class); return rootMapper(IndexFieldMapper.class);
} }
public SizeFieldMapper SizeFieldMapper() {
return rootMapper(SizeFieldMapper.class);
}
public Analyzer indexAnalyzer() { public Analyzer indexAnalyzer() {
return this.indexAnalyzer; return this.indexAnalyzer;
} }

View File

@ -43,7 +43,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
public static class Defaults extends IntegerFieldMapper.Defaults { public static class Defaults extends IntegerFieldMapper.Defaults {
public static final String NAME = CONTENT_TYPE; public static final String NAME = CONTENT_TYPE;
public static final boolean ENABLED = false; public static final EnabledAttributeMapper ENABLED_STATE = EnabledAttributeMapper.DISABLED;
public static final FieldType SIZE_FIELD_TYPE = new FieldType(IntegerFieldMapper.Defaults.FIELD_TYPE); public static final FieldType SIZE_FIELD_TYPE = new FieldType(IntegerFieldMapper.Defaults.FIELD_TYPE);
@ -54,15 +54,15 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
public static class Builder extends NumberFieldMapper.Builder<Builder, IntegerFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, IntegerFieldMapper> {
protected boolean enabled = Defaults.ENABLED; protected EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED;
public Builder() { public Builder() {
super(Defaults.NAME, new FieldType(Defaults.SIZE_FIELD_TYPE)); super(Defaults.NAME, new FieldType(Defaults.SIZE_FIELD_TYPE));
builder = this; builder = this;
} }
public Builder enabled(boolean enabled) { public Builder enabled(EnabledAttributeMapper enabled) {
this.enabled = enabled; this.enabledState = enabled;
return builder; return builder;
} }
@ -73,7 +73,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
@Override @Override
public SizeFieldMapper build(BuilderContext context) { public SizeFieldMapper build(BuilderContext context) {
return new SizeFieldMapper(enabled, fieldType, provider, fieldDataSettings); return new SizeFieldMapper(enabledState, fieldType, provider, fieldDataSettings);
} }
} }
@ -85,7 +85,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
String fieldName = Strings.toUnderscoreCase(entry.getKey()); String fieldName = Strings.toUnderscoreCase(entry.getKey());
Object fieldNode = entry.getValue(); Object fieldNode = entry.getValue();
if (fieldName.equals("enabled")) { if (fieldName.equals("enabled")) {
builder.enabled(nodeBooleanValue(fieldNode)); builder.enabled(nodeBooleanValue(fieldNode) ? EnabledAttributeMapper.ENABLED : EnabledAttributeMapper.DISABLED);
} else if (fieldName.equals("store")) { } else if (fieldName.equals("store")) {
builder.store(parseStore(fieldName, fieldNode.toString())); builder.store(parseStore(fieldName, fieldNode.toString()));
} }
@ -94,16 +94,16 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
} }
} }
private final boolean enabled; private EnabledAttributeMapper enabledState;
public SizeFieldMapper() { public SizeFieldMapper() {
this(Defaults.ENABLED, new FieldType(Defaults.SIZE_FIELD_TYPE), null, null); this(Defaults.ENABLED_STATE, new FieldType(Defaults.SIZE_FIELD_TYPE), null, null);
} }
public SizeFieldMapper(boolean enabled, FieldType fieldType, PostingsFormatProvider provider, @Nullable Settings fieldDataSettings) { public SizeFieldMapper(EnabledAttributeMapper enabled, FieldType fieldType, PostingsFormatProvider provider, @Nullable Settings fieldDataSettings) {
super(new Names(Defaults.NAME), Defaults.PRECISION_STEP, super(new Names(Defaults.NAME), Defaults.PRECISION_STEP,
Defaults.BOOST, fieldType, Defaults.NULL_VALUE, Defaults.IGNORE_MALFORMED, provider, null, fieldDataSettings); Defaults.BOOST, fieldType, Defaults.NULL_VALUE, Defaults.IGNORE_MALFORMED, provider, null, fieldDataSettings);
this.enabled = enabled; this.enabledState = enabled;
} }
@Override @Override
@ -112,7 +112,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
} }
public boolean enabled() { public boolean enabled() {
return this.enabled; return this.enabledState.enabled;
} }
@Override @Override
@ -141,7 +141,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
@Override @Override
protected Field innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
if (!enabled) { if (!enabledState.enabled) {
return null; return null;
} }
if (context.flyweight()) { if (context.flyweight()) {
@ -153,12 +153,12 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// all are defaults, no need to write it at all // all are defaults, no need to write it at all
if (enabled == Defaults.ENABLED && fieldType().stored() == Defaults.SIZE_FIELD_TYPE.stored()) { if (enabledState == Defaults.ENABLED_STATE && fieldType().stored() == Defaults.SIZE_FIELD_TYPE.stored()) {
return builder; return builder;
} }
builder.startObject(contentType()); builder.startObject(contentType());
if (enabled != Defaults.ENABLED) { if (enabledState != Defaults.ENABLED_STATE) {
builder.field("enabled", enabled); builder.field("enabled", enabledState.enabled);
} }
if (fieldType().stored() != Defaults.SIZE_FIELD_TYPE.stored()) { if (fieldType().stored() != Defaults.SIZE_FIELD_TYPE.stored()) {
builder.field("store", fieldType().stored()); builder.field("store", fieldType().stored());
@ -169,6 +169,11 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
@Override @Override
public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappingException { public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappingException {
// maybe allow to change enabled? But then we need to figure out null for default value SizeFieldMapper sizeFieldMapperMergeWith = (SizeFieldMapper) mergeWith;
if (!mergeContext.mergeFlags().simulate()) {
if (sizeFieldMapperMergeWith.enabledState != enabledState && !sizeFieldMapperMergeWith.enabledState.unset()) {
this.enabledState = sizeFieldMapperMergeWith.enabledState;
}
}
} }
} }

View File

@ -101,4 +101,20 @@ public class SizeMappingTests {
assertThat(doc.rootDoc().getField("_size"), nullValue()); assertThat(doc.rootDoc().getField("_size"), nullValue());
} }
@Test
public void testThatDisablingWorksWhenMerging() throws Exception {
String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_size").field("enabled", true).endObject()
.endObject().endObject().string();
DocumentMapper enabledMapper = MapperTests.newParser().parse(enabledMapping);
String disabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_size").field("enabled", false).endObject()
.endObject().endObject().string();
DocumentMapper disabledMapper = MapperTests.newParser().parse(disabledMapping);
enabledMapper.merge(disabledMapper, DocumentMapper.MergeFlags.mergeFlags().simulate(false));
assertThat(enabledMapper.SizeFieldMapper().enabled(), is(false));
}
} }