Skip to content

Commit

Permalink
[Rest Api Compatibility] Typed endpoints for search and related endpo…
Browse files Browse the repository at this point in the history
…ints (#72155)

Implements a V7 compatible typed endpoints for REST for search related apis
retrofits the REST layer change removed in #41640

relates main meta issue #51816
relates types removal issue #54160
  • Loading branch information
pgomulka authored May 12, 2021
1 parent b3a1977 commit 85ed910
Show file tree
Hide file tree
Showing 27 changed files with 688 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
Expand Down Expand Up @@ -1227,7 +1228,7 @@ public void testMultiSearch() throws IOException {
};
MultiSearchRequest.readMultiLineFormat(new BytesArray(EntityUtils.toByteArray(request.getEntity())),
REQUEST_BODY_CONTENT_TYPE.xContent(), consumer, null, multiSearchRequest.indicesOptions(), null, null, null,
xContentRegistry(), true);
xContentRegistry(), true, RestApiVersion.current());
assertEquals(requests, multiSearchRequest.requests());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.script.mustache;

import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
Expand All @@ -27,7 +28,8 @@
import static org.elasticsearch.rest.RestRequest.Method.POST;

public class RestMultiSearchTemplateAction extends BaseRestHandler {

static final String TYPES_DEPRECATION_MESSAGE = "[types removal]"
+ " Specifying types in multi search template requests is deprecated.";
private static final Set<String> RESPONSE_PARAMS;

static {
Expand All @@ -50,7 +52,13 @@ public List<Route> routes() {
new Route(GET, "/_msearch/template"),
new Route(POST, "/_msearch/template"),
new Route(GET, "/{index}/_msearch/template"),
new Route(POST, "/{index}/_msearch/template"));
new Route(POST, "/{index}/_msearch/template"),
Route.builder(GET, "/{index}/{type}/_msearch/template")
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build(),
Route.builder(POST, "/{index}/{type}/_msearch/template")
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build());
}

