17
17
import org .elasticsearch .common .Strings ;
18
18
import org .elasticsearch .common .bytes .BytesArray ;
19
19
import org .elasticsearch .common .geo .builders .EnvelopeBuilder ;
20
- import org .elasticsearch .common .geo .builders .ShapeBuilder ;
21
20
import org .elasticsearch .common .io .stream .BytesStreamOutput ;
22
21
import org .elasticsearch .common .xcontent .XContentBuilder ;
23
22
import org .elasticsearch .common .xcontent .XContentFactory ;
24
23
import org .elasticsearch .common .xcontent .XContentParser ;
25
24
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 ;
26
28
import org .elasticsearch .index .get .GetResult ;
27
29
import org .elasticsearch .test .AbstractQueryTestCase ;
28
- import org .elasticsearch .test .geo .RandomShapeGenerator ;
29
- import org .elasticsearch .test .geo .RandomShapeGenerator .ShapeType ;
30
30
import org .junit .After ;
31
31
import org .locationtech .jts .geom .Coordinate ;
32
32
33
33
import java .io .IOException ;
34
+ import java .util .Collections ;
35
+ import java .util .Map ;
34
36
35
37
import static org .hamcrest .CoreMatchers .instanceOf ;
36
38
import static org .hamcrest .CoreMatchers .notNullValue ;
@@ -44,7 +46,7 @@ public abstract class GeoShapeQueryBuilderTests extends AbstractQueryTestCase<Ge
44
46
protected static String indexedShapePath ;
45
47
protected static String indexedShapeIndex ;
46
48
protected static String indexedShapeRouting ;
47
- protected static ShapeBuilder <?, ?, ?> indexedShapeToReturn ;
49
+ protected static Geometry indexedShapeToReturn ;
48
50
49
51
protected abstract String fieldName ();
50
52
@@ -70,7 +72,7 @@ protected GetResponse executeGet(GetRequest getRequest) {
70
72
try {
71
73
XContentBuilder builder = XContentFactory .jsonBuilder ().prettyPrint ();
72
74
builder .startObject ();
73
- builder .field (expectedShapePath , indexedShapeToReturn );
75
+ builder .field (expectedShapePath , WellKnownText . toWKT ( indexedShapeToReturn ) );
74
76
builder .field (randomAlphaOfLengthBetween (10 , 20 ), "something" );
75
77
builder .endObject ();
76
78
json = Strings .toString (builder );
@@ -100,23 +102,23 @@ protected void doAssertLuceneQuery(GeoShapeQueryBuilder queryBuilder, Query quer
100
102
}
101
103
102
104
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 ));
105
107
assertEquals ("fieldName is required" , e .getMessage ());
106
108
}
107
109
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 ));
110
112
}
111
113
112
- public void testNoIndexedShape () throws IOException {
114
+ public void testNoIndexedShape () {
113
115
IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
114
116
() -> new GeoShapeQueryBuilder (fieldName (), null , null ));
115
117
assertEquals ("either shape or indexedShapeId is required" , e .getMessage ());
116
118
}
117
119
118
- public void testNoRelation () throws IOException {
119
- ShapeBuilder <?, ?, ?> shape = RandomShapeGenerator . createShapeWithin ( random (), null );
120
+ public void testNoRelation () {
121
+ Geometry shape = GeometryTestUtils . randomGeometry ( false );
120
122
GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder (fieldName (), shape );
121
123
IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> builder .relation (null ));
122
124
assertEquals ("No Shape Relation defined" , e .getMessage ());
@@ -159,25 +161,20 @@ public void testMustRewrite() throws IOException {
159
161
() -> query .toQuery (createSearchExecutionContext ()));
160
162
assertEquals ("query must be rewritten first" , e .getMessage ());
161
163
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 );
165
165
geoShapeQueryBuilder .strategy (query .strategy ());
166
166
geoShapeQueryBuilder .relation (query .relation ());
167
167
assertEquals (geoShapeQueryBuilder , rewrite );
168
168
}
169
169
170
- public void testMultipleRewrite () throws IOException {
170
+ public void testMultipleRewrite () {
171
171
GeoShapeQueryBuilder shape = doCreateTestQueryBuilder (true );
172
172
QueryBuilder builder = new BoolQueryBuilder ()
173
173
.should (shape )
174
174
.should (shape );
175
175
176
176
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 );
181
178
expectedShape .strategy (shape .strategy ());
182
179
expectedShape .relation (shape .relation ());
183
180
QueryBuilder expected = new BoolQueryBuilder ()
@@ -187,30 +184,22 @@ public void testMultipleRewrite() throws IOException {
187
184
}
188
185
189
186
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 );
195
189
queryBuilder .ignoreUnmapped (true );
196
190
Query query = queryBuilder .toQuery (createSearchExecutionContext ());
197
191
assertThat (query , notNullValue ());
198
192
assertThat (query , instanceOf (MatchNoDocsQuery .class ));
199
193
200
- final GeoShapeQueryBuilder failingQueryBuilder = randomBoolean () ?
201
- new GeoShapeQueryBuilder ("unmapped" , shape ) :
202
- new GeoShapeQueryBuilder ("unmapped" , shape .buildGeometry ());
194
+ final GeoShapeQueryBuilder failingQueryBuilder = new GeoShapeQueryBuilder ("unmapped" , geometry );
203
195
failingQueryBuilder .ignoreUnmapped (false );
204
196
QueryShardException e = expectThrows (QueryShardException .class , () -> failingQueryBuilder .toQuery (createSearchExecutionContext ()));
205
197
assertThat (e .getMessage (), containsString ("failed to find type for field [unmapped]" ));
206
198
}
207
199
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 );
214
203
QueryShardException e = expectThrows (QueryShardException .class , () -> queryBuilder .toQuery (createSearchExecutionContext ()));
215
204
assertThat (e .getMessage (), containsString ("Field [mapped_string] is of unsupported type [text] for [geo_shape] query" ));
216
205
}
@@ -230,4 +219,10 @@ protected QueryBuilder parseQuery(XContentParser parser) throws IOException {
230
219
assertThat (query , instanceOf (GeoShapeQueryBuilder .class ));
231
220
return query ;
232
221
}
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
+ }
233
228
}
0 commit comments