Skip to content

Commit

Permalink
[Rest Api Compatibility] Typed endpoints for search _count api (#73958)
Browse files Browse the repository at this point in the history
retrofits typed endpoints removed in #42112

relates #51816
  • Loading branch information
pgomulka authored Jun 14, 2021
1 parent bcc5e23 commit 13e884b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
3 changes: 0 additions & 3 deletions rest-api-spec/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ tasks.named("yamlRestCompatTest").configure {
systemProperty 'tests.rest.blacklist', ([
'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion and specifying both node_ids and node_names',
'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion without specifying nodes',
'count/11_basic_with_types/count body without query element',
'count/11_basic_with_types/count with body',
'count/11_basic_with_types/count with empty body',
'field_caps/30_filter/Field caps with index filter',
'get_source/11_basic_with_types/Basic with types',
'get_source/16_default_values_with_types/Default values',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
Expand All @@ -32,14 +34,21 @@
import static org.elasticsearch.search.internal.SearchContext.DEFAULT_TERMINATE_AFTER;

public class RestCountAction extends BaseRestHandler {

private final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestCountAction.class);
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in count requests is deprecated.";
@Override
public List<Route> routes() {
return List.of(
new Route(GET, "/_count"),
new Route(POST, "/_count"),
new Route(GET, "/{index}/_count"),
new Route(POST, "/{index}/_count"));
new Route(POST, "/{index}/_count"),
Route.builder(GET, "/{index}/{type}/_count")
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build(),
Route.builder(POST, "/{index}/{type}/_count")
.deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7)
.build());
}

@Override
Expand All @@ -49,6 +58,10 @@ public String getName() {

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
if (request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("type")) {
deprecationLogger.compatibleApiWarning("count_with_types", TYPES_DEPRECATION_MESSAGE);
request.param("type");
}
SearchRequest countRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index")));
countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0).trackTotalHits(true);
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.rest.action.search;


import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestRequest.Method;
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;

import static org.hamcrest.Matchers.instanceOf;

public class RestCountActionTests extends RestActionTestCase {

final List<String> contentTypeHeader = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));

@Before
public void setUpAction() {
controller().registerHandler(new RestCountAction());
verifyingClient.setExecuteVerifier((actionType, request) -> {
assertThat(request, instanceOf(SearchRequest.class));
return Mockito.mock(SearchResponse.class);
});
}

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

dispatchRequest(request);
assertWarnings(RestCountAction.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("Accept", contentTypeHeader))
.withMethod(Method.GET)
.withPath("/some_index/_count")
.withParams(params)
.build();

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

0 comments on commit 13e884b

Please sign in to comment.