Skip to content

Commit c2310b6

Browse files
committed
Add tests for types deprecation in the _search endpoint.
1 parent 8b42390 commit c2310b6

File tree

6 files changed

+103
-12
lines changed

6 files changed

+103
-12
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ public void testSearch() throws Exception {
174174
sourceBuilder.fetchSource(false);
175175
// end::search-source-filtering-off
176176
// tag::search-source-filtering-includes
177-
String[] includeFields = new String[] {"title", "user", "innerObject.*"};
178-
String[] excludeFields = new String[] {"_type"};
177+
String[] includeFields = new String[] {"title", "innerObject.*"};
178+
String[] excludeFields = new String[] {"user"};
179179
sourceBuilder.fetchSource(includeFields, excludeFields);
180180
// end::search-source-filtering-includes
181181
sourceBuilder.fetchSource(true);
@@ -247,7 +247,6 @@ public void onFailure(Exception e) {
247247
for (SearchHit hit : searchHits) {
248248
// tag::search-hits-singleHit-properties
249249
String index = hit.getIndex();
250-
String type = hit.getType();
251250
String id = hit.getId();
252251
float score = hit.getScore();
253252
// end::search-hits-singleHit-properties
@@ -263,8 +262,8 @@ public void onFailure(Exception e) {
263262
assertEquals(3, totalHits);
264263
assertNotNull(hits.getHits()[0].getSourceAsString());
265264
assertNotNull(hits.getHits()[0].getSourceAsMap().get("title"));
266-
assertNotNull(hits.getHits()[0].getSourceAsMap().get("user"));
267265
assertNotNull(hits.getHits()[0].getSourceAsMap().get("innerObject"));
266+
assertNull(hits.getHits()[0].getSourceAsMap().get("user"));
268267
}
269268
}
270269

docs/java-rest/high-level/search/search.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ be iterated over:
289289
include-tagged::{doc-tests-file}[{api}-hits-singleHit]
290290
--------------------------------------------------
291291

292-
The `SearchHit` provides access to basic information like index, type, docId and
293-
score of each search hit:
292+
The `SearchHit` provides access to basic information like index, document ID
293+
and score of each search hit:
294294

295295
["source","java",subs="attributes,callouts,macros"]
296296
--------------------------------------------------

server/src/main/java/org/elasticsearch/action/search/SearchRequest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,21 @@ public SearchRequest indicesOptions(IndicesOptions indicesOptions) {
217217
/**
218218
* The document types to execute the search against. Defaults to be executed against
219219
* all types.
220+
*
221+
* @deprecated Types are in the process of being removed. Instead of using a type, prefer to
222+
* filter on a field on the document.
220223
*/
224+
@Deprecated
221225
public String[] types() {
222226
return types;
223227
}
224228

225229
/**
226230
* The document types to execute the search against. Defaults to be executed against
227231
* all types.
228-
* @deprecated Types are going away, prefer filtering on a type.
232+
*
233+
* @deprecated Types are in the process of being removed. Instead of using a type, prefer to
234+
* filter on a field on the document.
229235
*/
230236
@Deprecated
231237
public SearchRequest types(String... types) {

server/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@
5454
import static org.elasticsearch.search.suggest.SuggestBuilders.termSuggestion;
5555

5656
public class RestSearchAction extends BaseRestHandler {
57-
5857
public static final String TYPED_KEYS_PARAM = "typed_keys";
5958
private static final Set<String> RESPONSE_PARAMS = Collections.singleton(TYPED_KEYS_PARAM);
59+
6060
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestSearchAction.class));
61+
static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
62+
" Specifying types in search requests is deprecated.";
6163

6264
public RestSearchAction(Settings settings, RestController controller) {
6365
super(settings);
@@ -150,11 +152,10 @@ public static void parseSearchRequest(SearchRequest searchRequest, RestRequest r
150152
searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
151153
}
152154

153-
String types = request.param("type");
154-
if (types != null) {
155-
deprecationLogger.deprecated("The {index}/{type}/_search endpoint is deprecated, use {index}/_search instead");
155+
if (request.hasParam("type")) {
156+
deprecationLogger.deprecated(TYPES_DEPRECATION_MESSAGE);
157+
searchRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
156158
}
157-
searchRequest.types(Strings.splitStringByCommaToArray(types));
158159
searchRequest.routing(request.param("routing"));
159160
searchRequest.preference(request.param("preference"));
160161
searchRequest.indicesOptions(IndicesOptions.fromRequest(request, searchRequest.indicesOptions()));

server/src/main/java/org/elasticsearch/search/SearchHit.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ public String getId() {
183183

184184
/**
185185
* The type of the document.
186+
*
187+
* @deprecated Types are in the process of being removed. Instead of using a type, prefer to
188+
* filter on a field on the document.
186189
*/
190+
@Deprecated
187191
public String getType() {
188192
return type != null ? type.string() : null;
189193
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.rest.action.search;
21+
22+
import org.elasticsearch.client.node.NodeClient;
23+
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.common.util.concurrent.ThreadContext;
25+
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
26+
import org.elasticsearch.rest.RestChannel;
27+
import org.elasticsearch.rest.RestController;
28+
import org.elasticsearch.rest.RestRequest;
29+
import org.elasticsearch.test.ESTestCase;
30+
import org.elasticsearch.test.rest.FakeRestChannel;
31+
import org.elasticsearch.test.rest.FakeRestRequest;
32+
import org.elasticsearch.usage.UsageService;
33+
34+
import java.util.Collections;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
38+
import static org.mockito.Mockito.mock;
39+
40+
public class RestSearchActionTests extends ESTestCase {
41+
private RestController controller;
42+
43+
public void setUp() throws Exception {
44+
super.setUp();
45+
controller = new RestController(Collections.emptySet(), null,
46+
mock(NodeClient.class),
47+
new NoneCircuitBreakerService(),
48+
new UsageService());
49+
new RestSearchAction(Settings.EMPTY, controller);
50+
}
51+
52+
public void testTypeInPath() {
53+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
54+
.withMethod(RestRequest.Method.GET)
55+
.withPath("/some_index/some_type/_search")
56+
.build();
57+
58+
performRequest(request);
59+
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
60+
}
61+
62+
public void testTypeParameter() {
63+
Map<String, String> params = new HashMap<>();
64+
params.put("type", "some_type");
65+
66+
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
67+
.withMethod(RestRequest.Method.GET)
68+
.withPath("/some_index/_search")
69+
.withParams(params)
70+
.build();
71+
72+
performRequest(request);
73+
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
74+
}
75+
76+
private void performRequest(RestRequest request) {
77+
RestChannel channel = new FakeRestChannel(request, false, 1);
78+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
79+
controller.dispatchRequest(request, channel, threadContext);
80+
}
81+
}

0 commit comments

Comments
 (0)