Browse Source

[Rest Api Compatibility] Allow for type in geo shape query (#74553)

Allowing to specify type in geo_shape query. previously removed by #47792
types removal compatible meta #54160
main meta issue #51816
Przemyslaw Gomulka 4 years ago
parent
commit
4fa1c8db46

+ 12 - 0
server/src/main/java/org/elasticsearch/index/query/AbstractGeometryQueryBuilder.java

@@ -15,6 +15,7 @@ import org.elasticsearch.Version;
 import org.elasticsearch.action.ActionListener;
 import org.elasticsearch.action.ActionListener;
 import org.elasticsearch.action.get.GetRequest;
 import org.elasticsearch.action.get.GetRequest;
 import org.elasticsearch.client.Client;
 import org.elasticsearch.client.Client;
+import org.elasticsearch.common.logging.DeprecationLogger;
 import org.elasticsearch.common.xcontent.ParseField;
 import org.elasticsearch.common.xcontent.ParseField;
 import org.elasticsearch.common.ParsingException;
 import org.elasticsearch.common.ParsingException;
 import org.elasticsearch.common.geo.GeoJson;
 import org.elasticsearch.common.geo.GeoJson;
@@ -29,6 +30,7 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.common.xcontent.XContentHelper;
 import org.elasticsearch.common.xcontent.XContentHelper;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.core.RestApiVersion;
 import org.elasticsearch.geometry.Geometry;
 import org.elasticsearch.geometry.Geometry;
 import org.elasticsearch.index.mapper.MappedFieldType;
 import org.elasticsearch.index.mapper.MappedFieldType;
 import org.elasticsearch.index.mapper.MapperService;
 import org.elasticsearch.index.mapper.MapperService;
@@ -41,6 +43,9 @@ import java.util.function.Supplier;
  * Base {@link QueryBuilder} that builds a Geometry Query
  * Base {@link QueryBuilder} that builds a Geometry Query
  */
  */
 public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQueryBuilder<QB>> extends AbstractQueryBuilder<QB> {
 public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQueryBuilder<QB>> extends AbstractQueryBuilder<QB> {
+    static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [geo_shape] queries. " +
+        "The type should no longer be specified in the [indexed_shape] section.";
+    private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(AbstractGeometryQueryBuilder.class);
 
 
     public static final String DEFAULT_SHAPE_INDEX_NAME = "shapes";
     public static final String DEFAULT_SHAPE_INDEX_NAME = "shapes";
     public static final String DEFAULT_SHAPE_FIELD_NAME = "shape";
     public static final String DEFAULT_SHAPE_FIELD_NAME = "shape";
@@ -53,6 +58,7 @@ public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQu
     protected static final ParseField RELATION_FIELD = new ParseField("relation");
     protected static final ParseField RELATION_FIELD = new ParseField("relation");
     protected static final ParseField INDEXED_SHAPE_FIELD = new ParseField("indexed_shape");
     protected static final ParseField INDEXED_SHAPE_FIELD = new ParseField("indexed_shape");
     protected static final ParseField SHAPE_ID_FIELD = new ParseField("id");
     protected static final ParseField SHAPE_ID_FIELD = new ParseField("id");
+    protected static final ParseField SHAPE_TYPE_FIELD = new ParseField("type");
     protected static final ParseField SHAPE_INDEX_FIELD = new ParseField("index");
     protected static final ParseField SHAPE_INDEX_FIELD = new ParseField("index");
     protected static final ParseField SHAPE_PATH_FIELD = new ParseField("path");
     protected static final ParseField SHAPE_PATH_FIELD = new ParseField("path");
     protected static final ParseField SHAPE_ROUTING_FIELD = new ParseField("routing");
     protected static final ParseField SHAPE_ROUTING_FIELD = new ParseField("routing");
@@ -408,6 +414,9 @@ public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQu
         } else {
         } else {
             builder.startObject(INDEXED_SHAPE_FIELD.getPreferredName())
             builder.startObject(INDEXED_SHAPE_FIELD.getPreferredName())
                 .field(SHAPE_ID_FIELD.getPreferredName(), indexedShapeId);
                 .field(SHAPE_ID_FIELD.getPreferredName(), indexedShapeId);
+            if (builder.getRestApiVersion() == RestApiVersion.V_7) {
+                builder.field(SHAPE_TYPE_FIELD.getPreferredName(), MapperService.SINGLE_MAPPING_NAME);
+            }
             if (indexedShapeIndex != null) {
             if (indexedShapeIndex != null) {
                 builder.field(SHAPE_INDEX_FIELD.getPreferredName(), indexedShapeIndex);
                 builder.field(SHAPE_INDEX_FIELD.getPreferredName(), indexedShapeIndex);
             }
             }
@@ -521,6 +530,9 @@ public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQu
                                 } else if (token.isValue()) {
                                 } else if (token.isValue()) {
                                     if (SHAPE_ID_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                     if (SHAPE_ID_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                         params.id = parser.text();
                                         params.id = parser.text();
+                                    } else if (parser.getRestApiVersion() == RestApiVersion.V_7 &&
+                                        SHAPE_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
+                                        deprecationLogger.compatibleApiWarning("geo_share_query_with_types", TYPES_DEPRECATION_MESSAGE);
                                     } else if (SHAPE_INDEX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                     } else if (SHAPE_INDEX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                         params.index = parser.text();
                                         params.index = parser.text();
                                     } else if (SHAPE_PATH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                     } else if (SHAPE_PATH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {

+ 70 - 0
x-pack/plugin/spatial/src/yamlRestCompatTest/resources/rest-api-spec/test/v7compat/geo_shape/10_compat_geo_shape_with_types.yml

@@ -0,0 +1,70 @@
+---
+setup:
+  - skip:
+      version: "9.0.0 - "
+      reason: "compatible from 8.x to 7.x"
+      features:
+        - "headers"
+        - "warnings"
+        - "allowed_warnings_regex"
+
+---
+"Test geo_shape with type":
+  - do:
+      headers:
+        Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
+        Accept: "application/vnd.elasticsearch+json;compatible-with=7"
+      allowed_warnings_regex:
+        - "\\[types removal\\].*"
+      indices.create:
+        index: shapes
+        include_type_name: true
+        body:
+          mappings:
+            _doc:
+              properties:
+                location:
+                  type: geo_shape
+
+
+  - do:
+      headers:
+        Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
+        Accept: "application/vnd.elasticsearch+json;compatible-with=7"
+      allowed_warnings_regex:
+        - "\\[types removal\\].*"
+      index:
+        index:  shapes
+        type:   _doc
+        id:     deu
+        body:
+          location:
+            type : "envelope"
+            coordinates:  [[13.0, 53.0], [14.0, 52.0]]
+
+  - do:
+      indices.refresh: {}
+
+  - do:
+      headers:
+        Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
+        Accept: "application/vnd.elasticsearch+json;compatible-with=7"
+      warnings:
+        - "[types removal] Types are deprecated in [geo_shape] queries. The type should no longer be specified in the [indexed_shape] section."
+      search:
+        rest_total_hits_as_int: true
+        index: shapes
+        size: 0
+        body:
+          query:
+            bool:
+              filter:
+                geo_shape:
+                  location:
+                    indexed_shape:
+                        index: "shapes"
+                        type: "_doc"
+                        id: "deu"
+                        path: "location"
+
+  - match: {hits.total:      1    }