Browse Source

Avoid duplicate types deprecation messages in search-related APIs. (#36802)

Julie Tibshirani 6 years ago
parent
commit
32ef80f3d4

+ 4 - 6
qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java

@@ -25,10 +25,6 @@ import org.elasticsearch.client.Request;
 import org.elasticsearch.client.Response;
 import org.elasticsearch.client.ResponseException;
 import org.elasticsearch.client.RestClient;
-import org.elasticsearch.index.mapper.TypeFieldMapper;
-import org.elasticsearch.rest.action.document.RestGetAction;
-import org.elasticsearch.rest.action.document.RestUpdateAction;
-import org.elasticsearch.rest.action.search.RestExplainAction;
 import org.elasticsearch.cluster.metadata.IndexMetaData;
 import org.elasticsearch.common.Booleans;
 import org.elasticsearch.common.CheckedFunction;
@@ -37,6 +33,9 @@ import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.common.xcontent.json.JsonXContent;
 import org.elasticsearch.common.xcontent.support.XContentMapValues;
+import org.elasticsearch.rest.action.document.RestGetAction;
+import org.elasticsearch.rest.action.document.RestUpdateAction;
+import org.elasticsearch.rest.action.search.RestExplainAction;
 import org.elasticsearch.test.NotEqualMessageBuilder;
 import org.elasticsearch.test.rest.ESRestTestCase;
 import org.elasticsearch.test.rest.yaml.ObjectPath;
@@ -574,8 +573,7 @@ public class FullClusterRestartIT extends AbstractFullClusterRestartTestCase {
 
         Request explainRequest = new Request("GET", "/" + index + "/" + type + "/" + id + "/_explain");
         explainRequest.setJsonEntity("{ \"query\": { \"match_all\" : {} }}");
-        explainRequest.setOptions(
-            expectWarnings(RestExplainAction.TYPES_DEPRECATION_MESSAGE, TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE));
+        explainRequest.setOptions(expectWarnings(RestExplainAction.TYPES_DEPRECATION_MESSAGE));
         String explanation = toStr(client().performRequest(explainRequest));
         assertFalse("Could not find payload boost in explanation\n" + explanation, explanation.contains("payloadBoost"));
 

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

@@ -19,7 +19,6 @@
 
 package org.elasticsearch.index.mapper;
 
-import org.apache.logging.log4j.LogManager;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.SortedSetDocValuesField;
 import org.apache.lucene.index.IndexOptions;
@@ -36,7 +35,6 @@ 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.common.logging.DeprecationLogger;
 import org.elasticsearch.common.lucene.Lucene;
 import org.elasticsearch.common.lucene.search.Queries;
 import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -92,10 +90,6 @@ public class TypeFieldMapper extends MetadataFieldMapper {
 
     public static final class TypeFieldType extends StringFieldType {
 
-        private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(TypeFieldType.class));
-        public static final String TYPES_DEPRECATION_MESSAGE =
-            "[types removal] Referring to types within search queries is deprecated, filter on a field instead.";
-
         TypeFieldType() {
         }
 
@@ -126,7 +120,6 @@ public class TypeFieldMapper extends MetadataFieldMapper {
 
         @Override
         public Query existsQuery(QueryShardContext context) {
-            deprecationLogger.deprecatedAndMaybeLog("exists_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
             return new MatchAllDocsQuery();
         }
 
@@ -137,7 +130,6 @@ public class TypeFieldMapper extends MetadataFieldMapper {
 
         @Override
         public Query termsQuery(List<?> values, QueryShardContext context) {
-            deprecationLogger.deprecatedAndMaybeLog("term_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
             DocumentMapper mapper = context.getMapperService().documentMapper();
             if (mapper == null) {
                 return new MatchNoDocsQuery("No types");
@@ -159,7 +151,6 @@ public class TypeFieldMapper extends MetadataFieldMapper {
 
         @Override
         public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
-            deprecationLogger.deprecatedAndMaybeLog("range_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
             Query result = new MatchAllDocsQuery();
             String type = context.getMapperService().documentMapper().type();
             if (type != null) {

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

@@ -19,6 +19,7 @@
 
 package org.elasticsearch.index.query;
 
+import org.apache.logging.log4j.LogManager;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.search.Query;
@@ -32,6 +33,7 @@ import org.elasticsearch.common.CheckedFunction;
 import org.elasticsearch.common.ParsingException;
 import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
+import org.elasticsearch.common.logging.DeprecationLogger;
 import org.elasticsearch.common.lucene.search.Queries;
 import org.elasticsearch.common.xcontent.NamedXContentRegistry;
 import org.elasticsearch.common.xcontent.XContentParser;
@@ -47,6 +49,7 @@ 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.ScriptService;
@@ -70,6 +73,10 @@ import static java.util.Collections.unmodifiableMap;
  * Context object used to create lucene queries on the shard level.
  */
 public class QueryShardContext extends QueryRewriteContext {
+    private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
+        LogManager.getLogger(QueryShardContext.class));
+    static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using the _type field " +
+        "in queries is deprecated, prefer to filter on a field instead.";
 
     private final ScriptService scriptService;
     private final IndexSettings indexSettings;
@@ -185,6 +192,9 @@ public class QueryShardContext extends QueryRewriteContext {
     }
 
     public MappedFieldType fieldMapper(String name) {
+        if (name.equals(TypeFieldMapper.NAME)) {
+            deprecationLogger.deprecatedAndMaybeLog("query_with_types", TYPES_DEPRECATION_MESSAGE);
+        }
         return failIfFieldMappingNotFound(name, mapperService.fullName(name));
     }
 

+ 5 - 3
server/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java

@@ -32,7 +32,6 @@ import org.elasticsearch.common.logging.DeprecationLogger;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.index.mapper.DocumentMapper;
-import org.elasticsearch.index.mapper.TypeFieldMapper;
 
 import java.io.IOException;
 import java.util.Objects;
@@ -41,7 +40,10 @@ public class TypeQueryBuilder extends AbstractQueryBuilder<TypeQueryBuilder> {
     public static final String NAME = "type";
 
     private static final ParseField VALUE_FIELD = new ParseField("value");
-    private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(TypeQueryBuilder.class));
+    private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
+        LogManager.getLogger(TypeQueryBuilder.class));
+    static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Type queries are deprecated, " +
+        "prefer to filter on a field instead.";
 
     private final String type;
 
@@ -128,7 +130,7 @@ public class TypeQueryBuilder extends AbstractQueryBuilder<TypeQueryBuilder> {
 
     @Override
     protected Query doToQuery(QueryShardContext context) throws IOException {
-        deprecationLogger.deprecatedAndMaybeLog("type_query", TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
+        deprecationLogger.deprecatedAndMaybeLog("type_query", TYPES_DEPRECATION_MESSAGE);
         //LUCENE 4 UPGRADE document mapper should use bytesref as well?
         DocumentMapper documentMapper = context.getMapperService().documentMapper(type);
         if (documentMapper == null) {

+ 0 - 38
server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java

@@ -18,11 +18,6 @@
  */
 package org.elasticsearch.index.mapper;
 
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field.Store;
-import org.apache.lucene.document.StringField;
-import org.apache.lucene.index.DirectoryReader;
-import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.search.MatchNoDocsQuery;
 import org.apache.lucene.search.Query;
@@ -36,8 +31,6 @@ import org.elasticsearch.index.query.QueryShardContext;
 import org.elasticsearch.test.VersionUtils;
 import org.mockito.Mockito;
 
-import java.io.IOException;
-
 public class TypeFieldTypeTests extends FieldTypeTestCase {
     @Override
     protected MappedFieldType createDefaultFieldType() {
@@ -81,36 +74,5 @@ public class TypeFieldTypeTests extends FieldTypeTestCase {
         Mockito.when(mapperService.documentMapper()).thenReturn(mapper);
         query = ft.termQuery("my_type", context);
         assertEquals(new MatchNoDocsQuery(), query);
-        assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
-    }
-
-    public void testExistsQuery() {
-        QueryShardContext context = Mockito.mock(QueryShardContext.class);
-        TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
-        ft.setName(TypeFieldMapper.NAME);
-        ft.existsQuery(context);
-        assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
-    }
-
-    public void testRangeQuery() {
-        QueryShardContext context = Mockito.mock(QueryShardContext.class);
-        MapperService mapperService = Mockito.mock(MapperService.class);
-        DocumentMapper mapper = Mockito.mock(DocumentMapper.class);
-        Mockito.when(context.getMapperService()).thenReturn(mapperService);
-        Mockito.when(mapperService.documentMapper()).thenReturn(mapper);
-        Mockito.when(mapper.type()).thenReturn("my_type");
-
-        TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
-        ft.setName(TypeFieldMapper.NAME);
-        ft.rangeQuery("type1", "type2", true, true, context);
-        assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
-    }
-
-    static DirectoryReader openReaderWithNewType(String type, IndexWriter writer) throws IOException {
-        Document doc = new Document();
-        StringField typeField = new StringField(TypeFieldMapper.NAME, type, Store.NO);
-        doc.add(typeField);
-        writer.addDocument(doc);
-        return DirectoryReader.open(writer);
     }
 }

+ 7 - 0
server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java

@@ -592,4 +592,11 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
         rewritten = query.rewrite(queryShardContext);
         assertThat(rewritten, instanceOf(MatchAllQueryBuilder.class));
     }
+
+    public void testTypeField() throws IOException {
+        RangeQueryBuilder builder = QueryBuilders.rangeQuery("_type")
+            .from("value1");
+        builder.doToQuery(createShardContext());
+        assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
+    }
 }

+ 6 - 0
server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java

@@ -168,4 +168,10 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase<TermQueryBu
         e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
         assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
     }
+
+    public void testTypeField() throws IOException {
+        TermQueryBuilder builder = QueryBuilders.termQuery("_type", "value1");
+        builder.doToQuery(createShardContext());
+        assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
+    }
 }

+ 6 - 0
server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java

@@ -310,5 +310,11 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
         assertEquals(Arrays.asList(5, 42d), TermsQueryBuilder.convert(list));
         assertEquals(Arrays.asList(5, 42d), TermsQueryBuilder.convertBack(TermsQueryBuilder.convert(list)));
     }
+
+    public void testTypeField() throws IOException {
+        TermsQueryBuilder builder = QueryBuilders.termsQuery("_type", "value1", "value2");
+        builder.doToQuery(createShardContext());
+        assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
+    }
 }
 

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

@@ -75,12 +75,12 @@ public class TypeQueryBuilderTests extends AbstractQueryTestCase<TypeQueryBuilde
     @Override
     public void testToQuery() throws IOException {
         super.testToQuery();
-        assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
+        assertWarnings(TypeQueryBuilder.TYPES_DEPRECATION_MESSAGE);
     }
 
     @Override
     public void testMustRewrite() throws IOException {
         super.testMustRewrite();
-        assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
+        assertWarnings(TypeQueryBuilder.TYPES_DEPRECATION_MESSAGE);
     }
 }

+ 6 - 0
server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java

@@ -148,4 +148,10 @@ public class WildcardQueryBuilderTests extends AbstractQueryTestCase<WildcardQue
         query = new WildcardQueryBuilder("_index", "index_" + index + "*").doToQuery(context);
         assertThat(query instanceof MatchNoDocsQuery, equalTo(true));
     }
+
+    public void testTypeField() throws IOException {
+        WildcardQueryBuilder builder = QueryBuilders.wildcardQuery("_type", "doc*");
+        builder.doToQuery(createShardContext());
+        assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
+    }
 }