@Override
Expand All @@ -68,6 +76,10 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
* Parses a {@link RestRequest} body and returns a {@link MultiSearchTemplateRequest}
*/
public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException {
if (restRequest.getRestApiVersion() == RestApiVersion.V_7 && restRequest.hasParam("type")) {
restRequest.param("type");
}

MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
if (restRequest.hasParam("max_concurrent_searches")) {
multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
Expand All @@ -27,6 +28,7 @@
import static org.elasticsearch.rest.RestRequest.Method.POST;

public class RestSearchTemplateAction extends BaseRestHandler {

public static final String TYPED_KEYS_PARAM = "typed_keys";
private static final Set<String> RESPONSE_PARAMS;

Expand All @@ -41,7 +43,13 @@ public List<Route> routes() {
new Route(GET, "/_search/template"),
new Route(POST, "/_search/template"),
new Route(GET, "/{index}/_search/template"),
new Route(POST, "/{index}/_search/template"));
new Route(POST, "/{index}/_search/template"),
Route.builder(GET, "/{index}/{type}/_search/template")
.deprecated(RestSearchAction.TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build(),
Route.builder(POST, "/{index}/{type}/_search/template")
.deprecated(RestSearchAction.TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.script.mustache;

import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase;
import org.junit.Before;
import org.mockito.Mockito;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class RestMultiSearchTemplateActionTests extends RestActionTestCase {
final List<String> contentTypeHeader = Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7));

@Before
public void setUpAction() {
controller().registerHandler(new RestMultiSearchTemplateAction(Settings.EMPTY));
//todo how to workaround this? we get AssertionError without this
verifyingClient.setExecuteVerifier((actionType, request) -> Mockito.mock(MultiSearchTemplateResponse.class));
verifyingClient.setExecuteLocallyVerifier((actionType, request) -> Mockito.mock(MultiSearchTemplateResponse.class));
}

public void testTypeInPath() {
String content = "{ \"index\": \"some_index\" } \n" +
"{\"source\": {\"query\" : {\"match_all\" :{}}}} \n";
BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8));

RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withMethod(RestRequest.Method.GET)
.withPath("/some_index/some_type/_msearch/template")
.withContent(bytesContent, null)
.build();

dispatchRequest(request);
assertWarnings(RestMultiSearchTemplateAction.TYPES_DEPRECATION_MESSAGE);
}

public void testTypeInBody() {
String content = "{ \"index\": \"some_index\", \"type\": \"some_type\" } \n" +
"{\"source\": {\"query\" : {\"match_all\" :{}}}} \n";
BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8));

RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withPath("/some_index/_msearch/template")
.withContent(bytesContent, null)
.build();

dispatchRequest(request);
assertWarnings(RestMultiSearchTemplateAction.TYPES_DEPRECATION_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.script.mustache;

import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.search.RestSearchAction;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase;
import org.junit.Before;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RestSearchTemplateActionTests extends RestActionTestCase {
final List<String> contentTypeHeader = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));

@Before
public void setUpAction() {
controller().registerHandler(new RestSearchTemplateAction());
verifyingClient.setExecuteVerifier((actionType, request) -> Mockito.mock(SearchTemplateResponse.class));
verifyingClient.setExecuteLocallyVerifier((actionType, request) -> Mockito.mock(SearchTemplateResponse.class));
}

public void testTypeInPath() {
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withMethod(RestRequest.Method.GET)
.withPath("/some_index/some_type/_search/template")
.build();

dispatchRequest(request);
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
}

public void testTypeParameter() {
Map<String, String> params = new HashMap<>();
params.put("type", "some_type");

RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withMethod(RestRequest.Method.GET)
.withPath("/some_index/_search/template")
.withParams(params)
.build();

dispatchRequest(request);
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
package org.elasticsearch.index.reindex;

import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.search.RestSearchAction;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -28,7 +30,12 @@ public RestDeleteByQueryAction() {

@Override
public List<Route> routes() {
return List.of(new Route(POST, "/{index}/_delete_by_query"));
return List.of(new Route(POST, "/{index}/_delete_by_query"),
Route.builder(POST, "/{index}/{type}/_delete_by_query")
.deprecated(RestSearchAction.TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build()
);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
package org.elasticsearch.index.reindex;

import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.search.RestSearchAction;
import org.elasticsearch.script.Script;

import java.io.IOException;
Expand All @@ -29,7 +31,12 @@ public RestUpdateByQueryAction() {

@Override
public List<Route> routes() {
return List.of(new Route(POST, "/{index}/_update_by_query"));
return List.of(
new Route(POST, "/{index}/_update_by_query"),
Route.builder(POST, "/{index}/{type}/_update_by_query")
.deprecated(RestSearchAction.TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build()
);
}

@Override
Expand All @@ -44,6 +51,9 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client

@Override
protected UpdateByQueryRequest buildRequest(RestRequest request, NamedWriteableRegistry namedWriteableRegistry) throws IOException {
if (request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("type")) {
request.param("type");
}
/*
* Passing the search request through UpdateByQueryRequest first allows
* it to set its own defaults which differ from SearchRequest's
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.index.reindex;

import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.search.RestSearchAction;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase;
import org.junit.Before;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class RestDeleteByQueryActionTests extends RestActionTestCase {

final List<String> contentTypeHeader = Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7));

@Before
public void setUpAction() {
controller().registerHandler(new RestDeleteByQueryAction());
verifyingClient.setExecuteVerifier((actionType, request) -> Mockito.mock(BulkByScrollResponse.class));
verifyingClient.setExecuteLocallyVerifier((actionType, request) -> Mockito.mock(BulkByScrollResponse.class));
}

public void testTypeInPath() throws IOException {
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withMethod(RestRequest.Method.POST)
.withPath("/some_index/some_type/_delete_by_query")
.build();

// checks the type in the URL is propagated correctly to the request object
// only works after the request is dispatched, so its params are filled from url.
dispatchRequest(request);


// RestDeleteByQueryAction itself doesn't check for a deprecated type usage
// checking here for a deprecation from its internal search request
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.index.reindex;

import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.search.RestSearchAction;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase;
import org.junit.Before;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class RestUpdateByQueryActionTests extends RestActionTestCase {

final List<String> contentTypeHeader = Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7));

@Before
public void setUpAction() {
controller().registerHandler(new RestUpdateByQueryAction());
verifyingClient.setExecuteVerifier((actionType, request) -> Mockito.mock(BulkByScrollResponse.class));
verifyingClient.setExecuteLocallyVerifier((actionType, request) -> Mockito.mock(BulkByScrollResponse.class));
}

public void testTypeInPath() throws IOException {
RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withMethod(RestRequest.Method.POST)
.withPath("/some_index/some_type/_update_by_query")
.build();

// checks the type in the URL is propagated correctly to the request object
// only works after the request is dispatched, so its params are filled from url.
dispatchRequest(request);

// RestUpdateByQueryAction itself doesn't check for a deprecated type usage
// checking here for a deprecation from its internal search request
assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE);
}
}
Loading

0 comments on commit 85ed910

Please sign in to comment.