diff --git a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java index 3ce23c8e6a204..7519cade0f29b 100644 --- a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java +++ b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java @@ -25,10 +25,6 @@ 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.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 @@ void assertAllSearchWorks(int count) throws IOException { 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")); 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 442a816be5c6b..c4d9ef966ca3d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java +++ b/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.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 MetadataFieldMapper getDefault(MappedFieldType fieldType, ParserContext c 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 boolean isSearchable() { @Override public Query existsQuery(QueryShardContext context) { - deprecationLogger.deprecatedAndMaybeLog("exists_query_with_type_field", TYPES_DEPRECATION_MESSAGE); return new MatchAllDocsQuery(); } @@ -137,7 +130,6 @@ public Query termQuery(Object value, QueryShardContext context) { @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 Query termsQuery(List values, QueryShardContext context) { @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) { 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 82bae93e84d49..f7f1d29f53098 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/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.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.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 @@ * 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 Collection simpleMatchToIndexNames(String pattern) { } public MappedFieldType fieldMapper(String name) { + if (name.equals(TypeFieldMapper.NAME)) { + deprecationLogger.deprecatedAndMaybeLog("query_with_types", TYPES_DEPRECATION_MESSAGE); + } return failIfFieldMappingNotFound(name, mapperService.fullName(name)); } diff --git a/server/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java index 260c47a334b30..ceb473bce40d0 100644 --- a/server/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java @@ -32,7 +32,6 @@ 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 { 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 String getWriteableName() { @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) { 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 d8840b275419b..dc6f14cb0148a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java +++ b/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.test.VersionUtils; import org.mockito.Mockito; -import java.io.IOException; - public class TypeFieldTypeTests extends FieldTypeTestCase { @Override protected MappedFieldType createDefaultFieldType() { @@ -81,36 +74,5 @@ public void testTermsQuery() throws Exception { 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); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index 28349994c63e3..df312ba84c309 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -592,4 +592,11 @@ public boolean convertNowRangeToMatchAll() { 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); + } } 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 6876d021a0ae9..a6e9590f54010 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java @@ -168,4 +168,10 @@ public void testParseFailsWithMultipleFields() throws IOException { 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); + } } 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 c954be5403f49..02df22fd97efb 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java @@ -310,5 +310,11 @@ public void testConversion() { 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); + } } diff --git a/server/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java index 17c49df366261..67916e52789c5 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java @@ -75,12 +75,12 @@ public void testFromJson() throws IOException { @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); } } 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 f5adb70c9fecc..caab692b9cf6b 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java @@ -148,4 +148,10 @@ public void testIndexWildcard() throws IOException { 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); + } }