From e662e4d55dc8a40df11789e3af2d7c908fa320cd Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Tue, 2 Apr 2013 12:33:57 +0200 Subject: [PATCH] Allowing to disable index field mapper after enabling --- .../index/mapper/DocumentMapper.java | 4 ++ .../mapper/internal/IndexFieldMapper.java | 38 +++++++++++-------- .../mapper/index/IndexTypeMapperTests.java | 17 +++++++++ 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 95701174759..73f0ae4e88e 100644 --- a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -402,6 +402,10 @@ public class DocumentMapper implements ToXContent { return rootMapper(TTLFieldMapper.class); } + public IndexFieldMapper IndexFieldMapper() { + return rootMapper(IndexFieldMapper.class); + } + public Analyzer indexAnalyzer() { return this.indexAnalyzer; } 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 b3a77384de9..245f41545c1 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java @@ -63,26 +63,26 @@ public class IndexFieldMapper extends AbstractFieldMapper implements Int FIELD_TYPE.freeze(); } - public static final boolean ENABLED = false; + public static final EnabledAttributeMapper ENABLED_STATE = EnabledAttributeMapper.DISABLED; } public static class Builder extends AbstractFieldMapper.Builder { - private boolean enabled = Defaults.ENABLED; + private EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED; public Builder() { super(Defaults.NAME, new FieldType(Defaults.FIELD_TYPE)); indexName = Defaults.INDEX_NAME; } - public Builder enabled(boolean enabled) { - this.enabled = enabled; + public Builder enabled(EnabledAttributeMapper enabledState) { + this.enabledState = enabledState; return this; } @Override 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 implements Int String fieldName = Strings.toUnderscoreCase(entry.getKey()); Object fieldNode = entry.getValue(); if (fieldName.equals("enabled")) { - builder.enabled(nodeBooleanValue(fieldNode)); + EnabledAttributeMapper mapper = nodeBooleanValue(fieldNode) ? EnabledAttributeMapper.ENABLED : EnabledAttributeMapper.DISABLED; + builder.enabled(mapper); } } return builder; } } - private final boolean enabled; + private EnabledAttributeMapper enabledState; public IndexFieldMapper() { this(Defaults.NAME, Defaults.INDEX_NAME); } 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) { super(new Names(name, indexName, indexName, name), boost, fieldType, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER, provider, null, fieldDataSettings); - this.enabled = enabled; + this.enabledState = enabledState; } public boolean enabled() { - return this.enabled; + return this.enabledState.enabled; } @Override @@ -173,7 +174,7 @@ public class IndexFieldMapper extends AbstractFieldMapper implements Int @Override protected Field parseCreateField(ParseContext context) throws IOException { - if (!enabled) { + if (!enabledState.enabled) { return null; } return new Field(names.indexName(), context.index(), fieldType); @@ -187,15 +188,15 @@ public class IndexFieldMapper extends AbstractFieldMapper implements Int @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { // 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; } builder.startObject(CONTENT_TYPE); if (fieldType().stored() != Defaults.FIELD_TYPE.stored()) { builder.field("store", fieldType().stored()); } - if (enabled != Defaults.ENABLED) { - builder.field("enabled", enabled); + if (enabledState != Defaults.ENABLED_STATE) { + builder.field("enabled", enabledState.enabled); } builder.endObject(); return builder; @@ -203,6 +204,11 @@ public class IndexFieldMapper extends AbstractFieldMapper implements Int @Override 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; + } + } } } diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/index/IndexTypeMapperTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/index/IndexTypeMapperTests.java index dc260692b1a..2fba0e9857b 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/index/IndexTypeMapperTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/index/IndexTypeMapperTests.java @@ -93,4 +93,21 @@ public class IndexTypeMapperTests { assertThat(doc.rootDoc().get("_index"), nullValue()); 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)); + } }