From 4b3dba527b4a82cce82581a5bd3a2fdd4865fa83 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Tue, 30 Jun 2015 13:31:17 -0700 Subject: [PATCH] Mappings: Rename "root" mappers to "metadata" mappers "Root" is a very confusing term for meta field mappers. This change renames "RootMapper" to "MetadataFieldMapper" and simplifies how metadata mappers are setup. It also requires that metadata mappers are now a FieldMapper (MetadataFieldMapper extends from AbstractFieldMapper). The only use of a root mapper that wasn't a field mapper was the theoretical "external" root mapper (just a test mapper). But it doesn't make sense to not have an actual field, and this falls inline with the hopefully eventual collapsing of AbstractFieldMapper/FieldMapper/Mapper. --- .../index/mapper/DocumentMapper.java | 21 ++-- .../index/mapper/DocumentMapperParser.java | 2 +- .../index/mapper/DocumentParser.java | 11 +- .../index/mapper/MapperService.java | 6 +- .../elasticsearch/index/mapper/Mapping.java | 36 +++---- .../index/mapper/MetadataFieldMapper.java | 55 ++++++++++ .../index/mapper/RootMapper.java | 42 -------- .../index/mapper/core/ByteFieldMapper.java | 11 +- .../index/mapper/core/DateFieldMapper.java | 2 +- .../index/mapper/core/DoubleFieldMapper.java | 11 +- .../index/mapper/core/FloatFieldMapper.java | 11 +- .../index/mapper/core/IntegerFieldMapper.java | 11 +- .../index/mapper/core/LongFieldMapper.java | 11 +- .../index/mapper/core/NumberFieldMapper.java | 101 +++++++++--------- .../index/mapper/core/ShortFieldMapper.java | 11 +- .../index/mapper/internal/AllFieldMapper.java | 6 +- .../internal/FieldNamesFieldMapper.java | 8 +- .../index/mapper/internal/IdFieldMapper.java | 6 +- .../mapper/internal/IndexFieldMapper.java | 7 +- .../mapper/internal/ParentFieldMapper.java | 8 +- .../mapper/internal/RoutingFieldMapper.java | 7 +- .../mapper/internal/SizeFieldMapper.java | 38 +++---- .../mapper/internal/SourceFieldMapper.java | 8 +- .../index/mapper/internal/TTLFieldMapper.java | 58 +++++----- .../mapper/internal/TimestampFieldMapper.java | 56 +++++----- .../mapper/internal/TypeFieldMapper.java | 6 +- .../index/mapper/internal/UidFieldMapper.java | 6 +- .../mapper/internal/VersionFieldMapper.java | 9 +- .../index/mapper/ip/IpFieldMapper.java | 2 +- .../index/mapper/object/ObjectMapper.java | 6 +- .../index/engine/InternalEngineTests.java | 2 +- ...apper.java => ExternalMetadataMapper.java} | 55 ++++++++-- .../ExternalValuesMapperIntegrationTests.java | 5 +- .../externalvalues/RegisterExternalTypes.java | 2 +- .../SimpleExternalMappingTests.java | 8 +- 35 files changed, 334 insertions(+), 311 deletions(-) create mode 100644 core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java delete mode 100644 core/src/main/java/org/elasticsearch/index/mapper/RootMapper.java rename core/src/test/java/org/elasticsearch/index/mapper/externalvalues/{ExternalRootMapper.java => ExternalMetadataMapper.java} (57%) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 0357f8299b9..de201941ed7 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -41,7 +41,6 @@ import org.elasticsearch.common.util.concurrent.ReleasableLock; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.Mapping.SourceTransform; import org.elasticsearch.index.mapper.internal.AllFieldMapper; import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper; @@ -81,7 +80,7 @@ public class DocumentMapper implements ToXContent { public static class Builder { - private Map, RootMapper> rootMappers = new LinkedHashMap<>(); + private Map, MetadataFieldMapper> rootMappers = new LinkedHashMap<>(); private List sourceTransforms = new ArrayList<>(1); @@ -128,9 +127,9 @@ public class DocumentMapper implements ToXContent { return this; } - public Builder put(RootMapper.Builder mapper) { - RootMapper rootMapper = (RootMapper) mapper.build(builderContext); - rootMappers.put(rootMapper.getClass(), rootMapper); + public Builder put(MetadataFieldMapper.Builder mapper) { + MetadataFieldMapper metadataMapper = mapper.build(builderContext); + rootMappers.put(metadataMapper.getClass(), metadataMapper); return this; } @@ -180,7 +179,7 @@ public class DocumentMapper implements ToXContent { public DocumentMapper(MapperService mapperService, String index, @Nullable Settings indexSettings, DocumentMapperParser docMapperParser, RootObjectMapper rootObjectMapper, ImmutableMap meta, - Map, RootMapper> rootMappers, + Map, MetadataFieldMapper> rootMappers, List sourceTransforms, ReentrantReadWriteLock mappingLock) { this.mapperService = mapperService; @@ -189,7 +188,7 @@ public class DocumentMapper implements ToXContent { this.mapping = new Mapping( Version.indexCreated(indexSettings), rootObjectMapper, - rootMappers.values().toArray(new RootMapper[rootMappers.values().size()]), + rootMappers.values().toArray(new MetadataFieldMapper[rootMappers.values().size()]), sourceTransforms.toArray(new SourceTransform[sourceTransforms.size()]), meta); this.documentParser = new DocumentParser(index, indexSettings, docMapperParser, this, new ReleasableLock(mappingLock.readLock())); @@ -206,9 +205,9 @@ public class DocumentMapper implements ToXContent { // collect all the mappers for this type List newObjectMappers = new ArrayList<>(); List newFieldMappers = new ArrayList<>(); - for (RootMapper rootMapper : this.mapping.rootMappers) { - if (rootMapper instanceof FieldMapper) { - newFieldMappers.add((FieldMapper) rootMapper); + for (MetadataFieldMapper metadataMapper : this.mapping.metadataMappers) { + if (metadataMapper instanceof FieldMapper) { + newFieldMappers.add((FieldMapper) metadataMapper); } } MapperUtils.collect(this.mapping.root, newObjectMappers, newFieldMappers); @@ -258,7 +257,7 @@ public class DocumentMapper implements ToXContent { } @SuppressWarnings({"unchecked"}) - public T rootMapper(Class type) { + public T rootMapper(Class type) { return mapping.rootMapper(type); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java b/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java index 90a77d1f248..c71a2a9ecfa 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java @@ -255,7 +255,7 @@ public class DocumentMapperParser extends AbstractIndexComponent { if (typeParser != null) { iterator.remove(); Map fieldNodeMap = (Map) fieldNode; - docBuilder.put(typeParser.parse(fieldName, fieldNodeMap, parserContext)); + docBuilder.put((MetadataFieldMapper.Builder)typeParser.parse(fieldName, fieldNodeMap, parserContext)); fieldNodeMap.remove("type"); checkNoRemainingFields(fieldName, fieldNodeMap, parserContext.indexVersionCreated()); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java index 9e73d2a5367..523be1abddd 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -33,12 +33,9 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.mapper.core.NumberFieldMapper; import org.elasticsearch.index.mapper.core.StringFieldMapper; -import org.elasticsearch.index.mapper.core.AbstractFieldMapper.Builder; import org.elasticsearch.index.mapper.core.DateFieldMapper.DateFieldType; -import org.elasticsearch.index.mapper.core.LongFieldMapper.LongFieldType; import org.elasticsearch.index.mapper.core.StringFieldMapper.StringFieldType; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import org.elasticsearch.index.mapper.internal.UidFieldMapper; @@ -117,8 +114,8 @@ class DocumentParser implements Closeable { throw new MapperParsingException("Malformed content, after first object, either the type field or the actual properties should exist"); } - for (RootMapper rootMapper : mapping.rootMappers) { - rootMapper.preParse(context); + for (MetadataFieldMapper metadataMapper : mapping.metadataMappers) { + metadataMapper.preParse(context); } if (!emptyDoc) { @@ -132,8 +129,8 @@ class DocumentParser implements Closeable { parser.nextToken(); } - for (RootMapper rootMapper : mapping.rootMappers) { - rootMapper.postParse(context); + for (MetadataFieldMapper metadataMapper : mapping.metadataMappers) { + metadataMapper.postParse(context); } } catch (Throwable e) { // if its already a mapper parsing exception, no need to wrap it... diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 946ee5b7a17..468176e40b4 100755 --- a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -296,10 +296,8 @@ public class MapperService extends AbstractIndexComponent { } else { List newObjectMappers = new ArrayList<>(); List newFieldMappers = new ArrayList<>(); - for (RootMapper rootMapper : mapper.mapping().rootMappers) { - if (rootMapper instanceof FieldMapper) { - newFieldMappers.add((FieldMapper) rootMapper); - } + for (MetadataFieldMapper metadataMapper : mapper.mapping().metadataMappers) { + newFieldMappers.add(metadataMapper); } MapperUtils.collect(mapper.mapping().root, newObjectMappers, newFieldMappers); checkNewMappersCompatibility(newObjectMappers, newFieldMappers, updateAllTypes); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java b/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java index e1e0752c077..6bd6acc3144 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java @@ -56,24 +56,24 @@ public final class Mapping implements ToXContent { final Version indexCreated; final RootObjectMapper root; - final RootMapper[] rootMappers; - final ImmutableMap, RootMapper> rootMappersMap; + final MetadataFieldMapper[] metadataMappers; + final ImmutableMap, MetadataFieldMapper> rootMappersMap; final SourceTransform[] sourceTransforms; volatile ImmutableMap meta; - public Mapping(Version indexCreated, RootObjectMapper rootObjectMapper, RootMapper[] rootMappers, SourceTransform[] sourceTransforms, ImmutableMap meta) { + public Mapping(Version indexCreated, RootObjectMapper rootObjectMapper, MetadataFieldMapper[] metadataMappers, SourceTransform[] sourceTransforms, ImmutableMap meta) { this.indexCreated = indexCreated; this.root = rootObjectMapper; - this.rootMappers = rootMappers; - ImmutableMap.Builder, RootMapper> builder = ImmutableMap.builder(); - for (RootMapper rootMapper : rootMappers) { - if (indexCreated.before(Version.V_2_0_0) && LEGACY_INCLUDE_IN_OBJECT.contains(rootMapper.name())) { - root.putMapper(rootMapper); + this.metadataMappers = metadataMappers; + ImmutableMap.Builder, MetadataFieldMapper> builder = ImmutableMap.builder(); + for (MetadataFieldMapper metadataMapper : metadataMappers) { + if (indexCreated.before(Version.V_2_0_0) && LEGACY_INCLUDE_IN_OBJECT.contains(metadataMapper.name())) { + root.putMapper(metadataMapper); } - builder.put(rootMapper.getClass(), rootMapper); + builder.put(metadataMapper.getClass(), metadataMapper); } // keep root mappers sorted for consistent serialization - Arrays.sort(rootMappers, new Comparator() { + Arrays.sort(metadataMappers, new Comparator() { @Override public int compare(Mapper o1, Mapper o2) { return o1.name().compareTo(o2.name()); @@ -93,24 +93,24 @@ public final class Mapping implements ToXContent { * Generate a mapping update for the given root object mapper. */ public Mapping mappingUpdate(Mapper rootObjectMapper) { - return new Mapping(indexCreated, (RootObjectMapper) rootObjectMapper, rootMappers, sourceTransforms, meta); + return new Mapping(indexCreated, (RootObjectMapper) rootObjectMapper, metadataMappers, sourceTransforms, meta); } /** Get the root mapper with the given class. */ @SuppressWarnings("unchecked") - public T rootMapper(Class clazz) { + public T rootMapper(Class clazz) { return (T) rootMappersMap.get(clazz); } /** @see DocumentMapper#merge(Mapping, boolean) */ public void merge(Mapping mergeWith, MergeResult mergeResult) { - assert rootMappers.length == mergeWith.rootMappers.length; + assert metadataMappers.length == mergeWith.metadataMappers.length; root.merge(mergeWith.root, mergeResult); - for (RootMapper rootMapper : rootMappers) { - RootMapper mergeWithRootMapper = mergeWith.rootMapper(rootMapper.getClass()); - if (mergeWithRootMapper != null) { - rootMapper.merge(mergeWithRootMapper, mergeResult); + for (MetadataFieldMapper metadataMapper : metadataMappers) { + MetadataFieldMapper mergeWithMetadataMapper = mergeWith.rootMapper(metadataMapper.getClass()); + if (mergeWithMetadataMapper != null) { + metadataMapper.merge(mergeWithMetadataMapper, mergeResult); } } @@ -141,7 +141,7 @@ public final class Mapping implements ToXContent { if (meta != null && !meta.isEmpty()) { builder.field("_meta", meta); } - for (Mapper mapper : rootMappers) { + for (Mapper mapper : metadataMappers) { mapper.toXContent(builder, params); } return builder; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java new file mode 100644 index 00000000000..9764d7cf351 --- /dev/null +++ b/core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java @@ -0,0 +1,55 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.mapper; + +import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.mapper.core.AbstractFieldMapper; +import org.elasticsearch.index.mapper.object.RootObjectMapper; + +import java.io.IOException; + + +/** + * A mapper for a builtin field containing metadata about a document. + */ +public abstract class MetadataFieldMapper extends AbstractFieldMapper { + + public abstract static class Builder extends AbstractFieldMapper.Builder { + public Builder(String name, MappedFieldType fieldType) { + super(name, fieldType); + } + } + + protected MetadataFieldMapper(MappedFieldType fieldType, Boolean docValues, @Nullable Settings fieldDataSettings, Settings indexSettings) { + super(fieldType, docValues, fieldDataSettings, indexSettings, MultiFields.empty(), null); + } + + /** + * Called before {@link FieldMapper#parse(ParseContext)} on the {@link RootObjectMapper}. + */ + public abstract void preParse(ParseContext context) throws IOException; + + /** + * Called after {@link FieldMapper#parse(ParseContext)} on the {@link RootObjectMapper}. + */ + public abstract void postParse(ParseContext context) throws IOException; + +} diff --git a/core/src/main/java/org/elasticsearch/index/mapper/RootMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/RootMapper.java deleted file mode 100644 index 0579178efc4..00000000000 --- a/core/src/main/java/org/elasticsearch/index/mapper/RootMapper.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.mapper; - -import org.elasticsearch.index.mapper.object.RootObjectMapper; - -import java.io.IOException; - - -/** - * A mapper that exists only as a mapper within a root object. - */ -public interface RootMapper extends Mapper { - - /** - * Called before {@link #parse(ParseContext)} on the {@link RootObjectMapper}. - */ - void preParse(ParseContext context) throws IOException; - - /** - * Called after {@link #parse(ParseContext)} on the {@link RootObjectMapper}. - */ - void postParse(ParseContext context) throws IOException; - -} diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java index 4448381296f..c24d7bbc413 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java @@ -299,7 +299,7 @@ public class ByteFieldMapper extends NumberFieldMapper { } } if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomByteNumericField field = new CustomByteNumericField(this, value, fieldType()); + CustomByteNumericField field = new CustomByteNumericField(value, fieldType()); field.setBoost(boost); fields.add(field); } @@ -334,18 +334,15 @@ public class ByteFieldMapper extends NumberFieldMapper { private final byte number; - private final NumberFieldMapper mapper; - - public CustomByteNumericField(NumberFieldMapper mapper, byte number, MappedFieldType fieldType) { - super(mapper, number, fieldType); - this.mapper = mapper; + public CustomByteNumericField(byte number, MappedFieldType fieldType) { + super(number, fieldType); this.number = number; } @Override public TokenStream tokenStream(Analyzer analyzer, TokenStream previous) { if (fieldType().indexOptions() != IndexOptions.NONE) { - return mapper.popCachedStream().setIntValue(number); + return getCachedStream().setIntValue(number); } return null; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index 18a9af639b5..729b6008efe 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -502,7 +502,7 @@ public class DateFieldMapper extends NumberFieldMapper { if (value != null) { if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomLongNumericField field = new CustomLongNumericField(this, value, fieldType()); + CustomLongNumericField field = new CustomLongNumericField(value, fieldType()); field.setBoost(boost); fields.add(field); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java index d0af749156c..c98126ad929 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java @@ -297,7 +297,7 @@ public class DoubleFieldMapper extends NumberFieldMapper { } if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomDoubleNumericField field = new CustomDoubleNumericField(this, value, fieldType()); + CustomDoubleNumericField field = new CustomDoubleNumericField(value, fieldType()); field.setBoost(boost); fields.add(field); } @@ -343,18 +343,15 @@ public class DoubleFieldMapper extends NumberFieldMapper { private final double number; - private final NumberFieldMapper mapper; - - public CustomDoubleNumericField(NumberFieldMapper mapper, double number, NumberFieldType fieldType) { - super(mapper, number, fieldType); - this.mapper = mapper; + public CustomDoubleNumericField(double number, NumberFieldType fieldType) { + super(number, fieldType); this.number = number; } @Override public TokenStream tokenStream(Analyzer analyzer, TokenStream previous) throws IOException { if (fieldType().indexOptions() != IndexOptions.NONE) { - return mapper.popCachedStream().setDoubleValue(number); + return getCachedStream().setDoubleValue(number); } return null; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java index 9cfaa8b8999..c123140e177 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java @@ -309,7 +309,7 @@ public class FloatFieldMapper extends NumberFieldMapper { } if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomFloatNumericField field = new CustomFloatNumericField(this, value, fieldType()); + CustomFloatNumericField field = new CustomFloatNumericField(value, fieldType()); field.setBoost(boost); fields.add(field); } @@ -355,18 +355,15 @@ public class FloatFieldMapper extends NumberFieldMapper { private final float number; - private final NumberFieldMapper mapper; - - public CustomFloatNumericField(NumberFieldMapper mapper, float number, NumberFieldType fieldType) { - super(mapper, number, fieldType); - this.mapper = mapper; + public CustomFloatNumericField(float number, NumberFieldType fieldType) { + super(number, fieldType); this.number = number; } @Override public TokenStream tokenStream(Analyzer analyzer, TokenStream previous) throws IOException { if (fieldType().indexOptions() != IndexOptions.NONE) { - return mapper.popCachedStream().setFloatValue(number); + return getCachedStream().setFloatValue(number); } return null; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java index f4958ee2ee0..ed6e71b8df1 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java @@ -313,7 +313,7 @@ public class IntegerFieldMapper extends NumberFieldMapper { protected void addIntegerFields(ParseContext context, List fields, int value, float boost) { if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomIntegerNumericField field = new CustomIntegerNumericField(this, value, fieldType()); + CustomIntegerNumericField field = new CustomIntegerNumericField(value, fieldType()); field.setBoost(boost); fields.add(field); } @@ -349,18 +349,15 @@ public class IntegerFieldMapper extends NumberFieldMapper { private final int number; - private final NumberFieldMapper mapper; - - public CustomIntegerNumericField(NumberFieldMapper mapper, int number, MappedFieldType fieldType) { - super(mapper, number, fieldType); - this.mapper = mapper; + public CustomIntegerNumericField(int number, MappedFieldType fieldType) { + super(number, fieldType); this.number = number; } @Override public TokenStream tokenStream(Analyzer analyzer, TokenStream previous) throws IOException { if (fieldType().indexOptions() != IndexOptions.NONE) { - return mapper.popCachedStream().setIntValue(number); + return getCachedStream().setIntValue(number); } return null; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java index 18596404c86..d4567cfd07d 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java @@ -298,7 +298,7 @@ public class LongFieldMapper extends NumberFieldMapper { } } if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomLongNumericField field = new CustomLongNumericField(this, value, fieldType()); + CustomLongNumericField field = new CustomLongNumericField(value, fieldType()); field.setBoost(boost); fields.add(field); } @@ -333,18 +333,15 @@ public class LongFieldMapper extends NumberFieldMapper { private final long number; - private final NumberFieldMapper mapper; - - public CustomLongNumericField(NumberFieldMapper mapper, long number, MappedFieldType fieldType) { - super(mapper, number, fieldType); - this.mapper = mapper; + public CustomLongNumericField(long number, MappedFieldType fieldType) { + super(number, fieldType); this.number = number; } @Override public TokenStream tokenStream(Analyzer analyzer, TokenStream previous) throws IOException { if (fieldType().indexOptions() != IndexOptions.NONE) { - return mapper.popCachedStream().setLongValue(number); + return getCachedStream().setLongValue(number); } return null; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java index 5b086ba65d4..b5d5d186349 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java @@ -184,41 +184,6 @@ public abstract class NumberFieldMapper extends AbstractFieldMapper implements A * otherwise for older indexes we must continue to write BINARY (for now) */ protected final boolean useSortedNumericDocValues; - - private ThreadLocal tokenStream = new ThreadLocal() { - @Override - protected NumericTokenStream initialValue() { - return new NumericTokenStream(fieldType().numericPrecisionStep()); - } - }; - - private static ThreadLocal tokenStream4 = new ThreadLocal() { - @Override - protected NumericTokenStream initialValue() { - return new NumericTokenStream(4); - } - }; - - private static ThreadLocal tokenStream8 = new ThreadLocal() { - @Override - protected NumericTokenStream initialValue() { - return new NumericTokenStream(8); - } - }; - - private static ThreadLocal tokenStream16 = new ThreadLocal() { - @Override - protected NumericTokenStream initialValue() { - return new NumericTokenStream(16); - } - }; - - private static ThreadLocal tokenStreamMax = new ThreadLocal() { - @Override - protected NumericTokenStream initialValue() { - return new NumericTokenStream(Integer.MAX_VALUE); - } - }; protected NumberFieldMapper(MappedFieldType fieldType, Boolean docValues, Explicit ignoreMalformed, Explicit coerce, @Nullable Settings fieldDataSettings, Settings indexSettings, @@ -335,32 +300,64 @@ public abstract class NumberFieldMapper extends AbstractFieldMapper implements A } } - protected NumericTokenStream popCachedStream() { - if (fieldType().numericPrecisionStep() == 4) { - return tokenStream4.get(); - } else if (fieldType().numericPrecisionStep() == 8) { - return tokenStream8.get(); - } else if (fieldType().numericPrecisionStep() == 16) { - return tokenStream16.get(); - } else if (fieldType().numericPrecisionStep() == Integer.MAX_VALUE) { - return tokenStreamMax.get(); - } - return tokenStream.get(); - } - // used to we can use a numeric field in a document that is then parsed twice! public abstract static class CustomNumericField extends Field { - protected final NumberFieldMapper mapper; + private ThreadLocal tokenStream = new ThreadLocal() { + @Override + protected NumericTokenStream initialValue() { + return new NumericTokenStream(fieldType().numericPrecisionStep()); + } + }; - public CustomNumericField(NumberFieldMapper mapper, Number value, MappedFieldType fieldType) { - super(mapper.fieldType().names().indexName(), fieldType); - this.mapper = mapper; + private static ThreadLocal tokenStream4 = new ThreadLocal() { + @Override + protected NumericTokenStream initialValue() { + return new NumericTokenStream(4); + } + }; + + private static ThreadLocal tokenStream8 = new ThreadLocal() { + @Override + protected NumericTokenStream initialValue() { + return new NumericTokenStream(8); + } + }; + + private static ThreadLocal tokenStream16 = new ThreadLocal() { + @Override + protected NumericTokenStream initialValue() { + return new NumericTokenStream(16); + } + }; + + private static ThreadLocal tokenStreamMax = new ThreadLocal() { + @Override + protected NumericTokenStream initialValue() { + return new NumericTokenStream(Integer.MAX_VALUE); + } + }; + + public CustomNumericField(Number value, MappedFieldType fieldType) { + super(fieldType.names().indexName(), fieldType); if (value != null) { this.fieldsData = value; } } + protected NumericTokenStream getCachedStream() { + if (fieldType().numericPrecisionStep() == 4) { + return tokenStream4.get(); + } else if (fieldType().numericPrecisionStep() == 8) { + return tokenStream8.get(); + } else if (fieldType().numericPrecisionStep() == 16) { + return tokenStream16.get(); + } else if (fieldType().numericPrecisionStep() == Integer.MAX_VALUE) { + return tokenStreamMax.get(); + } + return tokenStream.get(); + } + @Override public String stringValue() { return null; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java index 8522fd026a5..14b97dafbb4 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java @@ -307,7 +307,7 @@ public class ShortFieldMapper extends NumberFieldMapper { } } if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomShortNumericField field = new CustomShortNumericField(this, value, fieldType()); + CustomShortNumericField field = new CustomShortNumericField(value, fieldType()); field.setBoost(boost); fields.add(field); } @@ -343,18 +343,15 @@ public class ShortFieldMapper extends NumberFieldMapper { private final short number; - private final NumberFieldMapper mapper; - - public CustomShortNumericField(NumberFieldMapper mapper, short number, NumberFieldType fieldType) { - super(mapper, number, fieldType); - this.mapper = mapper; + public CustomShortNumericField(short number, NumberFieldType fieldType) { + super(number, fieldType); this.number = number; } @Override public TokenStream tokenStream(Analyzer analyzer, TokenStream previous) throws IOException { if (fieldType().indexOptions() != IndexOptions.NONE) { - return mapper.popCachedStream().setIntValue(number); + return getCachedStream().setIntValue(number); } return null; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java index 73e495b1558..9fee56c0767 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java @@ -40,7 +40,7 @@ import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.similarity.SimilarityLookupService; @@ -57,7 +57,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class AllFieldMapper extends AbstractFieldMapper implements RootMapper { +public class AllFieldMapper extends MetadataFieldMapper { public interface IncludeInAll extends Mapper { @@ -87,7 +87,7 @@ public class AllFieldMapper extends AbstractFieldMapper implements RootMapper { } } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private EnabledAttributeMapper enabled = Defaults.ENABLED; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java index 09f970b3e6e..25fa2676ce1 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java @@ -33,10 +33,8 @@ import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeMappingException; -import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import java.io.IOException; @@ -55,7 +53,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; * * Added in Elasticsearch 1.3. */ -public class FieldNamesFieldMapper extends AbstractFieldMapper implements RootMapper { +public class FieldNamesFieldMapper extends MetadataFieldMapper { public static final String NAME = "_field_names"; @@ -79,7 +77,7 @@ public class FieldNamesFieldMapper extends AbstractFieldMapper implements RootMa } } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private boolean enabled = Defaults.ENABLED; public Builder(MappedFieldType existing) { diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java index 9a52d50c5a4..a26b16885cd 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java @@ -47,7 +47,7 @@ import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.query.QueryParseContext; @@ -63,7 +63,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class IdFieldMapper extends AbstractFieldMapper implements RootMapper { +public class IdFieldMapper extends MetadataFieldMapper { public static final String NAME = "_id"; @@ -87,7 +87,7 @@ public class IdFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String PATH = null; } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private String path = Defaults.PATH; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java index f359f5a1ca8..72b0789abe7 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java @@ -31,12 +31,11 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; -import org.elasticsearch.index.mapper.MapperBuilders; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import java.io.IOException; @@ -50,7 +49,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class IndexFieldMapper extends AbstractFieldMapper implements RootMapper { +public class IndexFieldMapper extends MetadataFieldMapper { public static final String NAME = "_index"; @@ -75,7 +74,7 @@ public class IndexFieldMapper extends AbstractFieldMapper implements RootMapper public static final EnabledAttributeMapper ENABLED_STATE = EnabledAttributeMapper.UNSET_DISABLED; } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java index 08de520247a..aa62917aa36 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java @@ -37,12 +37,11 @@ import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; -import org.elasticsearch.index.mapper.MapperBuilders; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.query.QueryParseContext; @@ -54,14 +53,13 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import static org.elasticsearch.common.settings.Settings.builder; import static org.elasticsearch.common.settings.Settings.settingsBuilder; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeMapValue; /** * */ -public class ParentFieldMapper extends AbstractFieldMapper implements RootMapper { +public class ParentFieldMapper extends MetadataFieldMapper { public static final String NAME = "_parent"; public static final String CONTENT_TYPE = "_parent"; @@ -84,7 +82,7 @@ public class ParentFieldMapper extends AbstractFieldMapper implements RootMapper } } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { protected String indexName; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java index f4e00f9ccf1..715ccc4d1ff 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java @@ -23,7 +23,6 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; import org.elasticsearch.Version; -import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; @@ -35,7 +34,7 @@ import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import java.io.IOException; @@ -49,7 +48,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class RoutingFieldMapper extends AbstractFieldMapper implements RootMapper { +public class RoutingFieldMapper extends MetadataFieldMapper { public static final String NAME = "_routing"; public static final String CONTENT_TYPE = "_routing"; @@ -74,7 +73,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper implements RootMappe public static final String PATH = null; } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private boolean required = Defaults.REQUIRED; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java index f5e9c6594b9..f5222c2b798 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java @@ -21,19 +21,20 @@ package org.elasticsearch.index.mapper.internal; import org.apache.lucene.document.Field; import org.elasticsearch.Version; -import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.analysis.NumericIntegerAnalyzer; +import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; +import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.mapper.core.IntegerFieldMapper; import org.elasticsearch.index.mapper.core.NumberFieldMapper; @@ -45,7 +46,7 @@ import java.util.Map; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; import static org.elasticsearch.index.mapper.core.TypeParsers.parseStore; -public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { +public class SizeFieldMapper extends MetadataFieldMapper { public static final String NAME = "_size"; public static final String CONTENT_TYPE = "_size"; @@ -66,12 +67,12 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { } } - public static class Builder extends NumberFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { protected EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED; public Builder(MappedFieldType existing) { - super(Defaults.NAME, existing == null ? Defaults.SIZE_FIELD_TYPE : existing, Defaults.PRECISION_STEP_32_BIT); + super(Defaults.NAME, existing == null ? Defaults.SIZE_FIELD_TYPE : existing); builder = this; } @@ -85,16 +86,6 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { setupFieldType(context); return new SizeFieldMapper(enabledState, fieldType, context.indexSettings()); } - - @Override - protected NamedAnalyzer makeNumberAnalyzer(int precisionStep) { - return NumericIntegerAnalyzer.buildNamedAnalyzer(precisionStep); - } - - @Override - protected int maxPrecisionStep() { - return 32; - } } public static class TypeParser implements Mapper.TypeParser { @@ -124,8 +115,9 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { } public SizeFieldMapper(EnabledAttributeMapper enabled, MappedFieldType fieldType, Settings indexSettings) { - super(fieldType, false, Defaults.IGNORE_MALFORMED, Defaults.COERCE, null, indexSettings, MultiFields.empty(), null); + super(fieldType, false, null, indexSettings); this.enabledState = enabled; + } @Override @@ -147,6 +139,16 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { super.parse(context); } + @Override + public MappedFieldType defaultFieldType() { + return Defaults.SIZE_FIELD_TYPE; + } + + @Override + public FieldDataType defaultFieldDataType() { + return new FieldDataType("int"); + } + @Override public Mapper parse(ParseContext context) throws IOException { // nothing to do here, we call the parent in postParse @@ -154,14 +156,14 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { } @Override - protected void innerParseCreateField(ParseContext context, List fields) throws IOException { + protected void parseCreateField(ParseContext context, List fields) throws IOException { if (!enabledState.enabled) { return; } if (context.flyweight()) { return; } - fields.add(new CustomIntegerNumericField(this, context.source().length(), fieldType())); + fields.add(new IntegerFieldMapper.CustomIntegerNumericField(context.source().length(), fieldType())); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java index d6c16d291bb..e4149985741 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java @@ -48,8 +48,8 @@ import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import java.io.BufferedInputStream; @@ -66,7 +66,7 @@ import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeSt /** * */ -public class SourceFieldMapper extends AbstractFieldMapper implements RootMapper { +public class SourceFieldMapper extends MetadataFieldMapper { public static final String NAME = "_source"; @@ -92,7 +92,7 @@ public class SourceFieldMapper extends AbstractFieldMapper implements RootMapper } - public static class Builder extends Mapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private boolean enabled = Defaults.ENABLED; @@ -106,7 +106,7 @@ public class SourceFieldMapper extends AbstractFieldMapper implements RootMapper private String[] excludes = null; public Builder() { - super(Defaults.NAME); + super(Defaults.NAME, Defaults.FIELD_TYPE); } public Builder enabled(boolean enabled) { diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java index 751f7586880..238bf6a55f8 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java @@ -31,14 +31,16 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.AlreadyExpiredException; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.analysis.NumericLongAnalyzer; +import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.SourceToParse; +import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.mapper.core.LongFieldMapper; import org.elasticsearch.index.mapper.core.NumberFieldMapper; import org.elasticsearch.search.internal.SearchContext; @@ -52,7 +54,7 @@ import java.util.Map; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeTimeValue; -public class TTLFieldMapper extends LongFieldMapper implements RootMapper { +public class TTLFieldMapper extends MetadataFieldMapper { public static final String NAME = "_ttl"; public static final String CONTENT_TYPE = "_ttl"; @@ -60,7 +62,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { public static class Defaults extends LongFieldMapper.Defaults { public static final String NAME = TTLFieldMapper.CONTENT_TYPE; - public static final MappedFieldType TTL_FIELD_TYPE = new TTLFieldType(); + public static final TTLFieldType TTL_FIELD_TYPE = new TTLFieldType(); static { TTL_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); @@ -77,13 +79,13 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { public static final long DEFAULT = -1; } - public static class Builder extends NumberFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED; private long defaultTTL = Defaults.DEFAULT; public Builder() { - super(Defaults.NAME, Defaults.TTL_FIELD_TYPE, Defaults.PRECISION_STEP_64_BIT); + super(Defaults.NAME, Defaults.TTL_FIELD_TYPE); } public Builder enabled(EnabledAttributeMapper enabled) { @@ -99,17 +101,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { @Override public TTLFieldMapper build(BuilderContext context) { setupFieldType(context); - return new TTLFieldMapper(fieldType, enabledState, defaultTTL, ignoreMalformed(context),coerce(context), fieldDataSettings, context.indexSettings()); - } - - @Override - protected NamedAnalyzer makeNumberAnalyzer(int precisionStep) { - return NumericLongAnalyzer.buildNamedAnalyzer(precisionStep); - } - - @Override - protected int maxPrecisionStep() { - return 64; + return new TTLFieldMapper(fieldType, enabledState, defaultTTL, fieldDataSettings, context.indexSettings()); } } @@ -137,7 +129,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { } } - static final class TTLFieldType extends LongFieldType { + public static final class TTLFieldType extends LongFieldMapper.LongFieldType { public TTLFieldType() { } @@ -147,7 +139,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { } @Override - public LongFieldType clone() { + public TTLFieldType clone() { return new TTLFieldType(this); } @@ -170,13 +162,12 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { private long defaultTTL; public TTLFieldMapper(Settings indexSettings) { - this(Defaults.TTL_FIELD_TYPE.clone(), Defaults.ENABLED_STATE, Defaults.DEFAULT, Defaults.IGNORE_MALFORMED, Defaults.COERCE, null, indexSettings); + this(Defaults.TTL_FIELD_TYPE.clone(), Defaults.ENABLED_STATE, Defaults.DEFAULT, null, indexSettings); } - protected TTLFieldMapper(MappedFieldType fieldType, EnabledAttributeMapper enabled, long defaultTTL, Explicit ignoreMalformed, - Explicit coerce, @Nullable Settings fieldDataSettings, Settings indexSettings) { - super(fieldType, false, ignoreMalformed, coerce, - fieldDataSettings, indexSettings, MultiFields.empty(), null); + protected TTLFieldMapper(MappedFieldType fieldType, EnabledAttributeMapper enabled, long defaultTTL, + @Nullable Settings fieldDataSettings, Settings indexSettings) { + super(fieldType, false, fieldDataSettings, indexSettings); this.enabledState = enabled; this.defaultTTL = defaultTTL; } @@ -203,6 +194,16 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { super.parse(context); } + @Override + public MappedFieldType defaultFieldType() { + return Defaults.TTL_FIELD_TYPE; + } + + @Override + public FieldDataType defaultFieldDataType() { + return new FieldDataType("long"); + } + @Override public Mapper parse(ParseContext context) throws IOException, MapperParsingException { if (context.sourceToParse().ttl() < 0) { // no ttl has been provided externally @@ -210,7 +211,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { if (context.parser().currentToken() == XContentParser.Token.VALUE_STRING) { ttl = TimeValue.parseTimeValue(context.parser().text(), null, "ttl").millis(); } else { - ttl = context.parser().longValue(coerce.value()); + ttl = context.parser().longValue(true); } if (ttl <= 0) { throw new MapperParsingException("TTL value must be > 0. Illegal value provided [" + ttl + "]"); @@ -221,7 +222,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { } @Override - protected void innerParseCreateField(ParseContext context, List fields) throws IOException, AlreadyExpiredException { + protected void parseCreateField(ParseContext context, List fields) throws IOException, AlreadyExpiredException { if (enabledState.enabled && !context.sourceToParse().flyweight()) { long ttl = context.sourceToParse().ttl(); if (ttl <= 0 && defaultTTL > 0) { // no ttl provided so we use the default value @@ -237,7 +238,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { throw new AlreadyExpiredException(context.index(), context.type(), context.id(), timestamp, ttl, now); } // the expiration timestamp (timestamp + ttl) is set as field - fields.add(new CustomLongNumericField(this, expire, fieldType())); + fields.add(new LongFieldMapper.CustomLongNumericField(expire, fieldType())); } } } @@ -261,6 +262,11 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper { return builder; } + @Override + protected String contentType() { + return NAME; + } + @Override public void merge(Mapper mergeWith, MergeResult mergeResult) throws MergeMappingException { TTLFieldMapper ttlMergeWith = (TTLFieldMapper) mergeWith; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java index bb55e06b72f..ebce5a036b5 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java @@ -33,13 +33,15 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.analysis.NumericDateAnalyzer; +import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; +import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.elasticsearch.index.mapper.core.LongFieldMapper; import org.elasticsearch.index.mapper.core.NumberFieldMapper; @@ -53,7 +55,7 @@ import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBo import static org.elasticsearch.index.mapper.core.TypeParsers.parseDateTimeFormatter; import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; -public class TimestampFieldMapper extends DateFieldMapper implements RootMapper { +public class TimestampFieldMapper extends MetadataFieldMapper { public static final String NAME = "_timestamp"; public static final String CONTENT_TYPE = "_timestamp"; @@ -65,7 +67,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper // TODO: this should be removed public static final MappedFieldType PRE_20_FIELD_TYPE; public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern(DEFAULT_DATE_TIME_FORMAT); - public static final DateFieldType FIELD_TYPE = new TimestampFieldType(); + public static final TimestampFieldType FIELD_TYPE = new TimestampFieldType(); static { FIELD_TYPE.setStored(true); @@ -86,7 +88,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper public static final String DEFAULT_TIMESTAMP = "now"; } - public static class Builder extends NumberFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { private EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED; private String path = Defaults.PATH; @@ -95,15 +97,15 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper private Boolean ignoreMissing = null; public Builder(MappedFieldType existing) { - super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing, Defaults.PRECISION_STEP_64_BIT); + super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing); if (existing != null) { // if there is an existing type, always use that store value (only matters for < 2.0) explicitStore = true; } } - DateFieldType fieldType() { - return (DateFieldType)fieldType; + DateFieldMapper.DateFieldType fieldType() { + return (DateFieldMapper.DateFieldType)fieldType; } public Builder enabled(EnabledAttributeMapper enabledState) { @@ -144,18 +146,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper } setupFieldType(context); return new TimestampFieldMapper(fieldType, docValues, enabledState, path, defaultTimestamp, - ignoreMissing, - ignoreMalformed(context), coerce(context), fieldDataSettings, context.indexSettings()); - } - - @Override - protected NamedAnalyzer makeNumberAnalyzer(int precisionStep) { - return NumericDateAnalyzer.buildNamedAnalyzer(fieldType().dateTimeFormatter(), precisionStep); - } - - @Override - protected int maxPrecisionStep() { - return 64; + ignoreMissing, fieldDataSettings, context.indexSettings()); } } @@ -214,7 +205,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper } } - static final class TimestampFieldType extends DateFieldType { + public static final class TimestampFieldType extends DateFieldMapper.DateFieldType { public TimestampFieldType() {} @@ -223,7 +214,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper } @Override - public DateFieldType clone() { + public TimestampFieldType clone() { return new TimestampFieldType(this); } @@ -251,17 +242,14 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper private final Boolean ignoreMissing; public TimestampFieldMapper(Settings indexSettings, MappedFieldType existing) { - this(defaultFieldType(indexSettings, existing).clone(), null, Defaults.ENABLED, Defaults.PATH, Defaults.DEFAULT_TIMESTAMP, - null, Defaults.IGNORE_MALFORMED, Defaults.COERCE, + this(defaultFieldType(indexSettings, existing).clone(), null, Defaults.ENABLED, Defaults.PATH, Defaults.DEFAULT_TIMESTAMP, null, existing == null ? null : (existing.fieldDataType() == null ? null : existing.fieldDataType().getSettings()), indexSettings); } protected TimestampFieldMapper(MappedFieldType fieldType, Boolean docValues, EnabledAttributeMapper enabledState, String path, - String defaultTimestamp, Boolean ignoreMissing, Explicit ignoreMalformed, Explicit coerce, - @Nullable Settings fieldDataSettings, Settings indexSettings) { - super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, - indexSettings, MultiFields.empty(), null); + String defaultTimestamp, Boolean ignoreMissing, @Nullable Settings fieldDataSettings, Settings indexSettings) { + super(fieldType, docValues, fieldDataSettings, indexSettings); this.enabledState = enabledState; this.path = path; this.defaultTimestamp = defaultTimestamp; @@ -269,11 +257,21 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper this.ignoreMissing = ignoreMissing; } + @Override + public TimestampFieldType fieldType() { + return (TimestampFieldType)super.fieldType(); + } + @Override public MappedFieldType defaultFieldType() { return defaultFieldType; } + @Override + public FieldDataType defaultFieldDataType() { + return new FieldDataType("long"); + } + public boolean enabled() { return this.enabledState.enabled; } @@ -306,14 +304,14 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper } @Override - protected void innerParseCreateField(ParseContext context, List fields) throws IOException { + protected void parseCreateField(ParseContext context, List fields) throws IOException { if (enabledState.enabled) { long timestamp = context.sourceToParse().timestamp(); if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) { context.ignoredValue(fieldType().names().indexName(), String.valueOf(timestamp)); } if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - fields.add(new LongFieldMapper.CustomLongNumericField(this, timestamp, fieldType())); + fields.add(new LongFieldMapper.CustomLongNumericField(timestamp, fieldType())); } if (fieldType().hasDocValues()) { fields.add(new NumericDocValuesField(fieldType().names().indexName(), timestamp)); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java index fa84e6f4cbe..ee50ac4a484 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java @@ -41,7 +41,7 @@ import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import org.elasticsearch.index.query.QueryParseContext; @@ -55,7 +55,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class TypeFieldMapper extends AbstractFieldMapper implements RootMapper { +public class TypeFieldMapper extends MetadataFieldMapper { public static final String NAME = "_type"; @@ -78,7 +78,7 @@ public class TypeFieldMapper extends AbstractFieldMapper implements RootMapper { } } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { public Builder(MappedFieldType existing) { super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java index d030fcc7f6c..147e43be3dc 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java @@ -38,7 +38,7 @@ import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext.Document; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; @@ -51,7 +51,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class UidFieldMapper extends AbstractFieldMapper implements RootMapper { +public class UidFieldMapper extends MetadataFieldMapper { public static final String NAME = "_uid"; @@ -79,7 +79,7 @@ public class UidFieldMapper extends AbstractFieldMapper implements RootMapper { } } - public static class Builder extends AbstractFieldMapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { public Builder(MappedFieldType existing) { super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java index 0de26037cd5..31a1b275b6f 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java @@ -32,10 +32,9 @@ import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext.Document; -import org.elasticsearch.index.mapper.RootMapper; -import org.elasticsearch.index.mapper.core.AbstractFieldMapper; import java.io.IOException; import java.util.Iterator; @@ -43,7 +42,7 @@ import java.util.List; import java.util.Map; /** Mapper for the _version field. */ -public class VersionFieldMapper extends AbstractFieldMapper implements RootMapper { +public class VersionFieldMapper extends MetadataFieldMapper { public static final String NAME = "_version"; public static final String CONTENT_TYPE = "_version"; @@ -60,10 +59,10 @@ public class VersionFieldMapper extends AbstractFieldMapper implements RootMappe } } - public static class Builder extends Mapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { public Builder() { - super(Defaults.NAME); + super(Defaults.NAME, Defaults.FIELD_TYPE); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java index 5874fcc180e..23df42dc5c4 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java @@ -288,7 +288,7 @@ public class IpFieldMapper extends NumberFieldMapper { final long value = ipToLong(ipAsString); if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - CustomLongNumericField field = new CustomLongNumericField(this, value, fieldType()); + CustomLongNumericField field = new CustomLongNumericField(value, fieldType()); field.setBoost(fieldType().boost()); fields.add(field); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java index c762e0ab751..b426b162c14 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java @@ -42,7 +42,7 @@ import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperUtils; import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeResult; -import org.elasticsearch.index.mapper.RootMapper; +import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.index.mapper.internal.AllFieldMapper; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import org.elasticsearch.index.settings.IndexSettings; @@ -523,7 +523,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll, Clonea mappersToPut.add(mergeWithMapper); MapperUtils.collect(mergeWithMapper, newObjectMappers, newFieldMappers); } - } else if (mergeIntoMapper instanceof RootMapper == false) { + } else if (mergeIntoMapper instanceof MetadataFieldMapper == false) { // root mappers can only exist here for backcompat, and are merged in Mapping mergeIntoMapper.merge(mergeWithMapper, mergeResult); } @@ -593,7 +593,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll, Clonea int count = 0; for (Mapper mapper : sortedMappers) { - if (!(mapper instanceof RootMapper)) { + if (!(mapper instanceof MetadataFieldMapper)) { if (count++ == 0) { builder.startObject("properties"); } diff --git a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index f153a1839f0..bfd13a27c98 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -1717,7 +1717,7 @@ public class InternalEngineTests extends ElasticsearchTestCase { private Mapping dynamicUpdate() { BuilderContext context = new BuilderContext(Settings.EMPTY, new ContentPath()); final RootObjectMapper root = MapperBuilders.rootObject("some_type").build(context); - return new Mapping(Version.CURRENT, root, new RootMapper[0], new Mapping.SourceTransform[0], ImmutableMap.of()); + return new Mapping(Version.CURRENT, root, new MetadataFieldMapper[0], new Mapping.SourceTransform[0], ImmutableMap.of()); } public void testUpgradeOldIndex() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalRootMapper.java b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalMetadataMapper.java similarity index 57% rename from core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalRootMapper.java rename to core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalMetadataMapper.java index 0395dd22936..64974fd6286 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalRootMapper.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalMetadataMapper.java @@ -19,30 +19,66 @@ package org.elasticsearch.index.mapper.externalvalues; +import org.apache.lucene.document.Field; import org.apache.lucene.document.Field.Store; import org.apache.lucene.document.StringField; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.fielddata.FieldDataType; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.Mapper; +import org.elasticsearch.index.mapper.MapperParsingException; +import org.elasticsearch.index.mapper.MergeMappingException; +import org.elasticsearch.index.mapper.MergeResult; +import org.elasticsearch.index.mapper.MetadataFieldMapper; +import org.elasticsearch.index.mapper.ParseContext; +import org.elasticsearch.index.mapper.core.BooleanFieldMapper; import java.io.IOException; import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.Map; -public class ExternalRootMapper implements RootMapper { +public class ExternalMetadataMapper extends MetadataFieldMapper { static final String CONTENT_TYPE = "_external_root"; static final String FIELD_NAME = "_is_external"; static final String FIELD_VALUE = "true"; + private static MappedFieldType FIELD_TYPE = new BooleanFieldMapper.BooleanFieldType(); + static { + FIELD_TYPE.setNames(new MappedFieldType.Names(FIELD_NAME)); + FIELD_TYPE.freeze(); + } + + protected ExternalMetadataMapper(Settings indexSettings) { + super(FIELD_TYPE, true, null, indexSettings); + } + @Override public String name() { return CONTENT_TYPE; } + @Override + public MappedFieldType defaultFieldType() { + return FIELD_TYPE; + } + + @Override + public FieldDataType defaultFieldDataType() { + return new FieldDataType("string"); + } + + @Override + protected void parseCreateField(ParseContext context, List fields) throws IOException { + // handled in post parse + } + @Override public void merge(Mapper mergeWith, MergeResult mergeResult) throws MergeMappingException { - if (!(mergeWith instanceof ExternalRootMapper)) { + if (!(mergeWith instanceof ExternalMetadataMapper)) { mergeResult.addConflict("Trying to merge " + mergeWith + " with " + this); } } @@ -57,6 +93,11 @@ public class ExternalRootMapper implements RootMapper { return builder.startObject(CONTENT_TYPE).endObject(); } + @Override + protected String contentType() { + return CONTENT_TYPE; + } + @Override public void preParse(ParseContext context) throws IOException { } @@ -66,15 +107,15 @@ public class ExternalRootMapper implements RootMapper { context.doc().add(new StringField(FIELD_NAME, FIELD_VALUE, Store.YES)); } - public static class Builder extends Mapper.Builder { + public static class Builder extends MetadataFieldMapper.Builder { protected Builder() { - super(CONTENT_TYPE); + super(CONTENT_TYPE, FIELD_TYPE); } @Override - public ExternalRootMapper build(BuilderContext context) { - return new ExternalRootMapper(); + public ExternalMetadataMapper build(BuilderContext context) { + return new ExternalMetadataMapper(context.indexSettings()); } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalValuesMapperIntegrationTests.java b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalValuesMapperIntegrationTests.java index 7ee7a099cfc..44bc5541b72 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalValuesMapperIntegrationTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalValuesMapperIntegrationTests.java @@ -30,9 +30,6 @@ import org.junit.Test; import static org.hamcrest.Matchers.equalTo; -/** - */ -@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE) public class ExternalValuesMapperIntegrationTests extends ElasticsearchIntegrationTest { @Override @@ -47,7 +44,7 @@ public class ExternalValuesMapperIntegrationTests extends ElasticsearchIntegrati public void testExternalValues() throws Exception { prepareCreate("test-idx").addMapping("type", XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject(ExternalRootMapper.CONTENT_TYPE) + .startObject(ExternalMetadataMapper.CONTENT_TYPE) .endObject() .startObject("properties") .startObject("field").field("type", RegisterExternalTypes.EXTERNAL).endObject() diff --git a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/RegisterExternalTypes.java b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/RegisterExternalTypes.java index 5cd8110a371..8fb1814a60e 100755 --- a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/RegisterExternalTypes.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/RegisterExternalTypes.java @@ -35,7 +35,7 @@ public class RegisterExternalTypes extends AbstractIndexComponent { public RegisterExternalTypes(Index index, @IndexSettings Settings indexSettings, MapperService mapperService) { super(index, indexSettings); - mapperService.documentMapperParser().putRootTypeParser(ExternalRootMapper.CONTENT_TYPE, new ExternalRootMapper.TypeParser()); + mapperService.documentMapperParser().putRootTypeParser(ExternalMetadataMapper.CONTENT_TYPE, new ExternalMetadataMapper.TypeParser()); mapperService.documentMapperParser().putTypeParser(EXTERNAL, new ExternalMapper.TypeParser(EXTERNAL, "foo")); mapperService.documentMapperParser().putTypeParser(EXTERNAL_BIS, new ExternalMapper.TypeParser(EXTERNAL_BIS, "bar")); mapperService.documentMapperParser().putTypeParser(EXTERNAL_UPPER, new ExternalMapper.TypeParser(EXTERNAL_UPPER, "FOO BAR")); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java index b95fda8d7b4..8851f29fe80 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java @@ -36,14 +36,14 @@ public class SimpleExternalMappingTests extends ElasticsearchSingleNodeTest { @Test public void testExternalValues() throws Exception { MapperService mapperService = createIndex("test").mapperService(); - mapperService.documentMapperParser().putRootTypeParser(ExternalRootMapper.CONTENT_TYPE, - new ExternalRootMapper.TypeParser()); + mapperService.documentMapperParser().putRootTypeParser(ExternalMetadataMapper.CONTENT_TYPE, + new ExternalMetadataMapper.TypeParser()); mapperService.documentMapperParser().putTypeParser(RegisterExternalTypes.EXTERNAL, new ExternalMapper.TypeParser(RegisterExternalTypes.EXTERNAL, "foo")); DocumentMapper documentMapper = mapperService.documentMapperParser().parse( XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject(ExternalRootMapper.CONTENT_TYPE) + .startObject(ExternalMetadataMapper.CONTENT_TYPE) .endObject() .startObject("properties") .startObject("field").field("type", "external").endObject() @@ -68,7 +68,7 @@ public class SimpleExternalMappingTests extends ElasticsearchSingleNodeTest { assertThat(doc.rootDoc().getField("field.field"), notNullValue()); assertThat(doc.rootDoc().getField("field.field").stringValue(), is("foo")); - assertThat(doc.rootDoc().getField(ExternalRootMapper.FIELD_NAME).stringValue(), is(ExternalRootMapper.FIELD_VALUE)); + assertThat(doc.rootDoc().getField(ExternalMetadataMapper.FIELD_NAME).stringValue(), is(ExternalMetadataMapper.FIELD_VALUE)); }