Browse Source

Remove TypeFieldMapper (#62838)

We don't need a special TypeFieldMapper for anything in particular; all access
to the type field can be done via a TypeFieldType that issues appropriate
deprecation warnings.

Relates to #41059
Alan Woodward 5 years ago
parent
commit
981258b02b
20 changed files with 165 additions and 385 deletions
  1. 3 9
      docs/reference/mapping/fields.asciidoc
  2. 0 65
      docs/reference/mapping/fields/type-field.asciidoc
  3. 3 1
      rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml
  4. 1 1
      server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java
  5. 0 4
      server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java
  6. 3 0
      server/src/main/java/org/elasticsearch/index/mapper/MapperService.java
  7. 0 179
      server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java
  8. 81 0
      server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java
  9. 0 8
      server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java
  10. 0 2
      server/src/main/java/org/elasticsearch/indices/IndicesModule.java
  11. 2 2
      server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java
  12. 2 3
      server/src/test/java/org/elasticsearch/index/get/GetResultTests.java
  13. 1 1
      server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java
  14. 0 91
      server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java
  15. 55 6
      server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java
  16. 3 3
      server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java
  17. 2 1
      server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java
  18. 4 3
      server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java
  19. 1 2
      server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java
  20. 4 4
      server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java

+ 3 - 9
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
-<<mapping-type-field,`_type`>>, 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.
 
-<<mapping-type-field,`_type`>>::
-
-    The document's mapping type.
-
 <<mapping-id-field,`_id`>>::
 
     The document's ID.
@@ -74,5 +70,3 @@ include::fields/routing-field.asciidoc[]
 
 include::fields/source-field.asciidoc[]
 
-include::fields/type-field.asciidoc[]
-

+ 0 - 65
docs/reference/mapping/fields/type-field.asciidoc

@@ -1,65 +0,0 @@
-[[mapping-type-field]]
-=== `_type` field
-
-deprecated[6.0.0,See <<removal-of-types>>]
-
-Each document indexed is associated with a <<mapping-type-field,`_type`>> and
-an <<mapping-id-field,`_id`>>.  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
-

+ 3 - 1
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

+ 1 - 1
server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

@@ -144,7 +144,7 @@ public class DocumentMapper implements ToXContentFragment {
         }
 
         final Collection<String> 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<String> noopTombstoneMetadataFields = Arrays.asList(

+ 0 - 4
server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java

@@ -360,10 +360,6 @@ public abstract class MappedFieldType {
         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();

+ 3 - 0
server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

@@ -391,6 +391,9 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
      * 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);
     }
 

+ 0 - 179
server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java

@@ -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> searchLookup) {
-            Function<MapperService, String> 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<BytesRef> 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;
-    }
-
-}

+ 81 - 0
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> searchLookup) {
+        Function<MapperService, String> 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);
+    }
+}

+ 0 - 8
server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

@@ -33,7 +33,6 @@ import org.elasticsearch.common.CheckedFunction;
 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.Mapper;
 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 @@ import java.util.function.Supplier;
  * 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 class QueryShardContext extends QueryRewriteContext {
     }
 
     public MappedFieldType fieldMapper(String name) {
-        if (name.equals(TypeFieldMapper.NAME)) {
-            deprecationLogger.deprecate("query_with_types", TYPES_DEPRECATION_MESSAGE);
-        }
         return failIfFieldMappingNotFound(name, mapperService.fieldType(name));
     }
 

+ 0 - 2
server/src/main/java/org/elasticsearch/indices/IndicesModule.java

@@ -50,7 +50,6 @@ import org.elasticsearch.index.mapper.RoutingFieldMapper;
 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 @@ public class IndicesModule extends AbstractModule {
         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);

+ 2 - 2
server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java

@@ -24,7 +24,7 @@ import org.elasticsearch.index.fieldvisitor.SingleFieldsVisitor;
 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 @@ public class LeafFieldsLookup implements Map<Object, Object> {
         }
         if (data.fields() == null) {
             List<Object> 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) {

+ 2 - 3
server/src/test/java/org/elasticsearch/index/get/GetResultTests.java

@@ -33,7 +33,6 @@ import org.elasticsearch.index.mapper.IdFieldMapper;
 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 class GetResultTests extends ESTestCase {
         Map<String, DocumentField> fields = new HashMap<>(numFields);
         Map<String, DocumentField> 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<String> 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) {

+ 1 - 1
server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java

@@ -800,7 +800,7 @@ public class NestedObjectMapperTests extends ESSingleNodeTestCase {
 
         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()));
         }

+ 0 - 91
server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java

@@ -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<Class<? extends Plugin>> getPlugins() {
-        return pluginList(InternalSettingsPlugin.class);
-    }
-
-    public void testDocValuesSingleType() throws Exception {
-        testDocValues(this::createIndex);
-    }
-
-    public static void testDocValues(Function<String, IndexService> 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.<IndexableField>emptyList(), Arrays.asList(document.rootDoc().getFields(TypeFieldMapper.NAME)));
-    }
-}

+ 55 - 6
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.");
     }
 }

+ 3 - 3
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.Query;
 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 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase<TermQueryBu
             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 class TermQueryBuilderTests extends AbstractTermQueryTestCase<TermQueryBu
     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 {

+ 2 - 1
server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java

@@ -37,6 +37,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
 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 class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
     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 {

+ 4 - 3
server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java

@@ -24,6 +24,7 @@ import org.apache.lucene.search.MatchNoDocsQuery;
 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 @@ public class WildcardQueryBuilderTests extends AbstractQueryTestCase<WildcardQue
             } 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 class WildcardQueryBuilderTests extends AbstractQueryTestCase<WildcardQue
 
     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 class WildcardQueryBuilderTests extends AbstractQueryTestCase<WildcardQue
     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 {

+ 1 - 2
server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java

@@ -32,7 +32,6 @@ import org.elasticsearch.index.mapper.RoutingFieldMapper;
 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 class IndicesModuleTests extends ESTestCase {
     });
 
     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() {

+ 4 - 4
server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java

@@ -27,7 +27,7 @@ import org.elasticsearch.common.settings.Settings;
 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 class ValuesSourceConfigTests extends ESSingleNodeTestCase {
         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);
         }
     }