2929import org .apache .lucene .spatial .query .SpatialArgs ;
3030import org .apache .lucene .spatial .query .SpatialOperation ;
3131import org .apache .lucene .util .SetOnce ;
32+ import org .elasticsearch .Version ;
3233import org .elasticsearch .action .ActionListener ;
3334import org .elasticsearch .action .get .GetRequest ;
3435import org .elasticsearch .action .get .GetResponse ;
@@ -77,6 +78,7 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
7778 private static final ParseField SHAPE_TYPE_FIELD = new ParseField ("type" );
7879 private static final ParseField SHAPE_INDEX_FIELD = new ParseField ("index" );
7980 private static final ParseField SHAPE_PATH_FIELD = new ParseField ("path" );
81+ private static final ParseField SHAPE_ROUTING_FIELD = new ParseField ("routing" );
8082 private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField ("ignore_unmapped" );
8183
8284 private final String fieldName ;
@@ -89,8 +91,10 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
8991 private final String indexedShapeId ;
9092 private final String indexedShapeType ;
9193
94+
9295 private String indexedShapeIndex = DEFAULT_SHAPE_INDEX_NAME ;
9396 private String indexedShapePath = DEFAULT_SHAPE_FIELD_NAME ;
97+ private String indexedShapeRouting ;
9498
9599 private ShapeRelation relation = DEFAULT_SHAPE_RELATION ;
96100
@@ -166,6 +170,11 @@ public GeoShapeQueryBuilder(StreamInput in) throws IOException {
166170 indexedShapeType = in .readOptionalString ();
167171 indexedShapeIndex = in .readOptionalString ();
168172 indexedShapePath = in .readOptionalString ();
173+ if (in .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
174+ indexedShapeRouting = in .readOptionalString ();
175+ } else {
176+ indexedShapeRouting = null ;
177+ }
169178 }
170179 relation = ShapeRelation .readFromStream (in );
171180 strategy = in .readOptionalWriteable (SpatialStrategy ::readFromStream );
@@ -188,6 +197,11 @@ protected void doWriteTo(StreamOutput out) throws IOException {
188197 out .writeOptionalString (indexedShapeType );
189198 out .writeOptionalString (indexedShapeIndex );
190199 out .writeOptionalString (indexedShapePath );
200+ if (out .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
201+ out .writeOptionalString (indexedShapeRouting );
202+ } else if (indexedShapeRouting != null ) {
203+ throw new IllegalStateException ("indexed shape routing cannot be serialized to older nodes" );
204+ }
191205 }
192206 relation .writeTo (out );
193207 out .writeOptionalWriteable (strategy );
@@ -285,6 +299,26 @@ public String indexedShapePath() {
285299 return indexedShapePath ;
286300 }
287301
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+
288322 /**
289323 * Sets the relation of query shape and indexed shape.
290324 *
@@ -473,6 +507,9 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
473507 if (indexedShapePath != null ) {
474508 builder .field (SHAPE_PATH_FIELD .getPreferredName (), indexedShapePath );
475509 }
510+ if (indexedShapeRouting != null ) {
511+ builder .field (SHAPE_ROUTING_FIELD .getPreferredName (), indexedShapeRouting );
512+ }
476513 builder .endObject ();
477514 }
478515
@@ -498,6 +535,7 @@ public static GeoShapeQueryBuilder fromXContent(XContentParser parser) throws IO
498535 String type = null ;
499536 String index = null ;
500537 String shapePath = null ;
538+ String shapeRouting = null ;
501539
502540 XContentParser .Token token ;
503541 String currentFieldName = null ;
@@ -544,6 +582,8 @@ public static GeoShapeQueryBuilder fromXContent(XContentParser parser) throws IO
544582 index = parser .text ();
545583 } else if (SHAPE_PATH_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
546584 shapePath = parser .text ();
585+ } else if (SHAPE_ROUTING_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
586+ shapeRouting = parser .text ();
547587 }
548588 } else {
549589 throw new ParsingException (parser .getTokenLocation (), "[" + GeoShapeQueryBuilder .NAME +
@@ -581,6 +621,9 @@ public static GeoShapeQueryBuilder fromXContent(XContentParser parser) throws IO
581621 if (shapePath != null ) {
582622 builder .indexedShapePath (shapePath );
583623 }
624+ if (shapeRouting != null ) {
625+ builder .indexedShapeRouting (shapeRouting );
626+ }
584627 if (shapeRelation != null ) {
585628 builder .relation (shapeRelation );
586629 }
@@ -602,6 +645,7 @@ protected boolean doEquals(GeoShapeQueryBuilder other) {
602645 && Objects .equals (indexedShapeIndex , other .indexedShapeIndex )
603646 && Objects .equals (indexedShapePath , other .indexedShapePath )
604647 && Objects .equals (indexedShapeType , other .indexedShapeType )
648+ && Objects .equals (indexedShapeRouting , other .indexedShapeRouting )
605649 && Objects .equals (relation , other .relation )
606650 && Objects .equals (shape , other .shape )
607651 && Objects .equals (supplier , other .supplier )
@@ -612,7 +656,7 @@ protected boolean doEquals(GeoShapeQueryBuilder other) {
612656 @ Override
613657 protected int doHashCode () {
614658 return Objects .hash (fieldName , indexedShapeId , indexedShapeIndex ,
615- indexedShapePath , indexedShapeType , relation , shape , strategy , ignoreUnmapped , supplier );
659+ indexedShapePath , indexedShapeType , indexedShapeRouting , relation , shape , strategy , ignoreUnmapped , supplier );
616660 }
617661
618662 @ Override
@@ -629,6 +673,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
629673 SetOnce <ShapeBuilder > supplier = new SetOnce <>();
630674 queryRewriteContext .registerAsyncAction ((client , listener ) -> {
631675 GetRequest getRequest = new GetRequest (indexedShapeIndex , indexedShapeType , indexedShapeId );
676+ getRequest .routing (indexedShapeRouting );
632677 fetch (client , getRequest , indexedShapePath , ActionListener .wrap (builder -> {
633678 supplier .set (builder );
634679 listener .onResponse (null );
0 commit comments