29
29
import org .apache .lucene .spatial .query .SpatialArgs ;
30
30
import org .apache .lucene .spatial .query .SpatialOperation ;
31
31
import org .apache .lucene .util .SetOnce ;
32
+ import org .elasticsearch .Version ;
32
33
import org .elasticsearch .action .ActionListener ;
33
34
import org .elasticsearch .action .get .GetRequest ;
34
35
import org .elasticsearch .action .get .GetResponse ;
@@ -77,6 +78,7 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
77
78
private static final ParseField SHAPE_TYPE_FIELD = new ParseField ("type" );
78
79
private static final ParseField SHAPE_INDEX_FIELD = new ParseField ("index" );
79
80
private static final ParseField SHAPE_PATH_FIELD = new ParseField ("path" );
81
+ private static final ParseField SHAPE_ROUTING_FIELD = new ParseField ("routing" );
80
82
private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField ("ignore_unmapped" );
81
83
82
84
private final String fieldName ;
@@ -89,8 +91,10 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
89
91
private final String indexedShapeId ;
90
92
private final String indexedShapeType ;
91
93
94
+
92
95
private String indexedShapeIndex = DEFAULT_SHAPE_INDEX_NAME ;
93
96
private String indexedShapePath = DEFAULT_SHAPE_FIELD_NAME ;
97
+ private String indexedShapeRouting ;
94
98
95
99
private ShapeRelation relation = DEFAULT_SHAPE_RELATION ;
96
100
@@ -166,6 +170,11 @@ public GeoShapeQueryBuilder(StreamInput in) throws IOException {
166
170
indexedShapeType = in .readOptionalString ();
167
171
indexedShapeIndex = in .readOptionalString ();
168
172
indexedShapePath = in .readOptionalString ();
173
+ if (in .getVersion ().onOrAfter (Version .V_6_4_0 )) {
174
+ indexedShapeRouting = in .readOptionalString ();
175
+ } else {
176
+ indexedShapeRouting = null ;
177
+ }
169
178
}
170
179
relation = ShapeRelation .readFromStream (in );
171
180
strategy = in .readOptionalWriteable (SpatialStrategy ::readFromStream );
@@ -188,6 +197,11 @@ protected void doWriteTo(StreamOutput out) throws IOException {
188
197
out .writeOptionalString (indexedShapeType );
189
198
out .writeOptionalString (indexedShapeIndex );
190
199
out .writeOptionalString (indexedShapePath );
200
+ if (out .getVersion ().onOrAfter (Version .V_6_4_0 )) {
201
+ out .writeOptionalString (indexedShapeRouting );
202
+ } else if (indexedShapeRouting != null ) {
203
+ throw new IllegalStateException ("indexed shape routing cannot be serialized to older nodes" );
204
+ }
191
205
}
192
206
relation .writeTo (out );
193
207
out .writeOptionalWriteable (strategy );
@@ -285,6 +299,26 @@ public String indexedShapePath() {
285
299
return indexedShapePath ;
286
300
}
287
301
302
+ /**
303
+ * Sets the optional routing to the indexed Shape that will be used in the query
304
+ *
305
+ * @param indexedShapeRouting indexed shape routing
306
+ * @return this
307
+ */
308
+ public GeoShapeQueryBuilder indexedShapeRouting (String indexedShapeRouting ) {
309
+ this .indexedShapeRouting = indexedShapeRouting ;
310
+ return this ;
311
+ }
312
+
313
+
314
+ /**
315
+ * @return the optional routing to the indexed Shape that will be used in the
316
+ * Query
317
+ */
318
+ public String indexedShapeRouting () {
319
+ return indexedShapeRouting ;
320
+ }
321
+
288
322
/**
289
323
* Sets the relation of query shape and indexed shape.
290
324
*
@@ -473,6 +507,9 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
473
507
if (indexedShapePath != null ) {
474
508
builder .field (SHAPE_PATH_FIELD .getPreferredName (), indexedShapePath );
475
509
}
510
+ if (indexedShapeRouting != null ) {
511
+ builder .field (SHAPE_ROUTING_FIELD .getPreferredName (), indexedShapeRouting );
512
+ }
476
513
builder .endObject ();
477
514
}
478
515
@@ -498,6 +535,7 @@ public static GeoShapeQueryBuilder fromXContent(XContentParser parser) throws IO
498
535
String type = null ;
499
536
String index = null ;
500
537
String shapePath = null ;
538
+ String shapeRouting = null ;
501
539
502
540
XContentParser .Token token ;
503
541
String currentFieldName = null ;
@@ -544,6 +582,8 @@ public static GeoShapeQueryBuilder fromXContent(XContentParser parser) throws IO
544
582
index = parser .text ();
545
583
} else if (SHAPE_PATH_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
546
584
shapePath = parser .text ();
585
+ } else if (SHAPE_ROUTING_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
586
+ shapeRouting = parser .text ();
547
587
}
548
588
} else {
549
589
throw new ParsingException (parser .getTokenLocation (), "[" + GeoShapeQueryBuilder .NAME +
@@ -581,6 +621,9 @@ public static GeoShapeQueryBuilder fromXContent(XContentParser parser) throws IO
581
621
if (shapePath != null ) {
582
622
builder .indexedShapePath (shapePath );
583
623
}
624
+ if (shapeRouting != null ) {
625
+ builder .indexedShapeRouting (shapeRouting );
626
+ }
584
627
if (shapeRelation != null ) {
585
628
builder .relation (shapeRelation );
586
629
}
@@ -602,6 +645,7 @@ protected boolean doEquals(GeoShapeQueryBuilder other) {
602
645
&& Objects .equals (indexedShapeIndex , other .indexedShapeIndex )
603
646
&& Objects .equals (indexedShapePath , other .indexedShapePath )
604
647
&& Objects .equals (indexedShapeType , other .indexedShapeType )
648
+ && Objects .equals (indexedShapeRouting , other .indexedShapeRouting )
605
649
&& Objects .equals (relation , other .relation )
606
650
&& Objects .equals (shape , other .shape )
607
651
&& Objects .equals (supplier , other .supplier )
@@ -612,7 +656,7 @@ protected boolean doEquals(GeoShapeQueryBuilder other) {
612
656
@ Override
613
657
protected int doHashCode () {
614
658
return Objects .hash (fieldName , indexedShapeId , indexedShapeIndex ,
615
- indexedShapePath , indexedShapeType , relation , shape , strategy , ignoreUnmapped , supplier );
659
+ indexedShapePath , indexedShapeType , indexedShapeRouting , relation , shape , strategy , ignoreUnmapped , supplier );
616
660
}
617
661
618
662
@ Override
@@ -629,6 +673,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
629
673
SetOnce <ShapeBuilder > supplier = new SetOnce <>();
630
674
queryRewriteContext .registerAsyncAction ((client , listener ) -> {
631
675
GetRequest getRequest = new GetRequest (indexedShapeIndex , indexedShapeType , indexedShapeId );
676
+ getRequest .routing (indexedShapeRouting );
632
677
fetch (client , getRequest , indexedShapePath , ActionListener .wrap (builder -> {
633
678
supplier .set (builder );
634
679
listener .onResponse (null );
0 commit comments