diff --git a/docs/reference/mapping/fields.asciidoc b/docs/reference/mapping/fields.asciidoc index df9dbb376b5eb..75cb29abf89fd 100644 --- a/docs/reference/mapping/fields.asciidoc +++ b/docs/reference/mapping/fields.asciidoc @@ -1,9 +1,9 @@ [[mapping-fields]] == Metadata fields -Each document has metadata associated with it, such as the `_index`, mapping -<>, and `_id` metadata fields. The behavior of -some of these metadata fields can be customized when a mapping type is created. +Each document has metadata associated with it, such as the `_index` +and `_id` metadata fields. The behavior of some of these metadata +fields can be customized when a mapping is created. [discrete] === Identity metadata fields @@ -13,10 +13,6 @@ some of these metadata fields can be customized when a mapping type is created. The index to which the document belongs. -<>:: - - The document's mapping type. - <>:: The document's ID. @@ -74,5 +70,3 @@ include::fields/routing-field.asciidoc[] include::fields/source-field.asciidoc[] -include::fields/type-field.asciidoc[] - diff --git a/docs/reference/mapping/fields/type-field.asciidoc b/docs/reference/mapping/fields/type-field.asciidoc deleted file mode 100644 index 18c6f4264be3d..0000000000000 --- a/docs/reference/mapping/fields/type-field.asciidoc +++ /dev/null @@ -1,65 +0,0 @@ -[[mapping-type-field]] -=== `_type` field - -deprecated[6.0.0,See <>] - -Each document indexed is associated with a <> and -an <>. The `_type` field is indexed in order to make -searching by type name fast. - -The value of the `_type` field is accessible in queries, aggregations, -scripts, and when sorting: - -[source,console] --------------------------- -# Example documents - -PUT my-index-000001/_doc/1?refresh=true -{ - "text": "Document with type 'doc'" -} --------------------------- -// TESTSETUP - -[source,console] --------------------------- -GET my-index-000001/_search -{ - "query": { - "term": { - "_type": "_doc" <1> - } - }, - "aggs": { - "types": { - "terms": { - "field": "_type", <2> - "size": 10 - } - } - }, - "sort": [ - { - "_type": { <3> - "order": "desc" - } - } - ], - "script_fields": { - "type": { - "script": { - "lang": "painless", - "source": "doc['_type']" <4> - } - } - } -} - --------------------------- -// TEST[warning:[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead.] - -<1> Querying on the `_type` field -<2> Aggregating on the `_type` field -<3> Sorting on the `_type` field -<4> Accessing the `_type` field in scripts - diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml index 4472e0674f265..e16f8d90d21e5 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml @@ -1,6 +1,6 @@ setup: - skip: - features: ["headers"] + features: ["headers", "allowed_warnings"] - do: indices.create: @@ -567,6 +567,8 @@ setup: "Test exists query on _type field": - do: + allowed_warnings: + - "[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead." search: rest_total_hits_as_int: true index: test diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 0d9391fc5d875..a9e17d83734a6 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -144,7 +144,7 @@ public DocumentMapper(MapperService mapperService, Mapping mapping) { } final Collection deleteTombstoneMetadataFields = Arrays.asList(VersionFieldMapper.NAME, IdFieldMapper.NAME, - TypeFieldMapper.NAME, SeqNoFieldMapper.NAME, SeqNoFieldMapper.PRIMARY_TERM_NAME, SeqNoFieldMapper.TOMBSTONE_NAME); + SeqNoFieldMapper.NAME, SeqNoFieldMapper.PRIMARY_TERM_NAME, SeqNoFieldMapper.TOMBSTONE_NAME); this.deleteTombstoneMetadataFieldMappers = Stream.of(mapping.metadataMappers) .filter(field -> deleteTombstoneMetadataFields.contains(field.name())).toArray(MetadataFieldMapper[]::new); final Collection noopTombstoneMetadataFields = Arrays.asList( diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index 7d43216d62728..9fb613c5ba18b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -360,10 +360,6 @@ public static Term extractTerm(Query termQuery) { while (termQuery instanceof BoostQuery) { termQuery = ((BoostQuery) termQuery).getQuery(); } - if (termQuery instanceof TypeFieldMapper.TypesQuery) { - assert ((TypeFieldMapper.TypesQuery) termQuery).getTerms().length == 1; - return new Term(TypeFieldMapper.NAME, ((TypeFieldMapper.TypesQuery) termQuery).getTerms()[0]); - } if (termQuery instanceof TermInSetQuery) { TermInSetQuery tisQuery = (TermInSetQuery) termQuery; PrefixCodedTerms terms = tisQuery.getTermData(); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 750b217059027..2c714e2882803 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -391,6 +391,9 @@ public DocumentMapperForType documentMapperWithAutoCreate() { * Given the full name of a field, returns its {@link MappedFieldType}. */ public MappedFieldType fieldType(String fullName) { + if (fullName.equals(TypeFieldType.NAME)) { + return new TypeFieldType(this.mapper == null ? "_doc" : this.mapper.type()); + } return this.mapper == null ? null : this.mapper.fieldTypes().get(fullName); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java deleted file mode 100644 index 6b8e2fe7607b7..0000000000000 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.mapper; - -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.Term; -import org.apache.lucene.index.TermStates; -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.ConstantScoreQuery; -import org.apache.lucene.search.MatchAllDocsQuery; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.TermInSetQuery; -import org.apache.lucene.search.TermQuery; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.index.fielddata.IndexFieldData; -import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData; -import org.elasticsearch.index.query.QueryShardContext; -import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; -import org.elasticsearch.search.lookup.SearchLookup; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.function.Function; -import java.util.function.Supplier; - -public class TypeFieldMapper extends MetadataFieldMapper { - - public static final String NAME = "_type"; - - public static final String CONTENT_TYPE = "_type"; - - public static final TypeParser PARSER = new FixedTypeParser(c -> new TypeFieldMapper()); - - public static final class TypeFieldType extends ConstantFieldType { - - public static final TypeFieldType INSTANCE = new TypeFieldType(); - - private TypeFieldType() { - super(NAME, Collections.emptyMap()); - } - - @Override - public String typeName() { - return CONTENT_TYPE; - } - - @Override - public Query existsQuery(QueryShardContext context) { - return new MatchAllDocsQuery(); - } - - @Override - public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { - Function typeFunction = mapperService -> mapperService.documentMapper().type(); - return new ConstantIndexFieldData.Builder(typeFunction, name(), CoreValuesSourceType.BYTES); - } - - @Override - protected boolean matches(String pattern, boolean caseInsensitive, QueryShardContext context) { - if (caseInsensitive) { - return pattern.equalsIgnoreCase(MapperService.SINGLE_MAPPING_NAME); - } - return pattern.equals(MapperService.SINGLE_MAPPING_NAME); - } - } - - /** - * Specialization for a disjunction over many _type - */ - public static class TypesQuery extends Query { - // Same threshold as TermInSetQuery - private static final int BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD = 16; - - private final BytesRef[] types; - - public TypesQuery(BytesRef... types) { - if (types == null) { - throw new NullPointerException("types cannot be null."); - } - if (types.length == 0) { - throw new IllegalArgumentException("types must contains at least one value."); - } - this.types = types; - } - - public BytesRef[] getTerms() { - return types; - } - - @Override - public Query rewrite(IndexReader reader) throws IOException { - final int threshold = Math.min(BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD, BooleanQuery.getMaxClauseCount()); - if (types.length <= threshold) { - Set uniqueTypes = new HashSet<>(); - BooleanQuery.Builder bq = new BooleanQuery.Builder(); - int totalDocFreq = 0; - for (BytesRef type : types) { - if (uniqueTypes.add(type)) { - Term term = new Term(CONTENT_TYPE, type); - TermStates context = TermStates.build(reader.getContext(), term, true); - if (context.docFreq() == 0) { - // this _type is not present in the reader - continue; - } - totalDocFreq += context.docFreq(); - // strict equality should be enough ? - if (totalDocFreq >= reader.maxDoc()) { - assert totalDocFreq == reader.maxDoc(); - // Matches all docs since _type is a single value field - // Using a match_all query will help Lucene perform some optimizations - // For instance, match_all queries as filter clauses are automatically removed - return new MatchAllDocsQuery(); - } - bq.add(new TermQuery(term, context), BooleanClause.Occur.SHOULD); - } - } - return new ConstantScoreQuery(bq.build()); - } - return new TermInSetQuery(CONTENT_TYPE, types); - } - - @Override - public boolean equals(Object obj) { - if (sameClassAs(obj) == false) { - return false; - } - TypesQuery that = (TypesQuery) obj; - return Arrays.equals(types, that.types); - } - - @Override - public int hashCode() { - return 31 * classHash() + Arrays.hashCode(types); - } - - @Override - public String toString(String field) { - StringBuilder builder = new StringBuilder(); - for (BytesRef type : types) { - if (builder.length() > 0) { - builder.append(' '); - } - builder.append(new Term(CONTENT_TYPE, type).toString()); - } - return builder.toString(); - } - } - - private TypeFieldMapper() { - super(new TypeFieldType()); - } - - @Override - protected String contentType() { - return CONTENT_TYPE; - } - -} diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java new file mode 100644 index 0000000000000..fc694abeb4661 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java @@ -0,0 +1,81 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.mapper; + +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.Query; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData; +import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; +import org.elasticsearch.search.lookup.SearchLookup; + +import java.util.Collections; +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * Mediates access to the deprecated _type field + */ +public final class TypeFieldType extends ConstantFieldType { + + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(TypeFieldType.class); + public static final String TYPES_V7_DEPRECATION_MESSAGE = "[types removal] Using the _type field " + + "in queries and aggregations is deprecated, prefer to use a field instead."; + + public static final String NAME = "_type"; + + public static final String CONTENT_TYPE = "_type"; + + private final String type; + + TypeFieldType(String type) { + super(NAME, Collections.emptyMap()); + this.type = type; + } + + @Override + public String typeName() { + return CONTENT_TYPE; + } + + @Override + public Query existsQuery(QueryShardContext context) { + deprecationLogger.deprecate("typefieldtype", TYPES_V7_DEPRECATION_MESSAGE); + return new MatchAllDocsQuery(); + } + + @Override + public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { + Function typeFunction = mapperService -> type; + deprecationLogger.deprecate("typefieldtype", TYPES_V7_DEPRECATION_MESSAGE); + return new ConstantIndexFieldData.Builder(typeFunction, name(), CoreValuesSourceType.BYTES); + } + + @Override + protected boolean matches(String pattern, boolean caseInsensitive, QueryShardContext context) { + deprecationLogger.deprecate("typefieldtype", TYPES_V7_DEPRECATION_MESSAGE); + if (caseInsensitive) { + return pattern.equalsIgnoreCase(type); + } + return pattern.equals(type); + } +} diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 6efe13820cae6..dbf64f8710f88 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -33,7 +33,6 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.TriFunction; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -50,7 +49,6 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ObjectMapper; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldMapper; import org.elasticsearch.index.query.support.NestedScope; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.Script; @@ -77,9 +75,6 @@ * Context object used to create lucene queries on the shard level. */ public class QueryShardContext extends QueryRewriteContext { - private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(QueryShardContext.class); - public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using the _type field " + - "in queries and aggregations is deprecated, prefer to use a field instead."; private final ScriptService scriptService; private final IndexSettings indexSettings; @@ -233,9 +228,6 @@ public Set simpleMatchToIndexNames(String pattern) { } public MappedFieldType fieldMapper(String name) { - if (name.equals(TypeFieldMapper.NAME)) { - deprecationLogger.deprecate("query_with_types", TYPES_DEPRECATION_MESSAGE); - } return failIfFieldMappingNotFound(name, mapperService.fieldType(name)); } diff --git a/server/src/main/java/org/elasticsearch/indices/IndicesModule.java b/server/src/main/java/org/elasticsearch/indices/IndicesModule.java index 077f8750652d2..d206535eedd94 100644 --- a/server/src/main/java/org/elasticsearch/indices/IndicesModule.java +++ b/server/src/main/java/org/elasticsearch/indices/IndicesModule.java @@ -50,7 +50,6 @@ import org.elasticsearch.index.mapper.SeqNoFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldMapper; import org.elasticsearch.index.mapper.VersionFieldMapper; import org.elasticsearch.index.seqno.RetentionLeaseBackgroundSyncAction; import org.elasticsearch.index.seqno.RetentionLeaseSyncAction; @@ -150,7 +149,6 @@ private static Map initBuiltInMetadataMa builtInMetadataMappers.put(RoutingFieldMapper.NAME, RoutingFieldMapper.PARSER); builtInMetadataMappers.put(IndexFieldMapper.NAME, IndexFieldMapper.PARSER); builtInMetadataMappers.put(SourceFieldMapper.NAME, SourceFieldMapper.PARSER); - builtInMetadataMappers.put(TypeFieldMapper.NAME, TypeFieldMapper.PARSER); builtInMetadataMappers.put(NestedPathFieldMapper.NAME, NestedPathFieldMapper.PARSER); builtInMetadataMappers.put(VersionFieldMapper.NAME, VersionFieldMapper.PARSER); builtInMetadataMappers.put(SeqNoFieldMapper.NAME, SeqNoFieldMapper.PARSER); diff --git a/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java b/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java index c64d0b9445b69..97aa42b1f87f6 100644 --- a/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java +++ b/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java @@ -24,7 +24,7 @@ import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.TypeFieldMapper; +import org.elasticsearch.index.mapper.TypeFieldType; import java.io.IOException; import java.util.ArrayList; @@ -138,7 +138,7 @@ private FieldLookup loadFieldData(String name) { } if (data.fields() == null) { List values; - if (TypeFieldMapper.NAME.equals(data.fieldType().name())) { + if (TypeFieldType.NAME.equals(data.fieldType().name())) { values = new ArrayList<>(1); final DocumentMapper mapper = mapperService.documentMapper(); if (mapper != null) { diff --git a/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java b/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java index 26d57e7e02688..66f05988a68c9 100644 --- a/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java +++ b/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java @@ -33,7 +33,6 @@ import org.elasticsearch.index.mapper.IndexFieldMapper; import org.elasticsearch.index.mapper.SeqNoFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldMapper; import org.elasticsearch.index.mapper.VersionFieldMapper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.RandomObjects; @@ -250,9 +249,9 @@ public static Tuple,Map> rando Map fields = new HashMap<>(numFields); Map expectedFields = new HashMap<>(numFields); // As we are using this to construct a GetResult object that already contains - // index, type, id, version, seqNo, and source fields, we need to exclude them from random fields + // index, id, version, seqNo, and source fields, we need to exclude them from random fields Predicate excludeMetaFieldFilter = field -> - field.equals(TypeFieldMapper.NAME) || field.equals(IndexFieldMapper.NAME) || + field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME) || field.equals(VersionFieldMapper.NAME) || field.equals(SourceFieldMapper.NAME) || field.equals(SeqNoFieldMapper.NAME) ; while (fields.size() < numFields) { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java index 339f6563cbc30..4c81fe0b1b898 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java @@ -800,7 +800,7 @@ public void testReorderParent() throws IOException { assertThat(doc.docs().size(), equalTo(3)); if (Version.indexCreated(settings).before(Version.V_8_0_0)) { - assertThat(doc.docs().get(0).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePath())); + assertThat(doc.docs().get(0).get(TypeFieldType.NAME), equalTo(nested1Mapper.nestedTypePath())); } else { assertThat(doc.docs().get(0).get(NestedPathFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePath())); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java deleted file mode 100644 index da37ad545674b..0000000000000 --- a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.mapper; - -import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.IndexableField; -import org.apache.lucene.index.SortedSetDocValues; -import org.apache.lucene.store.Directory; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.index.IndexService; -import org.elasticsearch.index.fielddata.IndexFieldDataCache; -import org.elasticsearch.index.fielddata.IndexOrdinalsFieldData; -import org.elasticsearch.index.fielddata.LeafOrdinalsFieldData; -import org.elasticsearch.index.mapper.MapperService.MergeReason; -import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; -import org.elasticsearch.test.InternalSettingsPlugin; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.function.Function; - -public class TypeFieldMapperTests extends ESSingleNodeTestCase { - - @Override - protected Collection> getPlugins() { - return pluginList(InternalSettingsPlugin.class); - } - - public void testDocValuesSingleType() throws Exception { - testDocValues(this::createIndex); - } - - public static void testDocValues(Function createIndex) throws IOException { - MapperService mapperService = createIndex.apply("test").mapperService(); - DocumentMapper mapper = mapperService.merge("type", new CompressedXContent("{\"type\":{}}"), MergeReason.MAPPING_UPDATE); - ParsedDocument document = mapper.parse(new SourceToParse("index", "id", new BytesArray("{}"), XContentType.JSON)); - - Directory dir = newDirectory(); - IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()); - w.addDocument(document.rootDoc()); - DirectoryReader r = DirectoryReader.open(w); - w.close(); - - MappedFieldType ft = mapperService.fieldType(TypeFieldMapper.NAME); - IndexOrdinalsFieldData fd = (IndexOrdinalsFieldData) ft.fielddataBuilder("test", () -> { - throw new UnsupportedOperationException(); - }).build(new IndexFieldDataCache.None(), new NoneCircuitBreakerService(), mapperService); - LeafOrdinalsFieldData afd = fd.load(r.leaves().get(0)); - SortedSetDocValues values = afd.getOrdinalsValues(); - assertTrue(values.advanceExact(0)); - assertEquals(0, values.nextOrd()); - assertEquals(SortedSetDocValues.NO_MORE_ORDS, values.nextOrd()); - assertEquals(new BytesRef("type"), values.lookupOrd(0)); - r.close(); - dir.close(); - } - - public void testDefaults() throws IOException { - Settings indexSettings = Settings.EMPTY; - MapperService mapperService = createIndex("test", indexSettings).mapperService(); - DocumentMapper mapper = mapperService.merge("type", new CompressedXContent("{\"type\":{}}"), MergeReason.MAPPING_UPDATE); - ParsedDocument document = mapper.parse(new SourceToParse("index", "id", new BytesArray("{}"), XContentType.JSON)); - assertEquals(Collections.emptyList(), Arrays.asList(document.rootDoc().getFields(TypeFieldMapper.NAME))); - } -} diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java index bf832f4131210..f0ce7e1f17123 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java @@ -16,34 +16,83 @@ * specific language governing permissions and limitations * under the License. */ + package org.elasticsearch.index.mapper; +import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.index.fielddata.IndexFieldDataCache; +import org.elasticsearch.index.fielddata.IndexOrdinalsFieldData; +import org.elasticsearch.index.fielddata.LeafOrdinalsFieldData; import org.elasticsearch.index.query.QueryShardContext; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.mockito.Mockito; -public class TypeFieldTypeTests extends ESTestCase { +import java.io.IOException; + +import static org.hamcrest.Matchers.equalTo; + +public class TypeFieldTypeTests extends MapperServiceTestCase { + + public void testDocValues() throws Exception { + + MapperService mapperService = createMapperService(XContentFactory.jsonBuilder() + .startObject().startObject("type").endObject().endObject()); + DocumentMapper mapper = mapperService.documentMapper(); + ParsedDocument document = mapper.parse(source(b -> {})); + + withLuceneIndex(mapperService, iw -> iw.addDocument(document.rootDoc()), r -> { + MappedFieldType ft = mapperService.fieldType(TypeFieldType.NAME); + IndexOrdinalsFieldData fd = (IndexOrdinalsFieldData) ft.fielddataBuilder("test", () -> { + throw new UnsupportedOperationException(); + }).build(new IndexFieldDataCache.None(), new NoneCircuitBreakerService(), mapperService); + LeafOrdinalsFieldData afd = fd.load(r.leaves().get(0)); + SortedSetDocValues values = afd.getOrdinalsValues(); + assertTrue(values.advanceExact(0)); + assertEquals(0, values.nextOrd()); + assertEquals(SortedSetDocValues.NO_MORE_ORDS, values.nextOrd()); + assertEquals(new BytesRef("type"), values.lookupOrd(0)); + }); + + assertWarnings("[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead."); + } + + public void testTypeFieldIsNotInDocument() throws IOException { + DocumentMapper mapper = createDocumentMapper(mapping(b -> {})); + ParsedDocument document = mapper.parse(source(b -> {})); + assertThat(document.rootDoc().getFields(TypeFieldType.NAME).length, equalTo(0)); + } public void testTermsQuery() { QueryShardContext context = Mockito.mock(QueryShardContext.class); - TypeFieldMapper.TypeFieldType ft = TypeFieldMapper.TypeFieldType.INSTANCE; + TypeFieldType ft = new TypeFieldType("_doc"); Query query = ft.termQuery("_doc", context); assertEquals(new MatchAllDocsQuery(), query); query = ft.termQueryCaseInsensitive("_dOc", context); assertEquals(new MatchAllDocsQuery(), query); - - + + query = ft.termQuery("other_type", context); assertEquals(new MatchNoDocsQuery(), query); query = ft.termQueryCaseInsensitive("other_Type", context); assertEquals(new MatchNoDocsQuery(), query); - + + assertWarnings("[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead."); + } + + public void testExistsQuery() { + QueryShardContext context = Mockito.mock(QueryShardContext.class); + TypeFieldType ft = new TypeFieldType("_doc"); + Query query = ft.existsQuery(context); + assertEquals(new MatchAllDocsQuery(), query); + assertWarnings("[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead."); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java index b6d4f8f647603..13b37091009f0 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.index.query; import com.fasterxml.jackson.core.io.JsonStringEncoder; - import org.apache.lucene.index.Term; import org.apache.lucene.search.AutomatonQuery; import org.apache.lucene.search.MatchNoDocsQuery; @@ -29,6 +28,7 @@ import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.ParsingException; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.TypeFieldType; import java.io.IOException; @@ -111,7 +111,7 @@ protected void doAssertLuceneQuery(TermQueryBuilder queryBuilder, Query query, Q assertThat(query, instanceOf(MatchNoDocsQuery.class)); } } - + private Query termQuery(MappedFieldType mapper, Object value, boolean caseInsensitive) { if (caseInsensitive) { return mapper.termQueryCaseInsensitive(value, null); @@ -193,7 +193,7 @@ public void testParseAndSerializeBigInteger() throws IOException { public void testTypeField() throws IOException { TermQueryBuilder builder = QueryBuilders.termQuery("_type", "value1"); builder.doToQuery(createShardContext()); - assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE); + assertWarnings(TypeFieldType.TYPES_V7_DEPRECATION_MESSAGE); } public void testRewriteIndexQueryToMatchNone() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java index 4c382b1cfc9d5..01d267538e25b 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java @@ -37,6 +37,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.get.GetResult; +import org.elasticsearch.index.mapper.TypeFieldType; import org.elasticsearch.indices.TermsLookup; import org.elasticsearch.test.AbstractQueryTestCase; import org.hamcrest.CoreMatchers; @@ -320,7 +321,7 @@ public void testConversion() { public void testTypeField() throws IOException { TermsQueryBuilder builder = QueryBuilders.termsQuery("_type", "value1", "value2"); builder.doToQuery(createShardContext()); - assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE); + assertWarnings(TypeFieldType.TYPES_V7_DEPRECATION_MESSAGE); } public void testRewriteIndexQueryToMatchNone() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java index deeced8dd1f48..91c60deb29856 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java @@ -24,6 +24,7 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.WildcardQuery; import org.elasticsearch.common.ParsingException; +import org.elasticsearch.index.mapper.TypeFieldType; import org.elasticsearch.test.AbstractQueryTestCase; import java.io.IOException; @@ -77,7 +78,7 @@ protected void doAssertLuceneQuery(WildcardQueryBuilder queryBuilder, Query quer } else { assertThat(query, instanceOf(WildcardQuery.class)); WildcardQuery wildcardQuery = (WildcardQuery) query; - + assertThat(wildcardQuery.getField(), equalTo(expectedFieldName)); assertThat(wildcardQuery.getTerm().field(), equalTo(expectedFieldName)); // wildcard queries get normalized @@ -109,7 +110,7 @@ public void testEmptyValue() throws IOException { public void testFromJson() throws IOException { String json = "{ \"wildcard\" : { \"user\" : { \"wildcard\" : \"ki*y\"," - + " \"case_insensitive\" : true,\n" + + " \"case_insensitive\" : true,\n" + " \"boost\" : 2.0" + " } }}"; WildcardQueryBuilder parsed = (WildcardQueryBuilder) parseQuery(json); @@ -147,7 +148,7 @@ public void testParseFailsWithMultipleFields() throws IOException { public void testTypeField() throws IOException { WildcardQueryBuilder builder = QueryBuilders.wildcardQuery("_type", "doc*"); builder.doToQuery(createShardContext()); - assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE); + assertWarnings(TypeFieldType.TYPES_V7_DEPRECATION_MESSAGE); } public void testRewriteIndexQueryToMatchNone() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java b/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java index 5753ac7d5cee0..0dba9bb53859e 100644 --- a/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java +++ b/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java @@ -32,7 +32,6 @@ import org.elasticsearch.index.mapper.SeqNoFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldMapper; import org.elasticsearch.index.mapper.VersionFieldMapper; import org.elasticsearch.indices.mapper.MapperRegistry; import org.elasticsearch.plugins.MapperPlugin; @@ -76,7 +75,7 @@ public Map getMetadataMappers() { }); private static final String[] EXPECTED_METADATA_FIELDS = new String[]{ IgnoredFieldMapper.NAME, IdFieldMapper.NAME, - RoutingFieldMapper.NAME, IndexFieldMapper.NAME, SourceFieldMapper.NAME, TypeFieldMapper.NAME, + RoutingFieldMapper.NAME, IndexFieldMapper.NAME, SourceFieldMapper.NAME, NestedPathFieldMapper.NAME, VersionFieldMapper.NAME, SeqNoFieldMapper.NAME, FieldNamesFieldMapper.NAME }; public void testBuiltinMappers() { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java index 699ab62fdecb4..72d134f3d74ce 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java @@ -27,7 +27,7 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.SortedBinaryDocValues; -import org.elasticsearch.index.mapper.TypeFieldMapper; +import org.elasticsearch.index.mapper.TypeFieldType; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.test.ESSingleNodeTestCase; @@ -269,9 +269,9 @@ public void testTypeFieldDeprecation() { try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); - ValuesSourceConfig config = ValuesSourceConfig.resolve( - context, null, TypeFieldMapper.NAME, null, null, null, null, CoreValuesSourceType.BYTES); - assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE); + ValuesSourceConfig.resolve( + context, null, TypeFieldType.NAME, null, null, null, null, CoreValuesSourceType.BYTES); + assertWarnings(TypeFieldType.TYPES_V7_DEPRECATION_MESSAGE); } }