Allowing to disable index field mapper after enabling

This commit is contained in:
Alexander Reelsen 2013-04-02 12:33:57 +02:00
parent 9cc2563d5e
commit e662e4d55d
3 changed files with 43 additions and 16 deletions

View File

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

View File

@ -63,26 +63,26 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final boolean ENABLED = false; public static final EnabledAttributeMapper ENABLED_STATE = EnabledAttributeMapper.DISABLED;
} }
public static class Builder extends AbstractFieldMapper.Builder<Builder, IndexFieldMapper> { public static class Builder extends AbstractFieldMapper.Builder<Builder, IndexFieldMapper> {
private boolean enabled = Defaults.ENABLED; private EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED;
public Builder() { public Builder() {
super(Defaults.NAME, new FieldType(Defaults.FIELD_TYPE)); super(Defaults.NAME, new FieldType(Defaults.FIELD_TYPE));
indexName = Defaults.INDEX_NAME; indexName = Defaults.INDEX_NAME;
} }
public Builder enabled(boolean enabled) { public Builder enabled(EnabledAttributeMapper enabledState) {
this.enabled = enabled; this.enabledState = enabledState;
return this; return this;
} }
@Override @Override
public IndexFieldMapper build(BuilderContext context) { public IndexFieldMapper build(BuilderContext context) {
return new IndexFieldMapper(name, indexName, boost, fieldType, enabled, provider, fieldDataSettings); return new IndexFieldMapper(name, indexName, boost, fieldType, enabledState, provider, fieldDataSettings);
} }
} }
@ -96,32 +96,33 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
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)); EnabledAttributeMapper mapper = nodeBooleanValue(fieldNode) ? EnabledAttributeMapper.ENABLED : EnabledAttributeMapper.DISABLED;
builder.enabled(mapper);
} }
} }
return builder; return builder;
} }
} }
private final boolean enabled; private EnabledAttributeMapper enabledState;
public IndexFieldMapper() { public IndexFieldMapper() {
this(Defaults.NAME, Defaults.INDEX_NAME); this(Defaults.NAME, Defaults.INDEX_NAME);
} }
protected IndexFieldMapper(String name, String indexName) { protected IndexFieldMapper(String name, String indexName) {
this(name, indexName, Defaults.BOOST, new FieldType(Defaults.FIELD_TYPE), Defaults.ENABLED, null, null); this(name, indexName, Defaults.BOOST, new FieldType(Defaults.FIELD_TYPE), Defaults.ENABLED_STATE, null, null);
} }
public IndexFieldMapper(String name, String indexName, float boost, FieldType fieldType, boolean enabled, public IndexFieldMapper(String name, String indexName, float boost, FieldType fieldType, EnabledAttributeMapper enabledState,
PostingsFormatProvider provider, @Nullable Settings fieldDataSettings) { PostingsFormatProvider provider, @Nullable Settings fieldDataSettings) {
super(new Names(name, indexName, indexName, name), boost, fieldType, Lucene.KEYWORD_ANALYZER, super(new Names(name, indexName, indexName, name), boost, fieldType, Lucene.KEYWORD_ANALYZER,
Lucene.KEYWORD_ANALYZER, provider, null, fieldDataSettings); Lucene.KEYWORD_ANALYZER, provider, null, fieldDataSettings);
this.enabled = enabled; this.enabledState = enabledState;
} }
public boolean enabled() { public boolean enabled() {
return this.enabled; return this.enabledState.enabled;
} }
@Override @Override
@ -173,7 +174,7 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
@Override @Override
protected Field parseCreateField(ParseContext context) throws IOException { protected Field parseCreateField(ParseContext context) throws IOException {
if (!enabled) { if (!enabledState.enabled) {
return null; return null;
} }
return new Field(names.indexName(), context.index(), fieldType); return new Field(names.indexName(), context.index(), fieldType);
@ -187,15 +188,15 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// if all defaults, no need to write it at all // if all defaults, no need to write it at all
if (fieldType().stored() == Defaults.FIELD_TYPE.stored() && enabled == Defaults.ENABLED) { if (fieldType().stored() == Defaults.FIELD_TYPE.stored() && enabledState == Defaults.ENABLED_STATE) {
return builder; return builder;
} }
builder.startObject(CONTENT_TYPE); builder.startObject(CONTENT_TYPE);
if (fieldType().stored() != Defaults.FIELD_TYPE.stored()) { if (fieldType().stored() != Defaults.FIELD_TYPE.stored()) {
builder.field("store", fieldType().stored()); builder.field("store", fieldType().stored());
} }
if (enabled != Defaults.ENABLED) { if (enabledState != Defaults.ENABLED_STATE) {
builder.field("enabled", enabled); builder.field("enabled", enabledState.enabled);
} }
builder.endObject(); builder.endObject();
return builder; return builder;
@ -203,6 +204,11 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
@Override @Override
public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappingException { public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappingException {
// do nothing here, no merging, but also no exception IndexFieldMapper indexFieldMapperMergeWith = (IndexFieldMapper) mergeWith;
if (!mergeContext.mergeFlags().simulate()) {
if (indexFieldMapperMergeWith.enabledState != enabledState && !indexFieldMapperMergeWith.enabledState.unset()) {
this.enabledState = indexFieldMapperMergeWith.enabledState;
}
}
} }
} }

View File

@ -93,4 +93,21 @@ public class IndexTypeMapperTests {
assertThat(doc.rootDoc().get("_index"), nullValue()); assertThat(doc.rootDoc().get("_index"), nullValue());
assertThat(doc.rootDoc().get("field"), equalTo("value")); assertThat(doc.rootDoc().get("field"), equalTo("value"));
} }
@Test
public void testThatMergingFieldMappingAllowsDisabling() throws Exception {
String mappingWithIndexEnabled = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_index").field("enabled", true).field("store", "yes").endObject()
.endObject().endObject().string();
DocumentMapper mapperEnabled = MapperTests.newParser().parse(mappingWithIndexEnabled);
String mappingWithIndexDisabled = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_index").field("enabled", false).field("store", "yes").endObject()
.endObject().endObject().string();
DocumentMapper mapperDisabled = MapperTests.newParser().parse(mappingWithIndexDisabled);
mapperEnabled.merge(mapperDisabled, DocumentMapper.MergeFlags.mergeFlags().simulate(false));
assertThat(mapperEnabled.IndexFieldMapper().enabled(), is(false));
}
} }