15
15
import org .apache .lucene .search .MatchNoDocsQuery ;
16
16
import org .apache .lucene .search .Query ;
17
17
import org .elasticsearch .Version ;
18
- import org .elasticsearch .common .geo .GeoShapeType ;
19
18
import org .elasticsearch .common .geo .ShapeRelation ;
20
19
import org .elasticsearch .geometry .Circle ;
21
20
import org .elasticsearch .geometry .Geometry ;
33
32
import org .elasticsearch .index .mapper .MappedFieldType ;
34
33
import org .elasticsearch .index .query .QueryShardContext ;
35
34
import org .elasticsearch .index .query .QueryShardException ;
35
+ import org .elasticsearch .xpack .spatial .index .mapper .ShapeFieldMapper ;
36
36
37
37
import static org .elasticsearch .xpack .spatial .index .mapper .ShapeIndexer .toLucenePolygon ;
38
38
39
39
public class ShapeQueryProcessor implements AbstractGeometryFieldMapper .QueryProcessor {
40
40
41
41
@ Override
42
42
public Query process (Geometry shape , String fieldName , ShapeRelation relation , QueryShardContext context ) {
43
+ validateIsShapeFieldType (fieldName , context );
43
44
if (shape == null ) {
44
45
return new MatchNoDocsQuery ();
45
46
}
@@ -52,15 +53,21 @@ public Query process(Geometry shape, String fieldName, ShapeRelation relation, Q
52
53
return new ConstantScoreQuery (shape .visit (new ShapeVisitor (context , fieldName , relation )));
53
54
}
54
55
56
+ private void validateIsShapeFieldType (String fieldName , QueryShardContext context ) {
57
+ MappedFieldType fieldType = context .fieldMapper (fieldName );
58
+ if (fieldType instanceof ShapeFieldMapper .ShapeFieldType == false ) {
59
+ throw new QueryShardException (context , "Expected " + ShapeFieldMapper .CONTENT_TYPE
60
+ + " field type for Field [" + fieldName + "] but found " + fieldType .typeName ());
61
+ }
62
+ }
63
+
55
64
private class ShapeVisitor implements GeometryVisitor <Query , RuntimeException > {
56
65
QueryShardContext context ;
57
- MappedFieldType fieldType ;
58
66
String fieldName ;
59
67
ShapeRelation relation ;
60
68
61
69
ShapeVisitor (QueryShardContext context , String fieldName , ShapeRelation relation ) {
62
70
this .context = context ;
63
- this .fieldType = context .fieldMapper (fieldName );
64
71
this .fieldName = fieldName ;
65
72
this .relation = relation ;
66
73
}
@@ -87,13 +94,7 @@ private void visit(BooleanQuery.Builder bqb, GeometryCollection<?> collection) {
87
94
occur = BooleanClause .Occur .SHOULD ;
88
95
}
89
96
for (Geometry shape : collection ) {
90
- if (shape instanceof MultiPoint ) {
91
- // Flatten multipoints
92
- // We do not support multi-point queries?
93
- visit (bqb , (GeometryCollection <?>) shape );
94
- } else {
95
- bqb .add (shape .visit (this ), occur );
96
- }
97
+ bqb .add (shape .visit (this ), occur );
97
98
}
98
99
}
99
100
@@ -120,8 +121,11 @@ public Query visit(MultiLine multiLine) {
120
121
121
122
@ Override
122
123
public Query visit (MultiPoint multiPoint ) {
123
- throw new QueryShardException (context , "Field [" + fieldName + "] does not support " + GeoShapeType .MULTIPOINT +
124
- " queries" );
124
+ float [][] points = new float [multiPoint .size ()][2 ];
125
+ for (int i = 0 ; i < multiPoint .size (); i ++) {
126
+ points [i ] = new float [] {(float ) multiPoint .get (i ).getX (), (float ) multiPoint .get (i ).getY ()};
127
+ }
128
+ return XYShape .newPointQuery (fieldName , relation .getLuceneRelation (), points );
125
129
}
126
130
127
131
@ Override
@@ -145,8 +149,8 @@ public Query visit(Point point) {
145
149
// intersects is more efficient.
146
150
luceneRelation = ShapeField .QueryRelation .INTERSECTS ;
147
151
}
148
- return XYShape . newBoxQuery ( fieldName , luceneRelation ,
149
- ( float ) point . getX (), ( float ) point . getX (), ( float ) point . getY (), ( float ) point . getY () );
152
+ float [][] pointArray = new float [][] {{( float ) point . getX (), ( float ) point . getY ()}};
153
+ return XYShape . newPointQuery ( fieldName , luceneRelation , pointArray );
150
154
}
151
155
152
156
@ Override
0 commit comments