Skip to content

Commit 32ef80f

Browse files
authored
Avoid duplicate types deprecation messages in search-related APIs. (#36802)
1 parent 3412627 commit 32ef80f

File tree

10 files changed

+46
-58
lines changed

10 files changed

+46
-58
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
import org.elasticsearch.client.Response;
2626
import org.elasticsearch.client.ResponseException;
2727
import org.elasticsearch.client.RestClient;
28-
import org.elasticsearch.index.mapper.TypeFieldMapper;
29-
import org.elasticsearch.rest.action.document.RestGetAction;
30-
import org.elasticsearch.rest.action.document.RestUpdateAction;
31-
import org.elasticsearch.rest.action.search.RestExplainAction;
3228
import org.elasticsearch.cluster.metadata.IndexMetaData;
3329
import org.elasticsearch.common.Booleans;
3430
import org.elasticsearch.common.CheckedFunction;
@@ -37,6 +33,9 @@
3733
import org.elasticsearch.common.xcontent.XContentBuilder;
3834
import org.elasticsearch.common.xcontent.json.JsonXContent;
3935
import org.elasticsearch.common.xcontent.support.XContentMapValues;
36+
import org.elasticsearch.rest.action.document.RestGetAction;
37+
import org.elasticsearch.rest.action.document.RestUpdateAction;
38+
import org.elasticsearch.rest.action.search.RestExplainAction;
4039
import org.elasticsearch.test.NotEqualMessageBuilder;
4140
import org.elasticsearch.test.rest.ESRestTestCase;
4241
import org.elasticsearch.test.rest.yaml.ObjectPath;
@@ -574,8 +573,7 @@ void assertAllSearchWorks(int count) throws IOException {
574573

575574
Request explainRequest = new Request("GET", "/" + index + "/" + type + "/" + id + "/_explain");
576575
explainRequest.setJsonEntity("{ \"query\": { \"match_all\" : {} }}");
577-
explainRequest.setOptions(
578-
expectWarnings(RestExplainAction.TYPES_DEPRECATION_MESSAGE, TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE));
576+
explainRequest.setOptions(expectWarnings(RestExplainAction.TYPES_DEPRECATION_MESSAGE));
579577
String explanation = toStr(client().performRequest(explainRequest));
580578
assertFalse("Could not find payload boost in explanation\n" + explanation, explanation.contains("payloadBoost"));
581579

server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.index.mapper;
2121

22-
import org.apache.logging.log4j.LogManager;
2322
import org.apache.lucene.document.Field;
2423
import org.apache.lucene.document.SortedSetDocValuesField;
2524
import org.apache.lucene.index.IndexOptions;
@@ -36,7 +35,6 @@
3635
import org.apache.lucene.search.TermInSetQuery;
3736
import org.apache.lucene.search.TermQuery;
3837
import org.apache.lucene.util.BytesRef;
39-
import org.elasticsearch.common.logging.DeprecationLogger;
4038
import org.elasticsearch.common.lucene.Lucene;
4139
import org.elasticsearch.common.lucene.search.Queries;
4240
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -92,10 +90,6 @@ public MetadataFieldMapper getDefault(MappedFieldType fieldType, ParserContext c
9290

9391
public static final class TypeFieldType extends StringFieldType {
9492

95-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(TypeFieldType.class));
96-
public static final String TYPES_DEPRECATION_MESSAGE =
97-
"[types removal] Referring to types within search queries is deprecated, filter on a field instead.";
98-
9993
TypeFieldType() {
10094
}
10195

@@ -126,7 +120,6 @@ public boolean isSearchable() {
126120

127121
@Override
128122
public Query existsQuery(QueryShardContext context) {
129-
deprecationLogger.deprecatedAndMaybeLog("exists_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
130123
return new MatchAllDocsQuery();
131124
}
132125

@@ -137,7 +130,6 @@ public Query termQuery(Object value, QueryShardContext context) {
137130

138131
@Override
139132
public Query termsQuery(List<?> values, QueryShardContext context) {
140-
deprecationLogger.deprecatedAndMaybeLog("term_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
141133
DocumentMapper mapper = context.getMapperService().documentMapper();
142134
if (mapper == null) {
143135
return new MatchNoDocsQuery("No types");
@@ -159,7 +151,6 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
159151

160152
@Override
161153
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
162-
deprecationLogger.deprecatedAndMaybeLog("range_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
163154
Query result = new MatchAllDocsQuery();
164155
String type = context.getMapperService().documentMapper().type();
165156
if (type != null) {

server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.logging.log4j.LogManager;
2223
import org.apache.lucene.analysis.Analyzer;
2324
import org.apache.lucene.index.IndexReader;
2425
import org.apache.lucene.search.Query;
@@ -32,6 +33,7 @@
3233
import org.elasticsearch.common.ParsingException;
3334
import org.elasticsearch.common.Strings;
3435
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
36+
import org.elasticsearch.common.logging.DeprecationLogger;
3537
import org.elasticsearch.common.lucene.search.Queries;
3638
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3739
import org.elasticsearch.common.xcontent.XContentParser;
@@ -47,6 +49,7 @@
4749
import org.elasticsearch.index.mapper.MapperService;
4850
import org.elasticsearch.index.mapper.ObjectMapper;
4951
import org.elasticsearch.index.mapper.TextFieldMapper;
52+
import org.elasticsearch.index.mapper.TypeFieldMapper;
5053
import org.elasticsearch.index.query.support.NestedScope;
5154
import org.elasticsearch.index.similarity.SimilarityService;
5255
import org.elasticsearch.script.ScriptService;
@@ -70,6 +73,10 @@
7073
* Context object used to create lucene queries on the shard level.
7174
*/
7275
public class QueryShardContext extends QueryRewriteContext {
76+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
77+
LogManager.getLogger(QueryShardContext.class));
78+
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using the _type field " +
79+
"in queries is deprecated, prefer to filter on a field instead.";
7380

7481
private final ScriptService scriptService;
7582
private final IndexSettings indexSettings;
@@ -185,6 +192,9 @@ public Collection<String> simpleMatchToIndexNames(String pattern) {
185192
}
186193

187194
public MappedFieldType fieldMapper(String name) {
195+
if (name.equals(TypeFieldMapper.NAME)) {
196+
deprecationLogger.deprecatedAndMaybeLog("query_with_types", TYPES_DEPRECATION_MESSAGE);
197+
}
188198
return failIfFieldMappingNotFound(name, mapperService.fullName(name));
189199
}
190200

server/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.elasticsearch.common.xcontent.XContentBuilder;
3333
import org.elasticsearch.common.xcontent.XContentParser;
3434
import org.elasticsearch.index.mapper.DocumentMapper;
35-
import org.elasticsearch.index.mapper.TypeFieldMapper;
3635

3736
import java.io.IOException;
3837
import java.util.Objects;
@@ -41,7 +40,10 @@ public class TypeQueryBuilder extends AbstractQueryBuilder<TypeQueryBuilder> {
4140
public static final String NAME = "type";
4241

4342
private static final ParseField VALUE_FIELD = new ParseField("value");
44-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(TypeQueryBuilder.class));
43+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
44+
LogManager.getLogger(TypeQueryBuilder.class));
45+
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Type queries are deprecated, " +
46+
"prefer to filter on a field instead.";
4547

4648
private final String type;
4749

@@ -128,7 +130,7 @@ public String getWriteableName() {
128130

129131
@Override
130132
protected Query doToQuery(QueryShardContext context) throws IOException {
131-
deprecationLogger.deprecatedAndMaybeLog("type_query", TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
133+
deprecationLogger.deprecatedAndMaybeLog("type_query", TYPES_DEPRECATION_MESSAGE);
132134
//LUCENE 4 UPGRADE document mapper should use bytesref as well?
133135
DocumentMapper documentMapper = context.getMapperService().documentMapper(type);
134136
if (documentMapper == null) {

server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
*/
1919
package org.elasticsearch.index.mapper;
2020

21-
import org.apache.lucene.document.Document;
22-
import org.apache.lucene.document.Field.Store;
23-
import org.apache.lucene.document.StringField;
24-
import org.apache.lucene.index.DirectoryReader;
25-
import org.apache.lucene.index.IndexWriter;
2621
import org.apache.lucene.search.MatchAllDocsQuery;
2722
import org.apache.lucene.search.MatchNoDocsQuery;
2823
import org.apache.lucene.search.Query;
@@ -36,8 +31,6 @@
3631
import org.elasticsearch.test.VersionUtils;
3732
import org.mockito.Mockito;
3833

39-
import java.io.IOException;
40-
4134
public class TypeFieldTypeTests extends FieldTypeTestCase {
4235
@Override
4336
protected MappedFieldType createDefaultFieldType() {
@@ -81,36 +74,5 @@ public void testTermsQuery() throws Exception {
8174
Mockito.when(mapperService.documentMapper()).thenReturn(mapper);
8275
query = ft.termQuery("my_type", context);
8376
assertEquals(new MatchNoDocsQuery(), query);
84-
assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
85-
}
86-
87-
public void testExistsQuery() {
88-
QueryShardContext context = Mockito.mock(QueryShardContext.class);
89-
TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
90-
ft.setName(TypeFieldMapper.NAME);
91-
ft.existsQuery(context);
92-
assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
93-
}
94-
95-
public void testRangeQuery() {
96-
QueryShardContext context = Mockito.mock(QueryShardContext.class);
97-
MapperService mapperService = Mockito.mock(MapperService.class);
98-
DocumentMapper mapper = Mockito.mock(DocumentMapper.class);
99-
Mockito.when(context.getMapperService()).thenReturn(mapperService);
100-
Mockito.when(mapperService.documentMapper()).thenReturn(mapper);
101-
Mockito.when(mapper.type()).thenReturn("my_type");
102-
103-
TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
104-
ft.setName(TypeFieldMapper.NAME);
105-
ft.rangeQuery("type1", "type2", true, true, context);
106-
assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
107-
}
108-
109-
static DirectoryReader openReaderWithNewType(String type, IndexWriter writer) throws IOException {
110-
Document doc = new Document();
111-
StringField typeField = new StringField(TypeFieldMapper.NAME, type, Store.NO);
112-
doc.add(typeField);
113-
writer.addDocument(doc);
114-
return DirectoryReader.open(writer);
11577
}
11678
}

server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,11 @@ public boolean convertNowRangeToMatchAll() {
592592
rewritten = query.rewrite(queryShardContext);
593593
assertThat(rewritten, instanceOf(MatchAllQueryBuilder.class));
594594
}
595+
596+
public void testTypeField() throws IOException {
597+
RangeQueryBuilder builder = QueryBuilders.rangeQuery("_type")
598+
.from("value1");
599+
builder.doToQuery(createShardContext());
600+
assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
601+
}
595602
}

server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,10 @@ public void testParseFailsWithMultipleFields() throws IOException {
168168
e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
169169
assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
170170
}
171+
172+
public void testTypeField() throws IOException {
173+
TermQueryBuilder builder = QueryBuilders.termQuery("_type", "value1");
174+
builder.doToQuery(createShardContext());
175+
assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
176+
}
171177
}

server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,5 +310,11 @@ public void testConversion() {
310310
assertEquals(Arrays.asList(5, 42d), TermsQueryBuilder.convert(list));
311311
assertEquals(Arrays.asList(5, 42d), TermsQueryBuilder.convertBack(TermsQueryBuilder.convert(list)));
312312
}
313+
314+
public void testTypeField() throws IOException {
315+
TermsQueryBuilder builder = QueryBuilders.termsQuery("_type", "value1", "value2");
316+
builder.doToQuery(createShardContext());
317+
assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
318+
}
313319
}
314320

server/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public void testFromJson() throws IOException {
7575
@Override
7676
public void testToQuery() throws IOException {
7777
super.testToQuery();
78-
assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
78+
assertWarnings(TypeQueryBuilder.TYPES_DEPRECATION_MESSAGE);
7979
}
8080

8181
@Override
8282
public void testMustRewrite() throws IOException {
8383
super.testMustRewrite();
84-
assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
84+
assertWarnings(TypeQueryBuilder.TYPES_DEPRECATION_MESSAGE);
8585
}
8686
}

server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,10 @@ public void testIndexWildcard() throws IOException {
148148
query = new WildcardQueryBuilder("_index", "index_" + index + "*").doToQuery(context);
149149
assertThat(query instanceof MatchNoDocsQuery, equalTo(true));
150150
}
151+
152+
public void testTypeField() throws IOException {
153+
WildcardQueryBuilder builder = QueryBuilders.wildcardQuery("_type", "doc*");
154+
builder.doToQuery(createShardContext());
155+
assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
156+
}
151157
}

0 commit comments

Comments
 (0)