Skip to content

Commit 4fa1c8d

Browse files
authored
[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
1 parent 8a54a05 commit 4fa1c8d

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

server/src/main/java/org/elasticsearch/index/query/AbstractGeometryQueryBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.ActionListener;
1616
import org.elasticsearch.action.get.GetRequest;
1717
import org.elasticsearch.client.Client;
18+
import org.elasticsearch.common.logging.DeprecationLogger;
1819
import org.elasticsearch.common.xcontent.ParseField;
1920
import org.elasticsearch.common.ParsingException;
2021
import org.elasticsearch.common.geo.GeoJson;
@@ -29,6 +30,7 @@
2930
import org.elasticsearch.common.xcontent.XContentBuilder;
3031
import org.elasticsearch.common.xcontent.XContentHelper;
3132
import org.elasticsearch.common.xcontent.XContentParser;
33+
import org.elasticsearch.core.RestApiVersion;
3234
import org.elasticsearch.geometry.Geometry;
3335
import org.elasticsearch.index.mapper.MappedFieldType;
3436
import org.elasticsearch.index.mapper.MapperService;
@@ -41,6 +43,9 @@
4143
* Base {@link QueryBuilder} that builds a Geometry Query
4244
*/
4345
public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQueryBuilder<QB>> extends AbstractQueryBuilder<QB> {
46+
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [geo_shape] queries. " +
47+
"The type should no longer be specified in the [indexed_shape] section.";
48+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(AbstractGeometryQueryBuilder.class);
4449

4550
public static final String DEFAULT_SHAPE_INDEX_NAME = "shapes";
4651
public static final String DEFAULT_SHAPE_FIELD_NAME = "shape";
@@ -53,6 +58,7 @@ public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQu
5358
protected static final ParseField RELATION_FIELD = new ParseField("relation");
5459
protected static final ParseField INDEXED_SHAPE_FIELD = new ParseField("indexed_shape");
5560
protected static final ParseField SHAPE_ID_FIELD = new ParseField("id");
61+
protected static final ParseField SHAPE_TYPE_FIELD = new ParseField("type");
5662
protected static final ParseField SHAPE_INDEX_FIELD = new ParseField("index");
5763
protected static final ParseField SHAPE_PATH_FIELD = new ParseField("path");
5864
protected static final ParseField SHAPE_ROUTING_FIELD = new ParseField("routing");
@@ -408,6 +414,9 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
408414
} else {
409415
builder.startObject(INDEXED_SHAPE_FIELD.getPreferredName())
410416
.field(SHAPE_ID_FIELD.getPreferredName(), indexedShapeId);
417+
if (builder.getRestApiVersion() == RestApiVersion.V_7) {
418+
builder.field(SHAPE_TYPE_FIELD.getPreferredName(), MapperService.SINGLE_MAPPING_NAME);
419+
}
411420
if (indexedShapeIndex != null) {
412421
builder.field(SHAPE_INDEX_FIELD.getPreferredName(), indexedShapeIndex);
413422
}
@@ -521,6 +530,9 @@ public static ParsedGeometryQueryParams parsedParamsFromXContent(XContentParser
521530
} else if (token.isValue()) {
522531
if (SHAPE_ID_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
523532
params.id = parser.text();
533+
} else if (parser.getRestApiVersion() == RestApiVersion.V_7 &&
534+
SHAPE_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
535+
deprecationLogger.compatibleApiWarning("geo_share_query_with_types", TYPES_DEPRECATION_MESSAGE);
524536
} else if (SHAPE_INDEX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
525537
params.index = parser.text();
526538
} else if (SHAPE_PATH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
setup:
3+
- skip:
4+
version: "9.0.0 - "
5+
reason: "compatible from 8.x to 7.x"
6+
features:
7+
- "headers"
8+
- "warnings"
9+
- "allowed_warnings_regex"
10+
11+
---
12+
"Test geo_shape with type":
13+
- do:
14+
headers:
15+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
16+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
17+
allowed_warnings_regex:
18+
- "\\[types removal\\].*"
19+
indices.create:
20+
index: shapes
21+
include_type_name: true
22+
body:
23+
mappings:
24+
_doc:
25+
properties:
26+
location:
27+
type: geo_shape
28+
29+
30+
- do:
31+
headers:
32+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
33+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
34+
allowed_warnings_regex:
35+
- "\\[types removal\\].*"
36+
index:
37+
index: shapes
38+
type: _doc
39+
id: deu
40+
body:
41+
location:
42+
type : "envelope"
43+
coordinates: [[13.0, 53.0], [14.0, 52.0]]
44+
45+
- do:
46+
indices.refresh: {}
47+
48+
- do:
49+
headers:
50+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
51+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
52+
warnings:
53+
- "[types removal] Types are deprecated in [geo_shape] queries. The type should no longer be specified in the [indexed_shape] section."
54+
search:
55+
rest_total_hits_as_int: true
56+
index: shapes
57+
size: 0
58+
body:
59+
query:
60+
bool:
61+
filter:
62+
geo_shape:
63+
location:
64+
indexed_shape:
65+
index: "shapes"
66+
type: "_doc"
67+
id: "deu"
68+
path: "location"
69+
70+
- match: {hits.total: 1 }

0 commit comments

Comments
 (0)