From 6471766173a0fbf30ae4c304badd83d4d9bdcf77 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Wed, 7 Oct 2020 10:19:58 +0100 Subject: [PATCH] Don't emit separate warnings for type filters --- .../index/mapper/TypeFieldMapper.java | 17 ++++ .../index/mapper/TypeFieldType.java | 84 ------------------- .../search/DefaultSearchContext.java | 5 +- 3 files changed, 19 insertions(+), 87 deletions(-) delete mode 100644 server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java index 5764de78e28cb..73a7979239e9c 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java @@ -39,6 +39,7 @@ import java.time.ZoneId; import java.util.Collections; +import java.util.Objects; import java.util.function.Supplier; public class TypeFieldMapper extends MetadataFieldMapper { @@ -137,6 +138,22 @@ public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower } return new MatchAllDocsQuery(); } + + /** + * Build a type filter + * + * This does not emit a deprecation warning, as it is only called when a type + * has been specified in a REST request and warnings will have already been + * emitted at the REST layer. + */ + public Query typeFilter(String[] types) { + for (String t : types) { + if (Objects.equals(this.type, t)) { + return new MatchAllDocsQuery(); + } + } + return new MatchNoDocsQuery(); + } } private TypeFieldMapper() { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java deleted file mode 100644 index 2126b388e27d2..0000000000000 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java +++ /dev/null @@ -1,84 +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.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.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) { - deprecationLogger.deprecate("typefieldtype", TYPES_V7_DEPRECATION_MESSAGE); - return new ConstantIndexFieldData.Builder(type, name(), CoreValuesSourceType.BYTES); - } - - @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); - } - - @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/search/DefaultSearchContext.java b/server/src/main/java/org/elasticsearch/search/DefaultSearchContext.java index 6bd96439c8c45..5abe48dd28d21 100644 --- a/server/src/main/java/org/elasticsearch/search/DefaultSearchContext.java +++ b/server/src/main/java/org/elasticsearch/search/DefaultSearchContext.java @@ -78,7 +78,6 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -315,8 +314,8 @@ private Query createTypeFilter(String[] types) { if (mapperService().documentMapper() == null) { return null; } - MappedFieldType ft = new TypeFieldMapper.TypeFieldType(mapperService().documentMapper().type()); - return ft.termsQuery(Arrays.asList(types), queryShardContext); + TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType(mapperService().documentMapper().type()); + return ft.typeFilter(types); } return null; }