Skip to content

Commit ff83531

Browse files
authored
[Rest Api Compatibility] Typed endpoints for explain api (elastic#73901)
retrofits the typed endpoints for rest explain api removed in elastic#46926 relates elastic#51816
1 parent 93b3b6c commit ff83531

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

rest-api-spec/build.gradle

+3-9
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,14 @@ tasks.named("yamlRestCompatTest").configure {
125125
// This mean test cases where there is assertion on not finging by type won't work
126126
'mget/11_default_index_type/Default index/type',
127127
'mget/16_basic_with_types/Basic multi-get',
128-
// 88 - 14 = 74 tests won't be fixed
128+
// asserting about type not found won't work as we ignore the type information
129+
'explain/40_mix_typeless_typeful/Explain with typeless API on an index that has types',
130+
// 89 - 15 = 74 tests won't be fixed
129131
'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion and specifying both node_ids and node_names',
130132
'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion without specifying nodes',
131133
'count/11_basic_with_types/count body without query element',
132134
'count/11_basic_with_types/count with body',
133135
'count/11_basic_with_types/count with empty body',
134-
'explain/10_basic/Basic explain',
135-
'explain/10_basic/Basic explain with alias',
136-
'explain/11_basic_with_types/Basic explain',
137-
'explain/11_basic_with_types/Basic explain with alias',
138-
'explain/20_source_filtering/Source filtering',
139-
'explain/21_source_filtering_with_types/Source filtering',
140-
'explain/31_query_string_with_types/explain with query_string parameters',
141-
'explain/40_mix_typeless_typeful/Explain with typeless API on an index that has types',
142136
'field_caps/30_filter/Field caps with index filter',
143137
'get_source/11_basic_with_types/Basic with types',
144138
'get_source/16_default_values_with_types/Default values',

server/src/main/java/org/elasticsearch/action/explain/ExplainResponse.java

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.Version;
1313
import org.elasticsearch.action.ActionResponse;
1414
import org.elasticsearch.common.xcontent.ParseField;
15+
import org.elasticsearch.core.RestApiVersion;
1516
import org.elasticsearch.common.io.stream.StreamInput;
1617
import org.elasticsearch.common.io.stream.StreamOutput;
1718
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
@@ -172,6 +173,10 @@ public static ExplainResponse fromXContent(XContentParser parser, boolean exists
172173
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
173174
builder.startObject();
174175
builder.field(_INDEX.getPreferredName(), index);
176+
if (builder.getRestApiVersion() == RestApiVersion.V_7) {
177+
builder.field(MapperService.TYPE_FIELD_NAME, MapperService.SINGLE_MAPPING_NAME);
178+
}
179+
175180
builder.field(_ID.getPreferredName(), id);
176181
builder.field(MATCHED.getPreferredName(), isMatch());
177182
if (hasExplanation()) {

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.elasticsearch.action.explain.ExplainRequest;
1212
import org.elasticsearch.client.node.NodeClient;
13+
import org.elasticsearch.core.RestApiVersion;
1314
import org.elasticsearch.common.Strings;
1415
import org.elasticsearch.index.query.QueryBuilder;
1516
import org.elasticsearch.rest.BaseRestHandler;
@@ -28,12 +29,20 @@
2829
* Rest action for computing a score explanation for specific documents.
2930
*/
3031
public class RestExplainAction extends BaseRestHandler {
32+
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] " +
33+
"Specifying a type in explain requests is deprecated.";
3134

3235
@Override
3336
public List<Route> routes() {
3437
return List.of(
3538
new Route(GET, "/{index}/_explain/{id}"),
36-
new Route(POST, "/{index}/_explain/{id}"));
39+
new Route(POST, "/{index}/_explain/{id}"),
40+
Route.builder(GET, "/{index}/{type}/{id}/_explain")
41+
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
42+
.build(),
43+
Route.builder(POST, "/{index}/{type}/{id}/_explain")
44+
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
45+
.build());
3746
}
3847

3948
@Override
@@ -43,6 +52,9 @@ public String getName() {
4352

4453
@Override
4554
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
55+
if(request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("type")) {
56+
request.param("type");
57+
}
4658
ExplainRequest explainRequest = new ExplainRequest(request.param("index"), request.param("id"));
4759

4860
explainRequest.parent(request.param("parent"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.rest.action.search;
10+
11+
import org.elasticsearch.action.explain.ExplainResponse;
12+
import org.elasticsearch.core.RestApiVersion;
13+
import org.elasticsearch.common.xcontent.XContentType;
14+
import org.elasticsearch.rest.RestRequest;
15+
import org.elasticsearch.test.rest.FakeRestRequest;
16+
import org.elasticsearch.test.rest.RestActionTestCase;
17+
import org.junit.Before;
18+
import org.mockito.Mockito;
19+
20+
import java.util.Collections;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
public class RestExplainActionTests extends RestActionTestCase {
25+
final List<String> contentTypeHeader = Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7));
26+
27+
@Before
28+
public void setUpAction() {
29+
RestExplainAction action = new RestExplainAction();
30+
controller().registerHandler(action);
31+
verifyingClient.setExecuteVerifier((actionType, request) -> Mockito.mock(ExplainResponse.class));
32+
verifyingClient.setExecuteLocallyVerifier((actionType, request) -> Mockito.mock(ExplainResponse.class));
33+
}
34+
35+
public void testTypeInPath() {
36+
RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry())
37+
.withHeaders(Map.of("Accept", contentTypeHeader))
38+
.withMethod(RestRequest.Method.GET)
39+
.withPath("/some_index/some_type/some_id/_explain")
40+
.build();
41+
dispatchRequest(deprecatedRequest);
42+
assertWarnings(RestExplainAction.TYPES_DEPRECATION_MESSAGE);
43+
44+
RestRequest validRequest = new FakeRestRequest.Builder(xContentRegistry())
45+
.withHeaders(Map.of("Accept", contentTypeHeader))
46+
.withMethod(RestRequest.Method.GET)
47+
.withPath("/some_index/_explain/some_id")
48+
.build();
49+
dispatchRequest(validRequest);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)