Skip to content

Commit d40037c

Browse files
Deprecate uses of _type as a field name in queries (#36503)
1 parent b5d532f commit d40037c

File tree

7 files changed

+42
-16
lines changed

7 files changed

+42
-16
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,9 @@ public void testReindex() throws Exception {
823823
// tag::reindex-request-conflicts
824824
request.setConflicts("proceed"); // <1>
825825
// end::reindex-request-conflicts
826-
// tag::reindex-request-typeOrQuery
827-
request.setSourceDocTypes("_doc"); // <1>
828-
request.setSourceQuery(new TermQueryBuilder("user", "kimchy")); // <2>
829-
// end::reindex-request-typeOrQuery
826+
// tag::reindex-request-query
827+
request.setSourceQuery(new TermQueryBuilder("user", "kimchy")); // <1>
828+
// end::reindex-request-query
830829
// tag::reindex-request-size
831830
request.setSize(10); // <1>
832831
// end::reindex-request-size

docs/java-rest/high-level/document/reindex.asciidoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,13 @@ include-tagged::{doc-tests-file}[{api}-request-conflicts]
5757
--------------------------------------------------
5858
<1> Set `proceed` on version conflict
5959

60-
You can limit the documents by adding a type to the source or by adding a query.
60+
You can limit the documents by adding a query.
6161

6262
["source","java",subs="attributes,callouts,macros"]
6363
--------------------------------------------------
64-
include-tagged::{doc-tests-file}[{api}-request-typeOrQuery]
64+
include-tagged::{doc-tests-file}[{api}-request-query]
6565
--------------------------------------------------
66-
<1> Only copy `doc` type
67-
<2> Only copy documents which have field `user` set to `kimchy`
66+
<1> Only copy documents which have field `user` set to `kimchy`
6867

6968
It’s also possible to limit the number of processed documents by setting size.
7069

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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;
2829
import org.elasticsearch.rest.action.document.RestGetAction;
2930
import org.elasticsearch.rest.action.search.RestExplainAction;
3031
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -572,7 +573,8 @@ void assertAllSearchWorks(int count) throws IOException {
572573

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

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ public MetadataFieldMapper getDefault(MappedFieldType fieldType, ParserContext c
9090
}
9191
}
9292

93-
static final class TypeFieldType extends StringFieldType {
93+
public static final class TypeFieldType extends StringFieldType {
9494

9595
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.";
9698

9799
TypeFieldType() {
98100
}
@@ -124,6 +126,7 @@ public boolean isSearchable() {
124126

125127
@Override
126128
public Query existsQuery(QueryShardContext context) {
129+
deprecationLogger.deprecatedAndMaybeLog("exists_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
127130
return new MatchAllDocsQuery();
128131
}
129132

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

135138
@Override
136139
public Query termsQuery(List<?> values, QueryShardContext context) {
140+
deprecationLogger.deprecatedAndMaybeLog("term_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
137141
DocumentMapper mapper = context.getMapperService().documentMapper();
138142
if (mapper == null) {
139143
return new MatchNoDocsQuery("No types");
@@ -155,9 +159,7 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
155159

156160
@Override
157161
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
158-
deprecationLogger.deprecatedAndMaybeLog("range_single_type",
159-
"Running [range] query on [_type] field for an index with a single type."
160-
+ " As types are deprecated, this functionality will be removed in future releases.");
162+
deprecationLogger.deprecatedAndMaybeLog("range_query_with_type_field", TYPES_DEPRECATION_MESSAGE);
161163
Query result = new MatchAllDocsQuery();
162164
String type = context.getMapperService().documentMapper().type();
163165
if (type != null) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
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;
3536

3637
import java.io.IOException;
3738
import java.util.Objects;
@@ -127,7 +128,7 @@ public String getWriteableName() {
127128

128129
@Override
129130
protected Query doToQuery(QueryShardContext context) throws IOException {
130-
deprecationLogger.deprecated("The [type] query is deprecated, filter on a field instead.");
131+
deprecationLogger.deprecatedAndMaybeLog("type_query", TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
131132
//LUCENE 4 UPGRADE document mapper should use bytesref as well?
132133
DocumentMapper documentMapper = context.getMapperService().documentMapper(type);
133134
if (documentMapper == null) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ public void testTermsQuery() throws Exception {
8181
Mockito.when(mapperService.documentMapper()).thenReturn(mapper);
8282
query = ft.termQuery("my_type", context);
8383
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);
84107
}
85108

86109
static DirectoryReader openReaderWithNewType(String type, IndexWriter writer) throws IOException {

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("The [type] query is deprecated, filter on a field instead.");
78+
assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
7979
}
8080

8181
@Override
8282
public void testMustRewrite() throws IOException {
8383
super.testMustRewrite();
84-
assertWarnings("The [type] query is deprecated, filter on a field instead.");
84+
assertWarnings(TypeFieldMapper.TypeFieldType.TYPES_DEPRECATION_MESSAGE);
8585
}
8686
}

0 commit comments

Comments
 (0)