Skip to content

Commit 0f30c79

Browse files
authored
Remove legacy geo code from AbstractGeometryQueryBuilder classes (elastic#74741)
removes references to Legacy ShapeParser and ShapeBuilder in AbstractGeometryQueryBuilder classes in favour to Geometry and GeometryParser.
1 parent 718d79c commit 0f30c79

File tree

16 files changed

+178
-172
lines changed

16 files changed

+178
-172
lines changed

modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Arrays;
4444
import java.util.Collection;
4545
import java.util.Collections;
46+
import java.util.HashMap;
4647
import java.util.HashSet;
4748
import java.util.List;
4849
import java.util.Map;
@@ -188,10 +189,12 @@ public void testIndexedDocumentDoesNotExist() throws IOException {
188189
}
189190

190191
@Override
191-
protected Set<String> getObjectsHoldingArbitraryContent() {
192+
protected Map<String, String> getObjectsHoldingArbitraryContent() {
192193
//document contains arbitrary content, no error expected when an object is added to it
193-
return new HashSet<>(Arrays.asList(PercolateQueryBuilder.DOCUMENT_FIELD.getPreferredName(),
194-
PercolateQueryBuilder.DOCUMENTS_FIELD.getPreferredName()));
194+
final Map<String, String> objects = new HashMap<>();
195+
objects.put(PercolateQueryBuilder.DOCUMENT_FIELD.getPreferredName(), null);
196+
objects.put(PercolateQueryBuilder.DOCUMENTS_FIELD.getPreferredName(), null);
197+
return objects;
195198
}
196199

197200
public void testRequiredParameters() {

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.common.geo.GeometryIO;
2323
import org.elasticsearch.common.geo.GeometryParser;
2424
import org.elasticsearch.common.geo.ShapeRelation;
25-
import org.elasticsearch.common.geo.builders.ShapeBuilder;
2625
import org.elasticsearch.common.io.stream.StreamInput;
2726
import org.elasticsearch.common.io.stream.StreamOutput;
2827
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
@@ -78,21 +77,6 @@ public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQu
7877

7978
protected boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
8079

81-
/**
82-
* Creates a new ShapeQueryBuilder whose Query will be against the given
83-
* field name using the given Shape
84-
*
85-
* @param fieldName
86-
* Name of the field that will be queried
87-
* @param shape
88-
* Shape used in the Query
89-
* @deprecated use {@link #AbstractGeometryQueryBuilder(String, Geometry)} instead
90-
*/
91-
@Deprecated
92-
protected AbstractGeometryQueryBuilder(String fieldName, ShapeBuilder shape) {
93-
this(fieldName, shape == null ? null : shape.buildGeometry(), null);
94-
}
95-
9680
/**
9781
* Creates a new AbstractGeometryQueryBuilder whose Query will be against the given
9882
* field name using the given Shape
@@ -484,7 +468,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
484468
protected abstract static class ParsedGeometryQueryParams {
485469
public String fieldName;
486470
public ShapeRelation relation;
487-
public ShapeBuilder shape;
471+
public Geometry shape;
488472

489473
public String id = null;
490474
public String index = null;

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
import org.apache.lucene.search.ConstantScoreQuery;
1212
import org.apache.lucene.search.Query;
13+
import org.elasticsearch.common.geo.GeometryParser;
1314
import org.elasticsearch.common.xcontent.ParseField;
1415
import org.elasticsearch.common.ParsingException;
1516
import org.elasticsearch.common.geo.ShapeRelation;
1617
import org.elasticsearch.common.geo.SpatialStrategy;
17-
import org.elasticsearch.common.geo.builders.ShapeBuilder;
18-
import org.elasticsearch.common.geo.parsers.ShapeParser;
1918
import org.elasticsearch.common.io.stream.StreamInput;
2019
import org.elasticsearch.common.io.stream.StreamOutput;
2120
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -25,6 +24,7 @@
2524
import org.elasticsearch.index.mapper.MappedFieldType;
2625

2726
import java.io.IOException;
27+
import java.text.ParseException;
2828
import java.util.Objects;
2929
import java.util.function.Supplier;
3030

@@ -53,22 +53,6 @@ public GeoShapeQueryBuilder(String fieldName, Geometry shape) {
5353
super(fieldName, shape);
5454
}
5555

56-
/**
57-
* Creates a new GeoShapeQueryBuilder whose Query will be against the given
58-
* field name using the given Shape
59-
*
60-
* @param fieldName
61-
* Name of the field that will be queried
62-
* @param shape
63-
* Shape used in the Query
64-
*
65-
* @deprecated use {@link #GeoShapeQueryBuilder(String, Geometry)} instead
66-
*/
67-
@Deprecated
68-
public GeoShapeQueryBuilder(String fieldName, ShapeBuilder shape) {
69-
super(fieldName, shape);
70-
}
71-
7256
/**
7357
* Creates a new GeoShapeQueryBuilder whose Query will be against the given
7458
* field name and will use the Shape found with the given shape id and supplier
@@ -199,12 +183,17 @@ protected GeoShapeQueryBuilder doRewrite(QueryRewriteContext queryRewriteContext
199183

200184
private static class ParsedGeoShapeQueryParams extends ParsedGeometryQueryParams {
201185
SpatialStrategy strategy;
186+
private final GeometryParser geometryParser = new GeometryParser(true, true, true);
202187

203188
@Override
204189
protected boolean parseXContentField(XContentParser parser) throws IOException {
205190
SpatialStrategy strategy;
206191
if (SHAPE_FIELD.match(parser.currentName(), parser.getDeprecationHandler())) {
207-
this.shape = ShapeParser.parse(parser);
192+
try {
193+
this.shape = geometryParser.parse(parser);
194+
} catch (ParseException e) {
195+
throw new IOException(e);
196+
}
208197
return true;
209198
} else if (STRATEGY_FIELD.match(parser.currentName(), parser.getDeprecationHandler())) {
210199
String strategyName = parser.text();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ public static GeoShapeQueryBuilder geoShapeQuery(String name, Geometry shape) th
645645
*/
646646
@Deprecated
647647
public static GeoShapeQueryBuilder geoShapeQuery(String name, ShapeBuilder shape) throws IOException {
648-
return new GeoShapeQueryBuilder(name, shape);
648+
return new GeoShapeQueryBuilder(name, shape.buildGeometry());
649649
}
650650

651651
public static GeoShapeQueryBuilder geoShapeQuery(String name, String indexedShapeId) {

server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoPointTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
package org.elasticsearch.index.query;
99

1010
import org.elasticsearch.common.geo.ShapeRelation;
11-
import org.elasticsearch.common.geo.builders.ShapeBuilder;
12-
import org.elasticsearch.test.geo.RandomShapeGenerator;
11+
import org.elasticsearch.geo.GeometryTestUtils;
12+
import org.elasticsearch.geometry.Geometry;
1313

1414
public class GeoShapeQueryBuilderGeoPointTests extends GeoShapeQueryBuilderTests {
1515

@@ -18,14 +18,13 @@ protected String fieldName() {
1818
}
1919

2020
protected GeoShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) {
21-
RandomShapeGenerator.ShapeType shapeType = RandomShapeGenerator.ShapeType.POLYGON;
22-
ShapeBuilder<?, ?, ?> shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
21+
Geometry geometry = GeometryTestUtils.randomPolygon(false);
2322
GeoShapeQueryBuilder builder;
2423
clearShapeFields();
2524
if (indexedShape == false) {
26-
builder = new GeoShapeQueryBuilder(fieldName(), shape);
25+
builder = new GeoShapeQueryBuilder(fieldName(), geometry);
2726
} else {
28-
indexedShapeToReturn = shape;
27+
indexedShapeToReturn = geometry;
2928
indexedShapeId = randomAlphaOfLengthBetween(3, 20);
3029
builder = new GeoShapeQueryBuilder(fieldName(), indexedShapeId);
3130
if (randomBoolean()) {

server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoShapeTests.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
import org.elasticsearch.Version;
1111
import org.elasticsearch.common.geo.ShapeRelation;
12-
import org.elasticsearch.common.geo.builders.ShapeBuilder;
13-
import org.elasticsearch.test.geo.RandomShapeGenerator;
12+
import org.elasticsearch.geo.GeometryTestUtils;
13+
import org.elasticsearch.geometry.Geometry;
14+
import org.elasticsearch.geometry.ShapeType;
1415

1516
public class GeoShapeQueryBuilderGeoShapeTests extends GeoShapeQueryBuilderTests {
1617

@@ -19,19 +20,19 @@ protected String fieldName() {
1920
}
2021

2122
protected GeoShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) {
22-
RandomShapeGenerator.ShapeType shapeType = randomFrom(
23-
RandomShapeGenerator.ShapeType.POINT,
24-
RandomShapeGenerator.ShapeType.MULTIPOINT,
25-
RandomShapeGenerator.ShapeType.LINESTRING,
26-
RandomShapeGenerator.ShapeType.MULTILINESTRING,
27-
RandomShapeGenerator.ShapeType.POLYGON);
28-
ShapeBuilder<?, ?, ?> shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
23+
ShapeType shapeType = randomFrom(
24+
ShapeType.POINT,
25+
ShapeType.MULTIPOINT,
26+
ShapeType.LINESTRING,
27+
ShapeType.MULTILINESTRING,
28+
ShapeType.POLYGON);
29+
Geometry geometry = GeometryTestUtils.randomGeometry(shapeType, false);
2930
GeoShapeQueryBuilder builder;
3031
clearShapeFields();
3132
if (indexedShape == false) {
32-
builder = new GeoShapeQueryBuilder(fieldName(), shape);
33+
builder = new GeoShapeQueryBuilder(fieldName(), geometry);
3334
} else {
34-
indexedShapeToReturn = shape;
35+
indexedShapeToReturn = geometry;
3536
indexedShapeId = randomAlphaOfLengthBetween(3, 20);
3637
builder = new GeoShapeQueryBuilder(fieldName(), indexedShapeId);
3738
if (randomBoolean()) {
@@ -50,14 +51,14 @@ protected GeoShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) {
5051
if (randomBoolean()) {
5152
SearchExecutionContext context = createSearchExecutionContext();
5253
if (context.indexVersionCreated().onOrAfter(Version.V_7_5_0)) { // CONTAINS is only supported from version 7.5
53-
if (shapeType == RandomShapeGenerator.ShapeType.LINESTRING || shapeType == RandomShapeGenerator.ShapeType.MULTILINESTRING) {
54+
if (shapeType == ShapeType.LINESTRING || shapeType == ShapeType.MULTILINESTRING) {
5455
builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.CONTAINS));
5556
} else {
5657
builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS,
5758
ShapeRelation.WITHIN, ShapeRelation.CONTAINS));
5859
}
5960
} else {
60-
if (shapeType == RandomShapeGenerator.ShapeType.LINESTRING || shapeType == RandomShapeGenerator.ShapeType.MULTILINESTRING) {
61+
if (shapeType == ShapeType.LINESTRING || shapeType == ShapeType.MULTILINESTRING) {
6162
builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS));
6263
} else {
6364
builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.WITHIN));

server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717
import org.elasticsearch.common.Strings;
1818
import org.elasticsearch.common.bytes.BytesArray;
1919
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
20-
import org.elasticsearch.common.geo.builders.ShapeBuilder;
2120
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2221
import org.elasticsearch.common.xcontent.XContentBuilder;
2322
import org.elasticsearch.common.xcontent.XContentFactory;
2423
import org.elasticsearch.common.xcontent.XContentParser;
2524
import org.elasticsearch.common.xcontent.json.JsonXContent;
25+
import org.elasticsearch.geo.GeometryTestUtils;
26+
import org.elasticsearch.geometry.Geometry;
27+
import org.elasticsearch.geometry.utils.WellKnownText;
2628
import org.elasticsearch.index.get.GetResult;
2729
import org.elasticsearch.test.AbstractQueryTestCase;
28-
import org.elasticsearch.test.geo.RandomShapeGenerator;
29-
import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType;
3030
import org.junit.After;
3131
import org.locationtech.jts.geom.Coordinate;
3232

3333
import java.io.IOException;
34+
import java.util.Collections;
35+
import java.util.Map;
3436

3537
import static org.hamcrest.CoreMatchers.instanceOf;
3638
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -44,7 +46,7 @@ public abstract class GeoShapeQueryBuilderTests extends AbstractQueryTestCase<Ge
4446
protected static String indexedShapePath;
4547
protected static String indexedShapeIndex;
4648
protected static String indexedShapeRouting;
47-
protected static ShapeBuilder<?, ?, ?> indexedShapeToReturn;
49+
protected static Geometry indexedShapeToReturn;
4850

4951
protected abstract String fieldName();
5052

@@ -70,7 +72,7 @@ protected GetResponse executeGet(GetRequest getRequest) {
7072
try {
7173
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
7274
builder.startObject();
73-
builder.field(expectedShapePath, indexedShapeToReturn);
75+
builder.field(expectedShapePath, WellKnownText.toWKT(indexedShapeToReturn));
7476
builder.field(randomAlphaOfLengthBetween(10, 20), "something");
7577
builder.endObject();
7678
json = Strings.toString(builder);
@@ -100,23 +102,23 @@ protected void doAssertLuceneQuery(GeoShapeQueryBuilder queryBuilder, Query quer
100102
}
101103

102104
public void testNoFieldName() throws Exception {
103-
ShapeBuilder<?, ?, ?> shape = RandomShapeGenerator.createShapeWithin(random(), null);
104-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GeoShapeQueryBuilder(null, shape));
105+
Geometry geometry = GeometryTestUtils.randomGeometry(false);
106+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GeoShapeQueryBuilder(null, geometry));
105107
assertEquals("fieldName is required", e.getMessage());
106108
}
107109

108-
public void testNoShape() throws IOException {
109-
expectThrows(IllegalArgumentException.class, () -> new GeoShapeQueryBuilder(fieldName(), (ShapeBuilder) null));
110+
public void testNoShape() {
111+
expectThrows(IllegalArgumentException.class, () -> new GeoShapeQueryBuilder(fieldName(), (Geometry) null));
110112
}
111113

112-
public void testNoIndexedShape() throws IOException {
114+
public void testNoIndexedShape() {
113115
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
114116
() -> new GeoShapeQueryBuilder(fieldName(), null, null));
115117
assertEquals("either shape or indexedShapeId is required", e.getMessage());
116118
}
117119

118-
public void testNoRelation() throws IOException {
119-
ShapeBuilder<?, ?, ?> shape = RandomShapeGenerator.createShapeWithin(random(), null);
120+
public void testNoRelation() {
121+
Geometry shape = GeometryTestUtils.randomGeometry(false);
120122
GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder(fieldName(), shape);
121123
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.relation(null));
122124
assertEquals("No Shape Relation defined", e.getMessage());
@@ -159,25 +161,20 @@ public void testMustRewrite() throws IOException {
159161
() -> query.toQuery(createSearchExecutionContext()));
160162
assertEquals("query must be rewritten first", e.getMessage());
161163
QueryBuilder rewrite = rewriteAndFetch(query, createSearchExecutionContext());
162-
GeoShapeQueryBuilder geoShapeQueryBuilder = randomBoolean() ?
163-
new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn) :
164-
new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn.buildGeometry());
164+
GeoShapeQueryBuilder geoShapeQueryBuilder = new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn);
165165
geoShapeQueryBuilder.strategy(query.strategy());
166166
geoShapeQueryBuilder.relation(query.relation());
167167
assertEquals(geoShapeQueryBuilder, rewrite);
168168
}
169169

170-
public void testMultipleRewrite() throws IOException {
170+
public void testMultipleRewrite() {
171171
GeoShapeQueryBuilder shape = doCreateTestQueryBuilder(true);
172172
QueryBuilder builder = new BoolQueryBuilder()
173173
.should(shape)
174174
.should(shape);
175175

176176
builder = rewriteAndFetch(builder, createSearchExecutionContext());
177-
178-
GeoShapeQueryBuilder expectedShape = randomBoolean() ?
179-
new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn) :
180-
new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn.buildGeometry());
177+
GeoShapeQueryBuilder expectedShape = new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn);
181178
expectedShape.strategy(shape.strategy());
182179
expectedShape.relation(shape.relation());
183180
QueryBuilder expected = new BoolQueryBuilder()
@@ -187,30 +184,22 @@ public void testMultipleRewrite() throws IOException {
187184
}
188185

189186
public void testIgnoreUnmapped() throws IOException {
190-
ShapeType shapeType = ShapeType.randomType(random());
191-
ShapeBuilder<?, ?, ?> shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
192-
final GeoShapeQueryBuilder queryBuilder = randomBoolean() ?
193-
new GeoShapeQueryBuilder("unmapped", shape) :
194-
new GeoShapeQueryBuilder("unmapped", shape.buildGeometry());
187+
Geometry geometry = GeometryTestUtils.randomGeometry(false);
188+
final GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder("unmapped", geometry);
195189
queryBuilder.ignoreUnmapped(true);
196190
Query query = queryBuilder.toQuery(createSearchExecutionContext());
197191
assertThat(query, notNullValue());
198192
assertThat(query, instanceOf(MatchNoDocsQuery.class));
199193

200-
final GeoShapeQueryBuilder failingQueryBuilder = randomBoolean() ?
201-
new GeoShapeQueryBuilder("unmapped", shape) :
202-
new GeoShapeQueryBuilder("unmapped", shape.buildGeometry());
194+
final GeoShapeQueryBuilder failingQueryBuilder = new GeoShapeQueryBuilder("unmapped", geometry);
203195
failingQueryBuilder.ignoreUnmapped(false);
204196
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createSearchExecutionContext()));
205197
assertThat(e.getMessage(), containsString("failed to find type for field [unmapped]"));
206198
}
207199

208-
public void testWrongFieldType() throws IOException {
209-
ShapeType shapeType = ShapeType.randomType(random());
210-
ShapeBuilder<?, ?, ?> shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
211-
final GeoShapeQueryBuilder queryBuilder = randomBoolean() ?
212-
new GeoShapeQueryBuilder(TEXT_FIELD_NAME, shape) :
213-
new GeoShapeQueryBuilder(TEXT_FIELD_NAME, shape.buildGeometry());
200+
public void testWrongFieldType() {
201+
Geometry geometry = GeometryTestUtils.randomGeometry(false);
202+
final GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder(TEXT_FIELD_NAME, geometry);
214203
QueryShardException e = expectThrows(QueryShardException.class, () -> queryBuilder.toQuery(createSearchExecutionContext()));
215204
assertThat(e.getMessage(), containsString("Field [mapped_string] is of unsupported type [text] for [geo_shape] query"));
216205
}
@@ -230,4 +219,10 @@ protected QueryBuilder parseQuery(XContentParser parser) throws IOException {
230219
assertThat(query, instanceOf(GeoShapeQueryBuilder.class));
231220
return query;
232221
}
222+
223+
@Override
224+
protected Map<String, String> getObjectsHoldingArbitraryContent() {
225+
// shape field can accept any element but expects a type
226+
return Collections.singletonMap("shape", "Required [type]");
227+
}
233228
}

server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import java.util.EnumSet;
4646
import java.util.HashMap;
4747
import java.util.Map;
48-
import java.util.Set;
4948
import java.util.stream.Stream;
5049

5150
import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisQuery;
@@ -204,9 +203,9 @@ protected String[] shuffleProtectedFields() {
204203
}
205204

206205
@Override
207-
protected Set<String> getObjectsHoldingArbitraryContent() {
206+
protected Map<String, String> getObjectsHoldingArbitraryContent() {
208207
//doc contains arbitrary content, anything can be added to it and no exception will be thrown
209-
return Collections.singleton(MoreLikeThisQueryBuilder.DOC.getPreferredName());
208+
return Collections.singletonMap(MoreLikeThisQueryBuilder.DOC.getPreferredName(), null);
210209
}
211210

212211
@Override

0 commit comments

Comments
 (0)