瀏覽代碼

Deprecate types in validate query requests. (#35575)

Julie Tibshirani 7 年之前
父節點
當前提交
40ba4de5e6

+ 5 - 5
docs/reference/search/validate.asciidoc

@@ -60,7 +60,7 @@ The query may also be sent in the request body:
 
 [source,js]
 --------------------------------------------------
-GET twitter/_doc/_validate/query
+GET twitter/_validate/query
 {
   "query" : {
     "bool" : {
@@ -87,7 +87,7 @@ due to dynamic mapping, and 'foo' does not correctly parse into a date:
 
 [source,js]
 --------------------------------------------------
-GET twitter/_doc/_validate/query
+GET twitter/_validate/query
 {
   "query": {
     "query_string": {
@@ -110,7 +110,7 @@ about why a query failed:
 
 [source,js]
 --------------------------------------------------
-GET twitter/_doc/_validate/query?explain=true
+GET twitter/_validate/query?explain=true
 {
   "query": {
     "query_string": {
@@ -150,7 +150,7 @@ For More Like This:
 
 [source,js]
 --------------------------------------------------
-GET twitter/_doc/_validate/query?rewrite=true
+GET twitter/_validate/query?rewrite=true
 {
   "query": {
     "more_like_this": {
@@ -197,7 +197,7 @@ For Fuzzy Queries:
 
 [source,js]
 --------------------------------------------------
-GET twitter/_doc/_validate/query?rewrite=true&all_shards=true
+GET twitter/_validate/query?rewrite=true&all_shards=true
 {
   "query": {
     "match": {

+ 8 - 0
server/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java

@@ -87,14 +87,22 @@ public class ValidateQueryRequest extends BroadcastRequest<ValidateQueryRequest>
 
     /**
      * The types of documents the query will run against. Defaults to all types.
+     *
+     * @deprecated Types are in the process of being removed. Instead of using a type, prefer to
+     * filter on a field on the document.
      */
+    @Deprecated
     public String[] types() {
         return this.types;
     }
 
     /**
      * The types of documents the query will run against. Defaults to all types.
+     *
+     * @deprecated Types are in the process of being removed. Instead of using a type, prefer to
+     * filter on a field on the document.
      */
+    @Deprecated
     public ValidateQueryRequest types(String... types) {
         this.types = types;
         return this;

+ 13 - 1
server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryAction.java

@@ -19,6 +19,7 @@
 
 package org.elasticsearch.rest.action.admin.indices;
 
+import org.apache.logging.log4j.LogManager;
 import org.elasticsearch.action.admin.indices.validate.query.QueryExplanation;
 import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
 import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
@@ -26,6 +27,7 @@ import org.elasticsearch.action.support.IndicesOptions;
 import org.elasticsearch.client.node.NodeClient;
 import org.elasticsearch.common.ParsingException;
 import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.logging.DeprecationLogger;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.rest.BaseRestHandler;
@@ -43,6 +45,11 @@ import static org.elasticsearch.rest.RestRequest.Method.POST;
 import static org.elasticsearch.rest.RestStatus.OK;
 
 public class RestValidateQueryAction extends BaseRestHandler {
+    private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
+        LogManager.getLogger(RestValidateQueryAction.class));
+    static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
+        " Specifying types in validate query requests is deprecated.";
+
     public RestValidateQueryAction(Settings settings, RestController controller) {
         super(settings);
         controller.registerHandler(GET, "/_validate/query", this);
@@ -63,7 +70,12 @@ public class RestValidateQueryAction extends BaseRestHandler {
         ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
         validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
         validateQueryRequest.explain(request.paramAsBoolean("explain", false));
-        validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
+
+        if (request.hasParam("type")) {
+            deprecationLogger.deprecated(TYPES_DEPRECATION_MESSAGE);
+            validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
+        }
+
         validateQueryRequest.rewrite(request.paramAsBoolean("rewrite", false));
         validateQueryRequest.allShards(request.paramAsBoolean("all_shards", false));
 

+ 34 - 1
server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java

@@ -27,7 +27,10 @@ import org.elasticsearch.action.support.TransportAction;
 import org.elasticsearch.client.node.NodeClient;
 import org.elasticsearch.common.bytes.BytesArray;
 import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.util.concurrent.ThreadContext;
 import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
+import org.elasticsearch.rest.RestChannel;
 import org.elasticsearch.rest.RestController;
 import org.elasticsearch.rest.RestRequest;
 import org.elasticsearch.search.AbstractSearchTestCase;
@@ -56,7 +59,8 @@ public class RestValidateQueryActionTests extends AbstractSearchTestCase {
     private static NodeClient client = new NodeClient(Settings.EMPTY, threadPool);
 
     private static UsageService usageService = new UsageService();
-    private static RestController controller = new RestController(emptySet(), null, client, null, usageService);
+    private static RestController controller = new RestController(emptySet(), null, client,
+        new NoneCircuitBreakerService(), usageService);
     private static RestValidateQueryAction action = new RestValidateQueryAction(Settings.EMPTY, controller);
 
     /**
@@ -148,4 +152,33 @@ public class RestValidateQueryActionTests extends AbstractSearchTestCase {
             .withContent(new BytesArray(content), XContentType.JSON)
             .build();
     }
+
+    public void testTypeInPath() {
+        RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
+            .withMethod(RestRequest.Method.GET)
+            .withPath("/some_index/some_type/_validate/query")
+            .build();
+
+        performRequest(request);
+        assertWarnings(RestValidateQueryAction.TYPES_DEPRECATION_MESSAGE);
+    }
+
+    public void testTypeParameter() {
+        Map<String, String> params = new HashMap<>();
+        params.put("type", "some_type");
+        RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
+            .withMethod(RestRequest.Method.GET)
+            .withPath("_validate/query")
+            .withParams(params)
+            .build();
+
+        performRequest(request);
+        assertWarnings(RestValidateQueryAction.TYPES_DEPRECATION_MESSAGE);
+    }
+
+    private void performRequest(RestRequest request) {
+        RestChannel channel = new FakeRestChannel(request, false, 1);
+        ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
+        controller.dispatchRequest(request, channel, threadContext);
+    }
 }