diff --git a/docs/reference/migration/migrate_2_0.asciidoc b/docs/reference/migration/migrate_2_0.asciidoc index 0279d9d4761..e9c647e406b 100644 --- a/docs/reference/migration/migrate_2_0.asciidoc +++ b/docs/reference/migration/migrate_2_0.asciidoc @@ -279,6 +279,11 @@ to provide special features. They now have limited configuration options. * `_field_names` configuration is limited to disabling the field. * `_size` configuration is limited to enabling the field. +==== Meta fields in documents +Meta fields can no longer be specified within a document. They should be specified +via the API. For example, instead of adding a field `_parent` within a document, +use the `parent` url parameter when indexing that document. + ==== Source field limitations The `_source` field could previously be disabled dynamically. Since this field is a critical piece of many features like the Update API, it is no longer diff --git a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index bf5b4e0b4fd..a1a8180f8dc 100644 --- a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -30,6 +30,7 @@ import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchGenerationException; +import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -194,6 +195,7 @@ public class DocumentMapper implements ToXContent { this.type = rootObjectMapper.name(); this.typeText = new StringAndBytesText(this.type); this.mapping = new Mapping( + Version.indexCreated(indexSettings), rootObjectMapper, rootMappers.values().toArray(new RootMapper[rootMappers.values().size()]), sourceTransforms.toArray(new SourceTransform[sourceTransforms.size()]), @@ -210,7 +212,7 @@ 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.rootMappersNotIncludedInObject) { + for (RootMapper rootMapper : this.mapping.rootMappers) { if (rootMapper instanceof FieldMapper) { newFieldMappers.add((FieldMapper) rootMapper); } diff --git a/src/main/java/org/elasticsearch/index/mapper/InternalMapper.java b/src/main/java/org/elasticsearch/index/mapper/InternalMapper.java deleted file mode 100644 index e9faab72166..00000000000 --- a/src/main/java/org/elasticsearch/index/mapper/InternalMapper.java +++ /dev/null @@ -1,28 +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; - -/** - * A marker interface for internal mappings. - * - * - */ -public interface InternalMapper { -} diff --git a/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 69584d9bfee..809ea291ed7 100755 --- a/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -267,8 +267,8 @@ public class MapperService extends AbstractIndexComponent { List newObjectMappers = new ArrayList<>(); List> newFieldMappers = new ArrayList<>(); for (RootMapper rootMapper : mapper.mapping().rootMappers) { - if (!rootMapper.includeInObject() && rootMapper instanceof FieldMapper) { - newFieldMappers.add((FieldMapper) rootMapper); + if (rootMapper instanceof FieldMapper) { + newFieldMappers.add((FieldMapper)rootMapper); } } MapperUtils.collect(mapper.mapping().root, newObjectMappers, newFieldMappers); diff --git a/src/main/java/org/elasticsearch/index/mapper/Mapping.java b/src/main/java/org/elasticsearch/index/mapper/Mapping.java index b8bbf05616b..e1e0752c077 100644 --- a/src/main/java/org/elasticsearch/index/mapper/Mapping.java +++ b/src/main/java/org/elasticsearch/index/mapper/Mapping.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.mapper; import com.google.common.collect.ImmutableMap; +import org.elasticsearch.Version; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -28,7 +29,8 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.mapper.object.RootObjectMapper; import java.io.IOException; -import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Map; @@ -38,6 +40,8 @@ import java.util.Map; */ public final class Mapping implements ToXContent { + public static final List LEGACY_INCLUDE_IN_OBJECT = Arrays.asList("_all", "_id", "_parent", "_routing", "_timestamp", "_ttl"); + /** * Transformations to be applied to the source before indexing and/or after loading. */ @@ -50,27 +54,31 @@ public final class Mapping implements ToXContent { Map transformSourceAsMap(Map sourceAsMap); } + final Version indexCreated; final RootObjectMapper root; final RootMapper[] rootMappers; - final RootMapper[] rootMappersNotIncludedInObject; final ImmutableMap, RootMapper> rootMappersMap; final SourceTransform[] sourceTransforms; volatile ImmutableMap meta; - public Mapping(RootObjectMapper rootObjectMapper, RootMapper[] rootMappers, SourceTransform[] sourceTransforms, ImmutableMap meta) { + public Mapping(Version indexCreated, RootObjectMapper rootObjectMapper, RootMapper[] rootMappers, SourceTransform[] sourceTransforms, ImmutableMap meta) { + this.indexCreated = indexCreated; this.root = rootObjectMapper; this.rootMappers = rootMappers; - List rootMappersNotIncludedInObject = new ArrayList<>(); ImmutableMap.Builder, RootMapper> builder = ImmutableMap.builder(); for (RootMapper rootMapper : rootMappers) { - if (rootMapper.includeInObject()) { + if (indexCreated.before(Version.V_2_0_0) && LEGACY_INCLUDE_IN_OBJECT.contains(rootMapper.name())) { root.putMapper(rootMapper); - } else { - rootMappersNotIncludedInObject.add(rootMapper); } builder.put(rootMapper.getClass(), rootMapper); } - this.rootMappersNotIncludedInObject = rootMappersNotIncludedInObject.toArray(new RootMapper[rootMappersNotIncludedInObject.size()]); + // keep root mappers sorted for consistent serialization + Arrays.sort(rootMappers, new Comparator() { + @Override + public int compare(Mapper o1, Mapper o2) { + return o1.name().compareTo(o2.name()); + } + }); this.rootMappersMap = builder.build(); this.sourceTransforms = sourceTransforms; this.meta = meta; @@ -85,7 +93,7 @@ public final class Mapping implements ToXContent { * Generate a mapping update for the given root object mapper. */ public Mapping mappingUpdate(Mapper rootObjectMapper) { - return new Mapping((RootObjectMapper) rootObjectMapper, rootMappers, sourceTransforms, meta); + return new Mapping(indexCreated, (RootObjectMapper) rootObjectMapper, rootMappers, sourceTransforms, meta); } /** Get the root mapper with the given class. */ @@ -100,10 +108,6 @@ public final class Mapping implements ToXContent { root.merge(mergeWith.root, mergeResult); for (RootMapper rootMapper : rootMappers) { - // root mappers included in root object will get merge in the rootObjectMapper - if (rootMapper.includeInObject()) { - continue; - } RootMapper mergeWithRootMapper = mergeWith.rootMapper(rootMapper.getClass()); if (mergeWithRootMapper != null) { rootMapper.merge(mergeWithRootMapper, mergeResult); @@ -137,11 +141,12 @@ public final class Mapping implements ToXContent { if (meta != null && !meta.isEmpty()) { builder.field("_meta", meta); } + for (Mapper mapper : rootMappers) { + mapper.toXContent(builder, params); + } return builder; } - // no need to pass here id and boost, since they are added to the root object mapper - // in the constructor - }, rootMappersNotIncludedInObject); + }); return builder; } diff --git a/src/main/java/org/elasticsearch/index/mapper/RootMapper.java b/src/main/java/org/elasticsearch/index/mapper/RootMapper.java index 56172efb84b..0579178efc4 100644 --- a/src/main/java/org/elasticsearch/index/mapper/RootMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/RootMapper.java @@ -39,15 +39,4 @@ public interface RootMapper extends Mapper { */ void postParse(ParseContext context) throws IOException; - /** - * Should the mapper be included in the root - * {@link org.elasticsearch.index.mapper.object.ObjectMapper}. - * - * If this method returns true, then {@link #parse(ParseContext)} will be - * called if the context has a property that matches the name of this - * {@link RootMapper}. Otherwise {@link #parse(ParseContext)} will not - * be called. - */ - boolean includeInObject(); - } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java index b412df7af30..9b8e33204c5 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java @@ -36,11 +36,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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.core.AbstractFieldMapper; @@ -61,7 +60,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class AllFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class AllFieldMapper extends AbstractFieldMapper implements RootMapper { public interface IncludeInAll extends Mapper { @@ -212,11 +211,6 @@ public class AllFieldMapper extends AbstractFieldMapper implements Inter return null; } - @Override - public boolean includeInObject() { - return true; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { if (!enabledState.enabled) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java index 2026579dc2f..c30e820b033 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java @@ -22,10 +22,8 @@ package org.elasticsearch.index.mapper.internal; import com.google.common.collect.UnmodifiableIterator; import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; -import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; @@ -33,11 +31,10 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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.core.AbstractFieldMapper; @@ -58,7 +55,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; * * Added in Elasticsearch 1.3. */ -public class FieldNamesFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class FieldNamesFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_field_names"; @@ -188,11 +185,6 @@ public class FieldNamesFieldMapper extends AbstractFieldMapper implement return null; } - @Override - public boolean includeInObject() { - return false; - } - static Iterable extractFieldNames(final String fullPath) { return new Iterable() { @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java index 870f2af5e96..920d2b1df23 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java @@ -20,7 +20,6 @@ package org.elasticsearch.index.mapper.internal; import com.google.common.collect.Iterables; - import org.apache.lucene.document.BinaryDocValuesField; import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; @@ -44,7 +43,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; @@ -57,7 +55,6 @@ import org.elasticsearch.index.query.QueryParseContext; import java.io.IOException; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -68,7 +65,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class IdFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class IdFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_id"; @@ -264,11 +261,6 @@ public class IdFieldMapper extends AbstractFieldMapper implements Intern // it either get built in the preParse phase, or get parsed... } - @Override - public boolean includeInObject() { - return true; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { XContentParser parser = context.parser(); 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 7f5249958ab..96bea6d4d1a 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java @@ -30,12 +30,11 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperBuilders; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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.core.AbstractFieldMapper; @@ -51,7 +50,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class IndexFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class IndexFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_index"; @@ -170,11 +169,6 @@ public class IndexFieldMapper extends AbstractFieldMapper implements Int return null; } - @Override - public boolean includeInObject() { - return false; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { if (!enabledState.enabled) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java index 0657aaa76bd..4da382027ef 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.mapper.internal; import com.google.common.base.Objects; - import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; @@ -36,7 +35,6 @@ import org.elasticsearch.common.settings.loader.SettingsLoader; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.DocumentMapper; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; @@ -62,7 +60,7 @@ import static org.elasticsearch.index.mapper.MapperBuilders.parent; /** * */ -public class ParentFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class ParentFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_parent"; @@ -181,11 +179,6 @@ public class ParentFieldMapper extends AbstractFieldMapper implements Inter parse(context); } - @Override - public boolean includeInObject() { - return true; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { if (!active()) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java index 3631a3f68f9..7c3f4dadb29 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java @@ -30,11 +30,10 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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.core.AbstractFieldMapper; @@ -51,7 +50,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class RoutingFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class RoutingFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_routing"; public static final String CONTENT_TYPE = "_routing"; @@ -189,11 +188,6 @@ public class RoutingFieldMapper extends AbstractFieldMapper implements I return null; } - @Override - public boolean includeInObject() { - return true; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { if (context.sourceToParse().routing() != null) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java index 7acc52f41cc..1d8ad2a422c 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java @@ -139,11 +139,6 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { return null; } - @Override - public boolean includeInObject() { - return false; - } - @Override protected void innerParseCreateField(ParseContext context, List fields) throws IOException { if (!enabledState.enabled) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java index 4df729c10a5..be962af9234 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java @@ -45,7 +45,12 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.*; +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.core.AbstractFieldMapper; import java.io.IOException; @@ -60,7 +65,7 @@ import static org.elasticsearch.index.mapper.MapperBuilders.source; /** * */ -public class SourceFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class SourceFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_source"; @@ -257,11 +262,6 @@ public class SourceFieldMapper extends AbstractFieldMapper implements In return null; } - @Override - public boolean includeInObject() { - return false; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { if (!enabled) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java index 62c0beff862..8e817ddd903 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java @@ -30,11 +30,10 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.AlreadyExpiredException; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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.SourceToParse; @@ -52,7 +51,7 @@ import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBo import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeTimeValue; import static org.elasticsearch.index.mapper.MapperBuilders.ttl; -public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, RootMapper { +public class TTLFieldMapper extends LongFieldMapper implements RootMapper { public static final String NAME = "_ttl"; public static final String CONTENT_TYPE = "_ttl"; @@ -191,11 +190,6 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R return null; } - @Override - public boolean includeInObject() { - return true; - } - @Override protected void innerParseCreateField(ParseContext context, List fields) throws IOException, AlreadyExpiredException { if (enabledState.enabled && !context.sourceToParse().flyweight()) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java index 160c5c03699..9437ee3d056 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java @@ -32,11 +32,10 @@ import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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.core.DateFieldMapper; @@ -54,7 +53,7 @@ import static org.elasticsearch.index.mapper.MapperBuilders.timestamp; import static org.elasticsearch.index.mapper.core.TypeParsers.parseDateTimeFormatter; import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; -public class TimestampFieldMapper extends DateFieldMapper implements InternalMapper, RootMapper { +public class TimestampFieldMapper extends DateFieldMapper implements RootMapper { public static final String NAME = "_timestamp"; public static final String CONTENT_TYPE = "_timestamp"; @@ -215,7 +214,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), dateTimeFormatter, Defaults.PRECISION_STEP_64_BIT, Defaults.BOOST, fieldType, docValues, Defaults.NULL_VALUE, TimeUnit.MILLISECONDS /*always milliseconds*/, - ignoreMalformed, coerce, null, normsLoading, fieldDataSettings, + ignoreMalformed, coerce, null, normsLoading, fieldDataSettings, indexSettings, MultiFields.empty(), null); this.enabledState = enabledState; this.path = path; @@ -278,11 +277,6 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap return null; } - @Override - public boolean includeInObject() { - return true; - } - @Override protected void innerParseCreateField(ParseContext context, List fields) throws IOException { if (enabledState.enabled) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java index d51d56530a3..117690ed481 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java @@ -36,7 +36,6 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; @@ -57,7 +56,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class TypeFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class TypeFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_type"; @@ -157,11 +156,6 @@ public class TypeFieldMapper extends AbstractFieldMapper implements Inte return null; } - @Override - public boolean includeInObject() { - return false; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { if (fieldType.indexOptions() == IndexOptions.NONE && !fieldType.stored()) { diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java index 01744d37d3b..f71afd60cd7 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java @@ -32,11 +32,10 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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; @@ -53,7 +52,7 @@ import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; /** * */ -public class UidFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class UidFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_uid"; @@ -167,11 +166,6 @@ public class UidFieldMapper extends AbstractFieldMapper implements Internal return null; } - @Override - public boolean includeInObject() { - return false; - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { Field uid = new Field(NAME, Uid.createUid(context.stringBuilder(), context.type(), context.id()), Defaults.FIELD_TYPE); diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java index cb143e877a8..4254d2cd9fc 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java @@ -27,11 +27,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.MergeResult; 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; @@ -45,7 +44,7 @@ import java.util.Map; import static org.elasticsearch.index.mapper.MapperBuilders.version; /** Mapper for the _version field. */ -public class VersionFieldMapper extends AbstractFieldMapper implements InternalMapper, RootMapper { +public class VersionFieldMapper extends AbstractFieldMapper implements RootMapper { public static final String NAME = "_version"; public static final String CONTENT_TYPE = "_version"; @@ -136,11 +135,6 @@ public class VersionFieldMapper extends AbstractFieldMapper implements Int } } - @Override - public boolean includeInObject() { - return false; - } - @Override public FieldType defaultFieldType() { return Defaults.FIELD_TYPE; diff --git a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java index a43410dc46a..d48fe729b25 100644 --- a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java @@ -37,12 +37,12 @@ import org.elasticsearch.index.mapper.ContentPath; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperParser; import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.index.mapper.InternalMapper; import org.elasticsearch.index.mapper.Mapper; 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.internal.AllFieldMapper; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import org.elasticsearch.index.settings.IndexSettings; @@ -57,7 +57,6 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.TreeMap; import static com.google.common.collect.Lists.newArrayList; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; @@ -524,7 +523,8 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll, Clonea mappersToPut.add(mergeWithMapper); MapperUtils.collect(mergeWithMapper, newObjectMappers, newFieldMappers); } - } else { + } else if (mergeIntoMapper instanceof RootMapper == false) { + // root mappers can only exist here for backcompat, and are merged in Mapping mergeIntoMapper.merge(mergeWithMapper, mergeResult); } } @@ -553,11 +553,11 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll, Clonea @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - toXContent(builder, params, null, Mapper.EMPTY_ARRAY); + toXContent(builder, params, null); return builder; } - public void toXContent(XContentBuilder builder, Params params, ToXContent custom, Mapper... additionalMappers) throws IOException { + public void toXContent(XContentBuilder builder, Params params, ToXContent custom) throws IOException { builder.startObject(name); if (nested.isNested()) { builder.field("type", NESTED_CONTENT_TYPE); @@ -567,7 +567,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll, Clonea if (nested.isIncludeInRoot()) { builder.field("include_in_root", true); } - } else if (mappers.isEmpty()) { // only write the object content type if there are no properties, otherwise, it is automatically detected + } else if (mappers.isEmpty() && custom == null) { // only write the object content type if there are no properties, otherwise, it is automatically detected builder.field("type", CONTENT_TYPE); } if (dynamic != null) { @@ -598,26 +598,9 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll, Clonea } }); - // check internal mappers first (this is only relevant for root object) - for (Mapper mapper : sortedMappers) { - if (mapper instanceof InternalMapper) { - mapper.toXContent(builder, params); - } - } - if (additionalMappers != null && additionalMappers.length > 0) { - TreeMap additionalSortedMappers = new TreeMap<>(); - for (Mapper mapper : additionalMappers) { - additionalSortedMappers.put(mapper.name(), mapper); - } - - for (Mapper mapper : additionalSortedMappers.values()) { - mapper.toXContent(builder, params); - } - } - int count = 0; for (Mapper mapper : sortedMappers) { - if (!(mapper instanceof InternalMapper)) { + if (!(mapper instanceof RootMapper)) { if (count++ == 0) { builder.startObject("properties"); } diff --git a/src/test/java/org/elasticsearch/get/GetActionTests.java b/src/test/java/org/elasticsearch/get/GetActionTests.java index 578c043b10b..6262fc6956f 100644 --- a/src/test/java/org/elasticsearch/get/GetActionTests.java +++ b/src/test/java/org/elasticsearch/get/GetActionTests.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.test.ElasticsearchIntegrationTest; @@ -977,11 +978,8 @@ public class GetActionTests extends ElasticsearchIntegrationTest { "}"; assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource)); ensureGreen(); - String doc = "{\n" + - " \"_ttl\": \"1h\"\n" + - "}"; - client().prepareIndex("test", "doc").setId("1").setSource(doc).setParent("1").get(); + client().prepareIndex("test", "doc").setId("1").setSource("{}").setParent("1").setTTL(TimeValue.timeValueHours(1).getMillis()).get(); String[] fieldsList = {"_ttl", "_parent"}; // before refresh - document is only in translog diff --git a/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index 65406c910b4..5835710fea9 100644 --- a/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -1651,7 +1651,7 @@ public class InternalEngineTests extends ElasticsearchTestCase { private Mapping dynamicUpdate() { BuilderContext context = new BuilderContext(ImmutableSettings.EMPTY, new ContentPath()); final RootObjectMapper root = MapperBuilders.rootObject("some_type").build(context); - return new Mapping(root, new RootMapper[0], new Mapping.SourceTransform[0], ImmutableMap.of()); + return new Mapping(Version.CURRENT, root, new RootMapper[0], new Mapping.SourceTransform[0], ImmutableMap.of()); } public void testTranslogReplay() throws IOException { diff --git a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java index 0b9a370b958..89f57c67034 100644 --- a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java @@ -41,12 +41,12 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine.Searcher; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperParser; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParseContext.Document; +import org.elasticsearch.index.mapper.ParsedDocument; +import org.elasticsearch.index.mapper.internal.AllFieldMapper; import org.elasticsearch.index.mapper.internal.IndexFieldMapper; import org.elasticsearch.index.mapper.internal.SizeFieldMapper; -import org.elasticsearch.index.mapper.internal.SourceFieldMapper; import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.hamcrest.Matchers; import org.junit.Test; @@ -61,7 +61,6 @@ import java.util.Map; import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; @@ -70,17 +69,13 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -/** - * - */ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { - @Test public void testSimpleAllMappers() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = docMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = docMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); // One field is boosted so we should see AllTokenStream used: assertThat(field.tokenStream(docMapper.mappers().indexAnalyzer(), null), Matchers.instanceOf(AllTokenStream.class)); @@ -89,53 +84,49 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { assertThat(allEntries.fields().contains("address.last.location"), equalTo(true)); assertThat(allEntries.fields().contains("name.last"), equalTo(true)); assertThat(allEntries.fields().contains("simple1"), equalTo(true)); - FieldMapper mapper = docMapper.mappers().smartNameFieldMapper("_all"); + AllFieldMapper mapper = docMapper.allFieldMapper(); assertThat(field.fieldType().omitNorms(), equalTo(true)); assertThat(mapper.queryStringTermQuery(new Term("_all", "foobar")), Matchers.instanceOf(AllTermQuery.class)); } - @Test public void testAllMappersNoBoost() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json"); IndexService index = createIndex("test"); DocumentMapper docMapper = index.mapperService().documentMapperParser().parse(mapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = docMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = docMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); AllEntries allEntries = field.getAllEntries(); assertThat(allEntries.fields().size(), equalTo(3)); assertThat(allEntries.fields().contains("address.last.location"), equalTo(true)); assertThat(allEntries.fields().contains("name.last"), equalTo(true)); assertThat(allEntries.fields().contains("simple1"), equalTo(true)); - FieldMapper mapper = docMapper.mappers().smartNameFieldMapper("_all"); assertThat(field.fieldType().omitNorms(), equalTo(false)); } - @Test public void testAllMappersTermQuery() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_omit_positions_on_all.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = docMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = docMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); AllEntries allEntries = field.getAllEntries(); assertThat(allEntries.fields().size(), equalTo(3)); assertThat(allEntries.fields().contains("address.last.location"), equalTo(true)); assertThat(allEntries.fields().contains("name.last"), equalTo(true)); assertThat(allEntries.fields().contains("simple1"), equalTo(true)); - FieldMapper mapper = docMapper.mappers().smartNameFieldMapper("_all"); + AllFieldMapper mapper = docMapper.allFieldMapper(); assertThat(field.fieldType().omitNorms(), equalTo(false)); assertThat(mapper.queryStringTermQuery(new Term("_all", "foobar")), Matchers.instanceOf(AllTermQuery.class)); } // #6187: make sure we see AllTermQuery even when offsets are indexed in the _all field: - @Test public void testAllMappersWithOffsetsTermQuery() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_offsets_on_all.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = docMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = docMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); // _all field indexes positions, and mapping has boosts, so we should see AllTokenStream: assertThat(field.tokenStream(docMapper.mappers().indexAnalyzer(), null), Matchers.instanceOf(AllTokenStream.class)); @@ -144,37 +135,33 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { assertThat(allEntries.fields().contains("address.last.location"), equalTo(true)); assertThat(allEntries.fields().contains("name.last"), equalTo(true)); assertThat(allEntries.fields().contains("simple1"), equalTo(true)); - FieldMapper mapper = docMapper.mappers().smartNameFieldMapper("_all"); + AllFieldMapper mapper = docMapper.allFieldMapper(); assertThat(field.fieldType().omitNorms(), equalTo(false)); assertThat(mapper.queryStringTermQuery(new Term("_all", "foobar")), Matchers.instanceOf(AllTermQuery.class)); } // #6187: if _all doesn't index positions then we never use AllTokenStream, even if some fields have boost - @Test public void testBoostWithOmitPositions() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_boost_omit_positions_on_all.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = docMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = docMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); // _all field omits positions, so we should not get AllTokenStream even though fields are boosted assertThat(field.tokenStream(docMapper.mappers().indexAnalyzer(), null), Matchers.not(Matchers.instanceOf(AllTokenStream.class))); } // #6187: if no fields were boosted, we shouldn't use AllTokenStream - @Test public void testNoBoost() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = docMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = docMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); // no fields have boost, so we should not see AllTokenStream: assertThat(field.tokenStream(docMapper.mappers().indexAnalyzer(), null), Matchers.not(Matchers.instanceOf(AllTokenStream.class))); } - - @Test public void testSimpleAllMappersWithReparse() throws Exception { DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json"); @@ -183,23 +170,22 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { // reparse it DocumentMapper builtDocMapper = parser.parse(builtMapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = builtDocMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = builtDocMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); AllEntries allEntries = field.getAllEntries(); - assertThat(allEntries.fields().size(), equalTo(3)); + assertThat(allEntries.fields().toString(), allEntries.fields().size(), equalTo(3)); assertThat(allEntries.fields().contains("address.last.location"), equalTo(true)); assertThat(allEntries.fields().contains("name.last"), equalTo(true)); assertThat(allEntries.fields().contains("simple1"), equalTo(true)); assertThat(field.fieldType().omitNorms(), equalTo(true)); } - @Test public void testSimpleAllMappersWithStore() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = docMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = docMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); AllEntries allEntries = field.getAllEntries(); assertThat(allEntries.fields().size(), equalTo(2)); @@ -211,7 +197,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { assertThat(field.fieldType().omitNorms(), equalTo(false)); } - @Test public void testSimpleAllMappersWithReparseWithStore() throws Exception { DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json"); @@ -220,7 +205,7 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { // reparse it DocumentMapper builtDocMapper = parser.parse(builtMapping); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/all/test1.json"); - Document doc = builtDocMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = builtDocMapper.parse("person", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); AllEntries allEntries = field.getAllEntries(); @@ -233,7 +218,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { assertThat(field.fieldType().omitNorms(), equalTo(false)); } - @Test public void testRandom() throws Exception { boolean omitNorms = false; boolean stored = false; @@ -299,10 +283,9 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { byte[] json = jsonBuilder().startObject() .field("foo", "bar") - .field("_id", 1) .field("foobar", "foobar") .endObject().bytes().toBytes(); - Document doc = builtDocMapper.parse(new BytesArray(json)).rootDoc(); + Document doc = builtDocMapper.parse("test", "1", new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); if (enabled) { assertThat(field.fieldType().omitNorms(), equalTo(omitNorms)); @@ -339,41 +322,37 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { } - @Test public void testMultiField_includeInAllSetToFalse() throws IOException { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_include_in_all_set_to_false.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject() - .field("_id", "1") .field("foo") .startObject() .field("bar", "Elasticsearch rules!") .endObject() .endObject(); - Document doc = docMapper.parse(builder.bytes()).rootDoc(); + Document doc = docMapper.parse("test", "1", builder.bytes()).rootDoc(); AllField field = (AllField) doc.getField("_all"); AllEntries allEntries = field.getAllEntries(); assertThat(allEntries.fields(), empty()); } - @Test public void testMultiField_defaults() throws IOException { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_default.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject() - .field("_id", "1") .field("foo") .startObject() .field("bar", "Elasticsearch rules!") .endObject() .endObject(); - Document doc = docMapper.parse(builder.bytes()).rootDoc(); + Document doc = docMapper.parse("test", "1", builder.bytes()).rootDoc(); AllField field = (AllField) doc.getField("_all"); AllEntries allEntries = field.getAllEntries(); assertThat(allEntries.fields(), hasSize(1)); @@ -402,7 +381,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { // issue https://github.com/elasticsearch/elasticsearch/issues/5864 // test that RootObjectMapping still works - @Test public void testRootObjectMapperPropertiesDoNotCauseException() throws IOException { DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/type_dynamic_template_mapping.json"); @@ -416,7 +394,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { } // issue https://github.com/elasticsearch/elasticsearch/issues/5864 - @Test public void testRootMappersStillWorking() { String mapping = "{"; Map rootTypes = new HashMap<>(); @@ -475,4 +452,17 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { } } } + + public void testIncludeInObjectBackcompat() throws Exception { + String mapping = jsonBuilder().startObject().startObject("type").endObject().endObject().string(); + Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); + ParsedDocument doc = docMapper.parse("type", "1", XContentFactory.jsonBuilder() + .startObject().field("_all", "foo").endObject().bytes()); + + assertNull(doc.rootDoc().get("_all")); + AllField field = (AllField) doc.rootDoc().getField("_all"); + // the backcompat behavior is actually ignoring directly specifying _all + assertFalse(field.getAllEntries().fields().iterator().hasNext()); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/all/test1.json b/src/test/java/org/elasticsearch/index/mapper/all/test1.json index 9e1d8784f8e..4437d3f798d 100644 --- a/src/test/java/org/elasticsearch/index/mapper/all/test1.json +++ b/src/test/java/org/elasticsearch/index/mapper/all/test1.json @@ -1,5 +1,4 @@ { - "_id":"1", "name":{ "first":"shay", "last":"banon" diff --git a/src/test/java/org/elasticsearch/index/mapper/boost/FieldLevelBoostTests.java b/src/test/java/org/elasticsearch/index/mapper/boost/FieldLevelBoostTests.java index 219299fbcf8..8b9e9ab0524 100644 --- a/src/test/java/org/elasticsearch/index/mapper/boost/FieldLevelBoostTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/boost/FieldLevelBoostTests.java @@ -48,7 +48,7 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { .string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); - BytesReference json = XContentFactory.jsonBuilder().startObject().field("_id", "1") + BytesReference json = XContentFactory.jsonBuilder().startObject() .startObject("str_field").field("boost", 2.0).field("value", "some name").endObject() .startObject("int_field").field("boost", 3.0).field("value", 10).endObject() .startObject("byte_field").field("boost", 4.0).field("value", 20).endObject() @@ -58,7 +58,7 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { .startObject("long_field").field("boost", 8.0).field("value", 50).endObject() .startObject("short_field").field("boost", 9.0).field("value", 60).endObject() .bytes(); - Document doc = docMapper.parse(json).rootDoc(); + Document doc = docMapper.parse("person", "1", json).rootDoc(); IndexableField f = doc.getField("str_field"); assertThat((double) f.boost(), closeTo(2.0, 0.001)); @@ -100,8 +100,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("str_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("str_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { @@ -109,8 +109,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { } try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("int_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("int_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { @@ -118,8 +118,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { } try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("byte_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("byte_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { @@ -127,8 +127,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { } try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("date_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("date_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { @@ -136,8 +136,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { } try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("double_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("double_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { @@ -145,8 +145,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { } try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("float_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("float_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { @@ -154,8 +154,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { } try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("long_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("long_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { @@ -163,8 +163,8 @@ public class FieldLevelBoostTests extends ElasticsearchSingleNodeTest { } try { - docMapper.parse(XContentFactory.jsonBuilder().startObject() - .field("_id", "1").startObject("short_field").field("foo", "bar") + docMapper.parse("person", "1", XContentFactory.jsonBuilder().startObject() + .startObject("short_field").field("foo", "bar") .endObject().bytes()).rootDoc(); fail(); } catch (MapperParsingException ex) { diff --git a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/GenericStoreDynamicTemplateTests.java b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/GenericStoreDynamicTemplateTests.java index 478efc1b7d8..d1a4a05a67d 100644 --- a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/GenericStoreDynamicTemplateTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/GenericStoreDynamicTemplateTests.java @@ -46,7 +46,7 @@ public class GenericStoreDynamicTemplateTests extends ElasticsearchSingleNodeTes client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping).get(); DocumentMapper docMapper = index.mapperService().documentMapper("person"); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json"); - ParsedDocument parsedDoc = docMapper.parse(new BytesArray(json)); + ParsedDocument parsedDoc = docMapper.parse("person", "1", new BytesArray(json)); client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString()).get(); Document doc = parsedDoc.rootDoc(); diff --git a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json index 75b8a0ec912..b7439dcb9fe 100644 --- a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json +++ b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json @@ -1,5 +1,4 @@ { - "_id":"1", "name":"some name", "age":1 } \ No newline at end of file diff --git a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTemplateTests.java b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTemplateTests.java index 7108fa36fd5..c8c159b2df9 100644 --- a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTemplateTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTemplateTests.java @@ -47,7 +47,7 @@ public class PathMatchDynamicTemplateTests extends ElasticsearchSingleNodeTest { client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping).get(); DocumentMapper docMapper = index.mapperService().documentMapper("person"); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-data.json"); - ParsedDocument parsedDoc = docMapper.parse(new BytesArray(json)); + ParsedDocument parsedDoc = docMapper.parse("person", "1", new BytesArray(json)); client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString()).get(); Document doc = parsedDoc.rootDoc(); diff --git a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-data.json b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-data.json index 818bedd6c8b..2e6ec997c46 100644 --- a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-data.json +++ b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/pathmatch/test-data.json @@ -1,5 +1,4 @@ { - "_id":"1", "name":"top_level", "obj1":{ "name":"obj1_level", diff --git a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java index 8c53523c22a..a8b25024714 100644 --- a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java @@ -51,8 +51,8 @@ public class SimpleDynamicTemplatesTests extends ElasticsearchSingleNodeTest { client().admin().indices().preparePutMapping("test").setType("person").setSource(builder.string()).get(); DocumentMapper docMapper = index.mapperService().documentMapper("person"); builder = JsonXContent.contentBuilder(); - builder.startObject().field("_id", "1").field("s", "hello").field("l", 1).endObject(); - ParsedDocument parsedDoc = docMapper.parse(builder.bytes()); + builder.startObject().field("s", "hello").field("l", 1).endObject(); + ParsedDocument parsedDoc = docMapper.parse("person", "1", builder.bytes()); client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString()).get(); DocumentFieldMappers mappers = docMapper.mappers(); @@ -74,7 +74,7 @@ public class SimpleDynamicTemplatesTests extends ElasticsearchSingleNodeTest { client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping).get(); DocumentMapper docMapper = index.mapperService().documentMapper("person"); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json"); - ParsedDocument parsedDoc = docMapper.parse(new BytesArray(json)); + ParsedDocument parsedDoc = docMapper.parse("person", "1", new BytesArray(json)); client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString()).get(); Document doc = parsedDoc.rootDoc(); @@ -131,7 +131,7 @@ public class SimpleDynamicTemplatesTests extends ElasticsearchSingleNodeTest { client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping).get(); DocumentMapper docMapper = index.mapperService().documentMapper("person"); byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json"); - ParsedDocument parsedDoc = docMapper.parse(new BytesArray(json)); + ParsedDocument parsedDoc = docMapper.parse("person", "1", new BytesArray(json)); client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString()).get(); Document doc = parsedDoc.rootDoc(); diff --git a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json index 5682d1d1360..1ed3c50b98d 100644 --- a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json +++ b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json @@ -1,5 +1,4 @@ { - "_id":"1", "name":"some name", "age":1, "multi1":"multi 1", diff --git a/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalRootMapper.java b/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalRootMapper.java index 3198c080a8e..9e4d43f5189 100644 --- a/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalRootMapper.java +++ b/src/test/java/org/elasticsearch/index/mapper/externalvalues/ExternalRootMapper.java @@ -70,11 +70,6 @@ public class ExternalRootMapper implements RootMapper { context.doc().add(new StringField(FIELD_NAME, FIELD_VALUE, Store.YES)); } - @Override - public boolean includeInObject() { - return false; - } - public static class Builder extends Mapper.Builder { protected Builder() { diff --git a/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java index d57a46f93b4..6c46ba2bacb 100644 --- a/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java @@ -29,6 +29,8 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParsedDocument; +import org.elasticsearch.index.mapper.SourceToParse; +import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.internal.IdFieldMapper; import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.test.ElasticsearchSingleNodeTest; @@ -39,7 +41,7 @@ import static org.hamcrest.Matchers.nullValue; public class IdMappingTests extends ElasticsearchSingleNodeTest { - public void simpleIdTests() throws Exception { + public void testId() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .endObject().endObject().string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); @@ -57,18 +59,10 @@ public class IdMappingTests extends ElasticsearchSingleNodeTest { .startObject() .endObject() .bytes()); - fail(); + fail("expect missing id"); } catch (MapperParsingException e) { + assertTrue(e.getMessage().contains("No id found")); } - - doc = docMapper.parse("type", null, XContentFactory.jsonBuilder() - .startObject() - .field("_id", 1) - .endObject() - .bytes()); - - assertThat(doc.rootDoc().get(UidFieldMapper.NAME), notNullValue()); - assertThat(doc.rootDoc().get(IdFieldMapper.NAME), nullValue()); } public void testIdIndexedBackcompat() throws Exception { @@ -85,15 +79,6 @@ public class IdMappingTests extends ElasticsearchSingleNodeTest { assertThat(doc.rootDoc().get(UidFieldMapper.NAME), notNullValue()); assertThat(doc.rootDoc().get(IdFieldMapper.NAME), notNullValue()); - - doc = docMapper.parse("type", null, XContentFactory.jsonBuilder() - .startObject() - .field("_id", 1) - .endObject() - .bytes()); - - assertThat(doc.rootDoc().get(UidFieldMapper.NAME), notNullValue()); - assertThat(doc.rootDoc().get(IdFieldMapper.NAME), notNullValue()); } public void testIdPathBackcompat() throws Exception { @@ -115,4 +100,19 @@ public class IdMappingTests extends ElasticsearchSingleNodeTest { assertThat(serialized_id_mapping, equalTo(expected_id_mapping)); } + + public void testIncludeInObjectBackcompat() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string(); + Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); + + ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder() + .startObject() + .field("_id", "1") + .endObject() + .bytes()).type("type")); + + // _id is not indexed so we need to check _uid + assertEquals(Uid.createUid("type", "1"), doc.rootDoc().get(UidFieldMapper.NAME)); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/index/IndexTypeMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/index/IndexTypeMapperTests.java index baa77ed7616..2b2d3c264f9 100644 --- a/src/test/java/org/elasticsearch/index/mapper/index/IndexTypeMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/index/IndexTypeMapperTests.java @@ -40,9 +40,8 @@ public class IndexTypeMapperTests extends ElasticsearchSingleNodeTest { .startObject("_index").field("enabled", true).endObject() .endObject().endObject().string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); - IndexFieldMapper indexMapper = docMapper.rootMapper(IndexFieldMapper.class); + IndexFieldMapper indexMapper = docMapper.indexMapper(); assertThat(indexMapper.enabled(), equalTo(true)); - assertThat(docMapper.mappers().getMapper("_index"), instanceOf(IndexFieldMapper.class)); ParsedDocument doc = docMapper.parse("type", "1", XContentFactory.jsonBuilder() .startObject() diff --git a/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java b/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java index 477f2b60c85..fd528770d15 100644 --- a/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java @@ -69,7 +69,7 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { private void testMultiField(String mapping) throws Exception { DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/test-data.json")); - Document doc = docMapper.parse(json).rootDoc(); + Document doc = docMapper.parse("person", "1", json).rootDoc(); IndexableField f = doc.getField("name"); assertThat(f.name(), equalTo("name")); @@ -157,7 +157,7 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/test-data.json")); - Document doc = docMapper.parse(json).rootDoc(); + Document doc = docMapper.parse("person", "1", json).rootDoc(); IndexableField f = doc.getField("name"); assertThat(f.name(), equalTo("name")); @@ -184,7 +184,7 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/test-multi-field-type-no-default-field.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/test-data.json")); - Document doc = docMapper.parse(json).rootDoc(); + Document doc = docMapper.parse("person", "1", json).rootDoc(); assertNull(doc.getField("name")); IndexableField f = doc.getField("name.indexed"); @@ -267,10 +267,9 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.mappers().getMapper("a.b").fieldType().tokenized(), equalTo(false)); BytesReference json = jsonBuilder().startObject() - .field("_id", "1") .field("a", "-1,-1") .endObject().bytes(); - Document doc = docMapper.parse(json).rootDoc(); + Document doc = docMapper.parse("type", "1", json).rootDoc(); IndexableField f = doc.getField("a"); assertThat(f, notNullValue()); @@ -299,10 +298,9 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.mappers().getMapper("b.a").fieldType().tokenized(), equalTo(false)); json = jsonBuilder().startObject() - .field("_id", "1") .field("b", "-1,-1") .endObject().bytes(); - doc = docMapper.parse(json).rootDoc(); + doc = docMapper.parse("type", "1", json).rootDoc(); f = doc.getField("b"); assertThat(f, notNullValue()); @@ -319,10 +317,9 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions()); json = jsonBuilder().startObject() - .field("_id", "1") .startArray("b").startArray().value(-1).value(-1).endArray().startArray().value(-2).value(-2).endArray().endArray() .endObject().bytes(); - doc = docMapper.parse(json).rootDoc(); + doc = docMapper.parse("type", "1", json).rootDoc(); f = doc.getFields("b")[0]; assertThat(f, notNullValue()); @@ -367,10 +364,9 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.mappers().getMapper("a.b").fieldType().tokenized(), equalTo(true)); BytesReference json = jsonBuilder().startObject() - .field("_id", "1") .field("a", "complete me") .endObject().bytes(); - Document doc = docMapper.parse(json).rootDoc(); + Document doc = docMapper.parse("type", "1", json).rootDoc(); IndexableField f = doc.getField("a"); assertThat(f, notNullValue()); @@ -399,10 +395,9 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.mappers().getMapper("b.a").fieldType().tokenized(), equalTo(false)); json = jsonBuilder().startObject() - .field("_id", "1") .field("b", "complete me") .endObject().bytes(); - doc = docMapper.parse(json).rootDoc(); + doc = docMapper.parse("type", "1", json).rootDoc(); f = doc.getField("b"); assertThat(f, notNullValue()); diff --git a/src/test/java/org/elasticsearch/index/mapper/multifield/merge/JavaMultiFieldMergeTests.java b/src/test/java/org/elasticsearch/index/mapper/multifield/merge/JavaMultiFieldMergeTests.java index b68c49d7956..1235b6f693c 100644 --- a/src/test/java/org/elasticsearch/index/mapper/multifield/merge/JavaMultiFieldMergeTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/multifield/merge/JavaMultiFieldMergeTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperParser; import org.elasticsearch.index.mapper.MergeResult; @@ -51,14 +52,13 @@ public class JavaMultiFieldMergeTests extends ElasticsearchSingleNodeTest { assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions()); assertThat(docMapper.mappers().getMapper("name.indexed"), nullValue()); - BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-data.json")); - Document doc = docMapper.parse(json).rootDoc(); + BytesReference json = XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject().bytes(); + Document doc = docMapper.parse("person", "1", json).rootDoc(); IndexableField f = doc.getField("name"); assertThat(f, notNullValue()); f = doc.getField("name.indexed"); assertThat(f, nullValue()); - mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping2.json"); DocumentMapper docMapper2 = parser.parse(mapping); @@ -75,8 +75,7 @@ public class JavaMultiFieldMergeTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.mappers().getMapper("name.not_indexed2"), nullValue()); assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue()); - json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-data.json")); - doc = docMapper.parse(json).rootDoc(); + doc = docMapper.parse("person", "1", json).rootDoc(); f = doc.getField("name"); assertThat(f, notNullValue()); f = doc.getField("name.indexed"); @@ -98,11 +97,9 @@ public class JavaMultiFieldMergeTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue()); assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue()); - mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping4.json"); DocumentMapper docMapper4 = parser.parse(mapping); - mergeResult = docMapper.merge(docMapper4.mapping(), true); assertThat(Arrays.toString(mergeResult.buildConflicts()), mergeResult.hasConflicts(), equalTo(false)); @@ -127,8 +124,8 @@ public class JavaMultiFieldMergeTests extends ElasticsearchSingleNodeTest { assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions()); assertThat(docMapper.mappers().getMapper("name.indexed"), nullValue()); - BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-data.json")); - Document doc = docMapper.parse(json).rootDoc(); + BytesReference json = XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject().bytes(); + Document doc = docMapper.parse("person", "1", json).rootDoc(); IndexableField f = doc.getField("name"); assertThat(f, notNullValue()); f = doc.getField("name.indexed"); @@ -151,8 +148,7 @@ public class JavaMultiFieldMergeTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.mappers().getMapper("name.not_indexed2"), nullValue()); assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue()); - json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-data.json")); - doc = docMapper.parse(json).rootDoc(); + doc = docMapper.parse("person", "1", json).rootDoc(); f = doc.getField("name"); assertThat(f, notNullValue()); f = doc.getField("name.indexed"); diff --git a/src/test/java/org/elasticsearch/index/mapper/multifield/test-data.json b/src/test/java/org/elasticsearch/index/mapper/multifield/test-data.json index 35f9418e610..2e8ab256df9 100644 --- a/src/test/java/org/elasticsearch/index/mapper/multifield/test-data.json +++ b/src/test/java/org/elasticsearch/index/mapper/multifield/test-data.json @@ -1,5 +1,4 @@ { - "_id":1, "age":28, "name":"some name", "object1":{ diff --git a/src/test/java/org/elasticsearch/index/mapper/parent/ParentMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/parent/ParentMappingTests.java index c64458b77ea..099a12c579e 100644 --- a/src/test/java/org/elasticsearch/index/mapper/parent/ParentMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/parent/ParentMappingTests.java @@ -18,6 +18,10 @@ */ package org.elasticsearch.index.mapper.parent; +import org.elasticsearch.Version; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.ParsedDocument; @@ -29,13 +33,9 @@ import org.junit.Test; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; -/** - * - */ public class ParentMappingTests extends ElasticsearchSingleNodeTest { - @Test - public void parentNotMapped() throws Exception { + public void testParentNotSet() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .endObject().endObject().string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); @@ -47,17 +47,17 @@ public class ParentMappingTests extends ElasticsearchSingleNodeTest { .endObject() .bytes()).type("type").id("1")); - // no _parent mapping, used as a simple field - assertThat(doc.parent(), nullValue()); - assertThat(doc.rootDoc().get("_parent"), nullValue()); + // no _parent mapping, dynamically used as a string field + assertNull(doc.parent()); + assertNotNull(doc.rootDoc().get("_parent")); } - @Test - public void parentSetInDocNotExternally() throws Exception { + public void testParentSetInDocBackcompat() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("_parent").field("type", "p_type").endObject() .endObject().endObject().string(); - DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); + Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder() .startObject() @@ -66,12 +66,11 @@ public class ParentMappingTests extends ElasticsearchSingleNodeTest { .endObject() .bytes()).type("type").id("1")); - assertThat(doc.parent(), equalTo("1122")); - assertThat(doc.rootDoc().get("_parent"), equalTo(Uid.createUid("p_type", "1122"))); + assertEquals("1122", doc.parent()); + assertEquals(Uid.createUid("p_type", "1122"), doc.rootDoc().get("_parent")); } - @Test - public void parentNotSetInDocSetExternally() throws Exception { + public void testParentSet() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("_parent").field("type", "p_type").endObject() .endObject().endObject().string(); @@ -83,23 +82,6 @@ public class ParentMappingTests extends ElasticsearchSingleNodeTest { .endObject() .bytes()).type("type").id("1").parent("1122")); - assertThat(doc.rootDoc().get("_parent"), equalTo(Uid.createUid("p_type", "1122"))); - } - - @Test - public void parentSetInDocSetExternally() throws Exception { - String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("_parent").field("type", "p_type").endObject() - .endObject().endObject().string(); - DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); - - ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder() - .startObject() - .field("_parent", "1122") - .field("x_field", "x_value") - .endObject() - .bytes()).type("type").id("1").parent("1122")); - - assertThat(doc.rootDoc().get("_parent"), equalTo(Uid.createUid("p_type", "1122"))); + assertEquals(Uid.createUid("p_type", "1122"), doc.rootDoc().get("_parent")); } } diff --git a/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java index 302f3b3381b..8fd9a8ed3bd 100644 --- a/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java @@ -21,7 +21,10 @@ package org.elasticsearch.index.mapper.routing; import org.apache.lucene.index.IndexOptions; import org.elasticsearch.Version; +import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MappingMetaData; +import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; @@ -38,28 +41,23 @@ import java.util.Map; import static org.hamcrest.Matchers.*; -/** - * - */ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { - @Test - public void simpleRoutingMapperTests() throws Exception { + public void testRoutingMapper() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .endObject().endObject().string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder() - .startObject() - .field("field", "value") - .endObject() - .bytes()).type("type").id("1").routing("routing_value")); + .startObject() + .field("field", "value") + .endObject() + .bytes()).type("type").id("1").routing("routing_value")); assertThat(doc.rootDoc().get("_routing"), equalTo("routing_value")); assertThat(doc.rootDoc().get("field"), equalTo("value")); } - @Test public void testFieldTypeSettingsBackcompat() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("_routing") @@ -73,7 +71,6 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { assertEquals(IndexOptions.NONE, docMapper.routingFieldMapper().fieldType().indexOptions()); } - @Test public void testFieldTypeSettingsSerializationBackcompat() throws Exception { String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("_routing").field("store", "no").field("index", "no").endObject() @@ -93,4 +90,34 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { assertThat(routingConfiguration, hasKey("index")); assertThat(routingConfiguration.get("index").toString(), is("no")); } + + public void testPathBackcompat() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("_routing").field("path", "custom_routing").endObject() + .endObject().endObject().string(); + Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); + + XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("custom_routing", "routing_value").endObject(); + MappingMetaData mappingMetaData = new MappingMetaData(docMapper); + IndexRequest request = new IndexRequest("test", "type", "1").source(doc); + request.process(MetaData.builder().build(), mappingMetaData, true, "test"); + + assertEquals(request.routing(), "routing_value"); + } + + public void testIncludeInObjectBackcompat() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string(); + Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); + + XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_timestamp", 2000000).endObject(); + MappingMetaData mappingMetaData = new MappingMetaData(docMapper); + IndexRequest request = new IndexRequest("test", "type", "1").source(doc); + request.process(MetaData.builder().build(), mappingMetaData, true, "test"); + + // _routing in a document never worked, so backcompat is ignoring the field + assertNull(request.routing()); + assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_routing")); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/simple/SimpleMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/simple/SimpleMapperTests.java index 02ad4ab113b..b94d189d39a 100644 --- a/src/test/java/org/elasticsearch/index/mapper/simple/SimpleMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/simple/SimpleMapperTests.java @@ -56,7 +56,7 @@ public class SimpleMapperTests extends ElasticsearchSingleNodeTest { assertThat(doc.get(docMapper.mappers().getMapper("name.first").names().indexName()), equalTo("shay")); // System.out.println("Document: " + doc); // System.out.println("Json: " + docMapper.sourceMapper().value(doc)); - doc = docMapper.parse(json).rootDoc(); + doc = docMapper.parse("person", "1", json).rootDoc(); // System.out.println("Document: " + doc); // System.out.println("Json: " + docMapper.sourceMapper().value(doc)); } @@ -71,7 +71,7 @@ public class SimpleMapperTests extends ElasticsearchSingleNodeTest { // reparse it DocumentMapper builtDocMapper = parser.parse(builtMapping); BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1.json")); - Document doc = builtDocMapper.parse(json).rootDoc(); + Document doc = builtDocMapper.parse("person", "1", json).rootDoc(); assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1"))); assertThat(doc.get(docMapper.mappers().getMapper("name.first").names().indexName()), equalTo("shay")); // System.out.println("Document: " + doc); @@ -86,7 +86,7 @@ public class SimpleMapperTests extends ElasticsearchSingleNodeTest { assertThat((String) docMapper.meta().get("param1"), equalTo("value1")); BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1.json")); - Document doc = docMapper.parse(json).rootDoc(); + Document doc = docMapper.parse("person", "1", json).rootDoc(); assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1"))); assertThat(doc.get(docMapper.mappers().getMapper("name.first").names().indexName()), equalTo("shay")); // System.out.println("Document: " + doc); diff --git a/src/test/java/org/elasticsearch/index/mapper/simple/test1.json b/src/test/java/org/elasticsearch/index/mapper/simple/test1.json index 70ba1a5f87d..a4e64e9cc27 100644 --- a/src/test/java/org/elasticsearch/index/mapper/simple/test1.json +++ b/src/test/java/org/elasticsearch/index/mapper/simple/test1.json @@ -1,5 +1,4 @@ { - _id:"1", name:{ first:"shay", last:"banon" diff --git a/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java index c97fa5b789d..876bdf3ffed 100644 --- a/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java @@ -37,7 +37,12 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.mapper.DocumentMapper; +import org.elasticsearch.index.mapper.DocumentMapperParser; +import org.elasticsearch.index.mapper.FieldMapper; +import org.elasticsearch.index.mapper.MergeResult; +import org.elasticsearch.index.mapper.ParsedDocument; +import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.index.mapper.internal.TimestampFieldMapper; import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.junit.Test; @@ -53,7 +58,14 @@ import static org.elasticsearch.Version.V_1_5_0; import static org.elasticsearch.Version.V_2_0_0; import static org.elasticsearch.test.VersionUtils.randomVersion; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.isIn; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.hamcrest.Matchers.notNullValue; /** */ @@ -577,12 +589,17 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { .endObject().endObject().string(); MergeResult mergeResult = docMapper.merge(parser.parse(mapping).mapping(), true); - String[] expectedConflicts = {"mapper [_timestamp] has different index values", "mapper [_timestamp] has different store values", "Cannot update default in _timestamp value. Value is 1970-01-01 now encountering 1970-01-02", "Cannot update path in _timestamp value. Value is foo path in merged mapping is bar", "mapper [_timestamp] has different tokenize values"}; + List expectedConflicts = new ArrayList<>(Arrays.asList( + "mapper [_timestamp] has different index values", + "mapper [_timestamp] has different store values", + "Cannot update default in _timestamp value. Value is 1970-01-01 now encountering 1970-01-02", + "Cannot update path in _timestamp value. Value is foo path in merged mapping is bar", + "mapper [_timestamp] has different tokenize values")); for (String conflict : mergeResult.buildConflicts()) { - assertThat(conflict, isIn(expectedConflicts)); + assertTrue("found unexpected conflict [" + conflict + "]", expectedConflicts.remove(conflict)); } - assertThat(mergeResult.buildConflicts().length, equalTo(expectedConflicts.length)); + assertTrue("missing conflicts: " + Arrays.toString(expectedConflicts.toArray()), expectedConflicts.isEmpty()); assertThat(docMapper.timestampFieldMapper().fieldDataType().getLoading(), equalTo(FieldMapper.Loading.LAZY)); assertTrue(docMapper.timestampFieldMapper().enabled()); assertThat(docMapper.timestampFieldMapper().fieldDataType().getFormat(indexSettings), equalTo("doc_values")); @@ -672,7 +689,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { docMapper.refreshSource(); docMapper = parser.parse(docMapper.mappingSource().string()); MergeResult mergeResult = docMapper.merge(parser.parse(mapping2).mapping(), true); - assertThat(mergeResult.buildConflicts().length, equalTo(conflict == null ? 0:1)); + assertThat(mergeResult.buildConflicts().length, equalTo(conflict == null ? 0 : 1)); if (conflict != null) { assertThat(mergeResult.buildConflicts()[0], containsString(conflict)); } @@ -732,4 +749,35 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.timestampFieldMapper().hasDocValues(), equalTo(docValues)); assertAcked(client().admin().indices().prepareDelete("test_doc_values")); } + + public void testPath() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("_timestamp").field("enabled", true).field("path", "custom_timestamp").endObject() + .endObject().endObject().string(); + DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); + + XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("custom_timestamp", 1).endObject(); + MappingMetaData mappingMetaData = new MappingMetaData(docMapper); + IndexRequest request = new IndexRequest("test", "type", "1").source(doc); + request.process(MetaData.builder().build(), mappingMetaData, true, "test"); + + assertEquals(request.timestamp(), "1"); + } + + public void testIncludeInObjectBackcompat() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("_timestamp").field("enabled", true).field("default", "1970").field("format", "YYYY").endObject() + .endObject().endObject().string(); + Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); + + XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_timestamp", 2000000).endObject(); + MappingMetaData mappingMetaData = new MappingMetaData(docMapper); + IndexRequest request = new IndexRequest("test", "type", "1").source(doc); + request.process(MetaData.builder().build(), mappingMetaData, true, "test"); + + // _timestamp in a document never worked, so backcompat is ignoring the field + assertEquals(MappingMetaData.Timestamp.parseStringTimestamp("1970", Joda.forPattern("YYYY")), request.timestamp()); + assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_timestamp")); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java index 92b9ba83859..cbd2003bbd7 100644 --- a/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java @@ -22,7 +22,10 @@ package org.elasticsearch.index.mapper.ttl; import org.apache.lucene.index.IndexOptions; import org.elasticsearch.Version; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; +import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MappingMetaData; +import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.compress.CompressedString; import org.elasticsearch.common.settings.ImmutableSettings; @@ -291,6 +294,23 @@ public class TTLMappingTests extends ElasticsearchSingleNodeTest { } + public void testIncludeInObjectBackcompat() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("_ttl").field("enabled", true).endObject() + .endObject().endObject().string(); + Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); + + XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_ttl", "2d").endObject(); + MappingMetaData mappingMetaData = new MappingMetaData(docMapper); + IndexRequest request = new IndexRequest("test", "type", "1").source(doc); + request.process(MetaData.builder().build(), mappingMetaData, true, "test"); + + // _ttl in a document never worked, so backcompat is ignoring the field + assertEquals(-1, request.ttl()); + assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_ttl")); + } + private org.elasticsearch.common.xcontent.XContentBuilder getMappingWithTtlEnabled() throws IOException { return getMappingWithTtlEnabled(null); } diff --git a/src/test/java/org/elasticsearch/index/mapper/update/default_mapping_with_disabled_root_types.json b/src/test/java/org/elasticsearch/index/mapper/update/default_mapping_with_disabled_root_types.json index 1f98a3f98c8..139f7bc3440 100644 --- a/src/test/java/org/elasticsearch/index/mapper/update/default_mapping_with_disabled_root_types.json +++ b/src/test/java/org/elasticsearch/index/mapper/update/default_mapping_with_disabled_root_types.json @@ -1 +1 @@ -{"type":{"_timestamp":{"enabled":false},"_index":{"enabled":false},"_size":{"enabled":false}}} \ No newline at end of file +{"type":{"_index":{"enabled":false},"_size":{"enabled":false},"_timestamp":{"enabled":false}}} \ No newline at end of file diff --git a/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeFormatTests.java b/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeFormatTests.java index e4c30e6928b..9431f8a8cf6 100644 --- a/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeFormatTests.java +++ b/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeFormatTests.java @@ -58,7 +58,7 @@ public class IndexQueryParserFilterDateRangeFormatTests extends ElasticsearchSin MapperService mapperService = indexService.mapperService(); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json"); mapperService.merge("person", new CompressedString(mapping), true); - ParsedDocument doc = mapperService.documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); + ParsedDocument doc = mapperService.documentMapper("person").parse("person", "1", new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); assertNotNull(doc.dynamicMappingsUpdate()); client().admin().indices().preparePutMapping("test").setType("person").setSource(doc.dynamicMappingsUpdate().toString()).get(); queryParser = injector.getInstance(IndexQueryParserService.class); diff --git a/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeTimezoneTests.java b/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeTimezoneTests.java index 9a670995865..6d1a3dbb344 100644 --- a/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeTimezoneTests.java +++ b/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeTimezoneTests.java @@ -59,7 +59,7 @@ public class IndexQueryParserFilterDateRangeTimezoneTests extends ElasticsearchS MapperService mapperService = indexService.mapperService(); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json"); mapperService.merge("person", new CompressedString(mapping), true); - ParsedDocument doc = mapperService.documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); + ParsedDocument doc = mapperService.documentMapper("person").parse("person", "1", new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); assertNotNull(doc.dynamicMappingsUpdate()); client().admin().indices().preparePutMapping("test").setType("person").setSource(doc.dynamicMappingsUpdate().toString()).get(); queryParser = injector.getInstance(IndexQueryParserService.class); diff --git a/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java b/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java index c1095e38554..e9a3d876d31 100644 --- a/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java +++ b/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java @@ -166,7 +166,7 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json"); mapperService.merge("person", new CompressedString(mapping), true); - ParsedDocument doc = mapperService.documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); + ParsedDocument doc = mapperService.documentMapper("person").parse("person", "1", new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); assertNotNull(doc.dynamicMappingsUpdate()); client().admin().indices().preparePutMapping("test").setType("person").setSource(doc.dynamicMappingsUpdate().toString()).get(); diff --git a/src/test/java/org/elasticsearch/index/query/data.json b/src/test/java/org/elasticsearch/index/query/data.json index b3c6db83a07..79f139f84ba 100644 --- a/src/test/java/org/elasticsearch/index/query/data.json +++ b/src/test/java/org/elasticsearch/index/query/data.json @@ -1,6 +1,4 @@ { - _boost:3.7, - _id:"1", name:{ first:"shay", last:"banon"