@@ -837,8 +837,8 @@ public DeleteRequest documentDeleteRequest(String id, @Nullable String routing,
837837 });
838838 }
839839
840- public DeleteByQueryRequest documentDeleteByQueryRequest (Query query , Class <?> clazz , IndexCoordinates index ,
841- @ Nullable RefreshPolicy refreshPolicy ) {
840+ public DeleteByQueryRequest documentDeleteByQueryRequest (Query query , @ Nullable String routing , Class <?> clazz ,
841+ IndexCoordinates index , @ Nullable RefreshPolicy refreshPolicy ) {
842842
843843 Assert .notNull (query , "query must not be null" );
844844 Assert .notNull (index , "index must not be null" );
@@ -857,6 +857,8 @@ public DeleteByQueryRequest documentDeleteByQueryRequest(Query query, Class<?> c
857857
858858 if (query .getRoute () != null ) {
859859 b .routing (query .getRoute ());
860+ } else if (StringUtils .hasText (routing )) {
861+ b .routing (routing );
860862 }
861863
862864 return b ;
@@ -998,45 +1000,53 @@ public UpdateByQueryRequest documentUpdateByQueryRequest(UpdateQuery updateQuery
9981000
9991001 // region search
10001002
1001- public <T > SearchRequest searchRequest (Query query , @ Nullable Class <T > clazz , IndexCoordinates indexCoordinates ,
1002- boolean forCount ) {
1003- return searchRequest (query , clazz , indexCoordinates , forCount , false , null );
1003+ public <T > SearchRequest searchRequest (Query query , @ Nullable String routing , @ Nullable Class <T > clazz ,
1004+ IndexCoordinates indexCoordinates , boolean forCount ) {
1005+ return searchRequest (query , routing , clazz , indexCoordinates , forCount , false , null );
10041006 }
10051007
1006- public <T > SearchRequest searchRequest (Query query , @ Nullable Class <T > clazz , IndexCoordinates indexCoordinates ,
1007- boolean forCount , long scrollTimeInMillis ) {
1008- return searchRequest (query , clazz , indexCoordinates , forCount , true , scrollTimeInMillis );
1008+ public <T > SearchRequest searchRequest (Query query , @ Nullable String routing , @ Nullable Class <T > clazz ,
1009+ IndexCoordinates indexCoordinates , boolean forCount , long scrollTimeInMillis ) {
1010+ return searchRequest (query , routing , clazz , indexCoordinates , forCount , true , scrollTimeInMillis );
10091011 }
10101012
1011- public <T > SearchRequest searchRequest (Query query , @ Nullable Class <T > clazz , IndexCoordinates indexCoordinates ,
1012- boolean forCount , boolean forBatchedSearch ) {
1013- return searchRequest (query , clazz , indexCoordinates , forCount , forBatchedSearch , null );
1013+ public <T > SearchRequest searchRequest (Query query , @ Nullable String routing , @ Nullable Class <T > clazz ,
1014+ IndexCoordinates indexCoordinates , boolean forCount , boolean forBatchedSearch ) {
1015+ return searchRequest (query , routing , clazz , indexCoordinates , forCount , forBatchedSearch , null );
10141016 }
10151017
1016- public <T > SearchRequest searchRequest (Query query , @ Nullable Class <T > clazz , IndexCoordinates indexCoordinates ,
1017- boolean forCount , boolean forBatchedSearch , @ Nullable Long scrollTimeInMillis ) {
1018+ public <T > SearchRequest searchRequest (Query query , @ Nullable String routing , @ Nullable Class <T > clazz ,
1019+ IndexCoordinates indexCoordinates , boolean forCount , boolean forBatchedSearch ,
1020+ @ Nullable Long scrollTimeInMillis ) {
10181021
10191022 String [] indexNames = indexCoordinates .getIndexNames ();
10201023 Assert .notNull (query , "query must not be null" );
10211024 Assert .notNull (indexCoordinates , "indexCoordinates must not be null" );
10221025
10231026 elasticsearchConverter .updateQuery (query , clazz );
10241027 SearchRequest .Builder builder = new SearchRequest .Builder ();
1025- prepareSearchRequest (query , clazz , indexCoordinates , builder , forCount , forBatchedSearch );
1028+ prepareSearchRequest (query , routing , clazz , indexCoordinates , builder , forCount , forBatchedSearch );
10261029
10271030 if (scrollTimeInMillis != null ) {
10281031 builder .scroll (t -> t .time (scrollTimeInMillis + "ms" ));
10291032 }
10301033
10311034 builder .query (getQuery (query , clazz ));
10321035
1036+ if (StringUtils .hasText (query .getRoute ())) {
1037+ builder .routing (query .getRoute ());
1038+ }
1039+ if (StringUtils .hasText (routing )) {
1040+ builder .routing (routing );
1041+ }
1042+
10331043 addFilter (query , builder );
10341044
10351045 return builder .build ();
10361046 }
10371047
10381048 public MsearchRequest searchMsearchRequest (
1039- List <ElasticsearchTemplate .MultiSearchQueryParameter > multiSearchQueryParameters ) {
1049+ List <ElasticsearchTemplate .MultiSearchQueryParameter > multiSearchQueryParameters , @ Nullable String routing ) {
10401050
10411051 // basically the same stuff as in prepareSearchRequest, but the new Elasticsearch has different builders for a
10421052 // normal search and msearch
@@ -1049,11 +1059,16 @@ public MsearchRequest searchMsearchRequest(
10491059 .header (h -> {
10501060 h //
10511061 .index (Arrays .asList (param .index ().getIndexNames ())) //
1052- .routing (query .getRoute ()) //
10531062 .searchType (searchType (query .getSearchType ())) //
10541063 .requestCache (query .getRequestCache ()) //
10551064 ;
10561065
1066+ if (StringUtils .hasText (query .getRoute ())) {
1067+ h .routing (query .getRoute ());
1068+ } else if (StringUtils .hasText (routing )) {
1069+ h .routing (routing );
1070+ }
1071+
10571072 if (query .getPreference () != null ) {
10581073 h .preference (query .getPreference ());
10591074 }
@@ -1156,8 +1171,8 @@ public MsearchRequest searchMsearchRequest(
11561171 });
11571172 }
11581173
1159- private <T > void prepareSearchRequest (Query query , @ Nullable Class <T > clazz , IndexCoordinates indexCoordinates ,
1160- SearchRequest .Builder builder , boolean forCount , boolean forBatchedSearch ) {
1174+ private <T > void prepareSearchRequest (Query query , @ Nullable String routing , @ Nullable Class <T > clazz ,
1175+ IndexCoordinates indexCoordinates , SearchRequest .Builder builder , boolean forCount , boolean forBatchedSearch ) {
11611176
11621177 String [] indexNames = indexCoordinates .getIndexNames ();
11631178
@@ -1190,6 +1205,8 @@ private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, Ind
11901205
11911206 if (query .getRoute () != null ) {
11921207 builder .routing (query .getRoute ());
1208+ } else if (StringUtils .hasText (routing )) {
1209+ builder .routing (routing );
11931210 }
11941211
11951212 if (query .getPreference () != null ) {
@@ -1559,7 +1576,8 @@ public ClosePointInTimeRequest searchClosePointInTime(String pit) {
15591576 return ClosePointInTimeRequest .of (cpit -> cpit .id (pit ));
15601577 }
15611578
1562- public SearchTemplateRequest searchTemplate (SearchTemplateQuery query , IndexCoordinates index ) {
1579+ public SearchTemplateRequest searchTemplate (SearchTemplateQuery query , @ Nullable String routing ,
1580+ IndexCoordinates index ) {
15631581
15641582 Assert .notNull (query , "query must not be null" );
15651583
@@ -1570,10 +1588,15 @@ public SearchTemplateRequest searchTemplate(SearchTemplateQuery query, IndexCoor
15701588 .id (query .getId ()) //
15711589 .index (Arrays .asList (index .getIndexNames ())) //
15721590 .preference (query .getPreference ()) //
1573- .routing (query .getRoute ()) //
15741591 .searchType (searchType (query .getSearchType ())).source (query .getSource ()) //
15751592 ;
15761593
1594+ if (query .getRoute () != null ) {
1595+ builder .routing (query .getRoute ());
1596+ } else if (StringUtils .hasText (routing )) {
1597+ builder .routing (routing );
1598+ }
1599+
15771600 var expandWildcards = query .getExpandWildcards ();
15781601 if (!expandWildcards .isEmpty ()) {
15791602 builder .expandWildcards (expandWildcards (expandWildcards ));
0 commit comments