Skip to content

Commit 8b14d89

Browse files
Address review comments
1 parent 121d0f6 commit 8b14d89

File tree

3 files changed

+55
-61
lines changed

3 files changed

+55
-61
lines changed

server/src/main/java/org/elasticsearch/action/search/SearchRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ public String getProjectRouting() {
175175
}
176176

177177
public void setProjectRouting(@Nullable String projectRouting) {
178+
if (this.projectRouting != null) {
179+
throw new IllegalArgumentException("project_routing is already set to [" + this.projectRouting + "]");
180+
}
181+
178182
this.projectRouting = projectRouting;
179183
}
180184

server/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ public static void parseSearchRequest(
202202
searchRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
203203
if (requestContentParser != null) {
204204
if (searchUsageHolder == null) {
205-
searchRequest.source().parseXContent(requestContentParser, true, clusterSupportsFeature);
205+
searchRequest.source().parseXContent(searchRequest, requestContentParser, true, clusterSupportsFeature);
206206
} else {
207-
searchRequest.source().parseXContent(requestContentParser, true, searchUsageHolder, clusterSupportsFeature);
207+
searchRequest.source().parseXContent(searchRequest, requestContentParser, true, searchUsageHolder, clusterSupportsFeature);
208208
}
209209
}
210210

@@ -250,7 +250,7 @@ public static void parseSearchRequest(
250250
}
251251
searchRequest.indicesOptions(indicesOptions);
252252

253-
validateSearchRequest(request, searchRequest, crossProjectEnabled);
253+
validateSearchRequest(request, searchRequest);
254254

255255
if (searchRequest.pointInTimeBuilder() != null) {
256256
preparePointInTime(searchRequest, request);
@@ -410,50 +410,10 @@ static void preparePointInTime(SearchRequest request, RestRequest restRequest) {
410410
* might modify the search request to align certain parameters.
411411
*/
412412
public static void validateSearchRequest(RestRequest restRequest, SearchRequest searchRequest) {
413-
validateSearchRequest(restRequest, searchRequest, false);
414-
}
415-
416-
private static void validateSearchRequest(RestRequest restRequest, SearchRequest searchRequest, boolean crossProjectEnabled) {
417413
checkRestTotalHits(restRequest, searchRequest);
418414
checkSearchType(restRequest, searchRequest);
419415
// ensures that the rest param is consumed
420416
restRequest.paramAsBoolean(INCLUDE_NAMED_QUERIES_SCORE_PARAM, false);
421-
checkProjectRouting(searchRequest, crossProjectEnabled);
422-
}
423-
424-
private static void checkProjectRouting(SearchRequest searchRequest, boolean crossProjectEnabled) {
425-
/*
426-
* There are 2 ways of specifying project_routing:
427-
* - as a query parameter: /_search?project_routing=..., and,
428-
* - within the request's body.
429-
*
430-
* Because we do not have access to `IndicesRequest/SearchRequest` from `SearchSourceBuilder`, and, project_routing
431-
* can be potentially specified in 2 different places, we need to explicitly check this scenario.
432-
*/
433-
if (searchRequest.source() == null) {
434-
return;
435-
}
436-
437-
String projectRoutingInBody = searchRequest.source().projectRouting();
438-
// If it's null, either the query parameter is also null or it isn't. Either way, we're fine with it.
439-
if (projectRoutingInBody != null) {
440-
if (crossProjectEnabled == false) {
441-
throw new IllegalArgumentException("project_routing is allowed only when CPS is enabled and the endpoint supports CPS");
442-
}
443-
444-
// Query parameter was also set. This is not allowed, irrespective of the values.
445-
if (searchRequest.getProjectRouting() != null) {
446-
throw new IllegalArgumentException(
447-
"project_routing is specified in both the places: as query parameter and in the request's body"
448-
);
449-
}
450-
451-
/*
452-
* Bring forward the project_routing value so that TransportSearchAction can pick it up. Although TSA can pick it up
453-
* from `SearchSourceBuilder`, let's use `IndicesRequest#getProjectRouting()` since that's the intended way.
454-
*/
455-
searchRequest.setProjectRouting(projectRoutingInBody);
456-
}
457417
}
458418

459419
/**

server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ public static HighlightBuilder highlight() {
213213

214214
private boolean skipInnerHits = false;
215215

216-
private String projectRouting;
217-
218216
/**
219217
* Constructs a new search source builder.
220218
*/
@@ -612,19 +610,6 @@ public TimeValue timeout() {
612610
return timeout;
613611
}
614612

615-
public String projectRouting() {
616-
return projectRouting;
617-
}
618-
619-
public SearchSourceBuilder projectRouting(String projectRouting) {
620-
if (this.projectRouting != null) {
621-
throw new IllegalArgumentException("project_routing is already set");
622-
}
623-
624-
this.projectRouting = projectRouting;
625-
return this;
626-
}
627-
628613
/**
629614
* An optional terminate_after to terminate the search after collecting
630615
* <code>terminateAfter</code> documents
@@ -1342,6 +1327,26 @@ private SearchSourceBuilder shallowCopy(
13421327
return rewrittenBuilder;
13431328
}
13441329

1330+
/**
1331+
* Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent.
1332+
*
1333+
* @param searchRequest The SearchRequest object that's representing the request we're parsing which shall receive
1334+
* the parsed info.
1335+
* @param parser The xContent parser.
1336+
* @param checkTrailingTokens If true throws a parsing exception when extra tokens are found after the main object.
1337+
* @param searchUsageHolder holder for the search usage statistics
1338+
* @param clusterSupportsFeature used to check if certain features are available on this cluster
1339+
*/
1340+
public SearchSourceBuilder parseXContent(
1341+
SearchRequest searchRequest,
1342+
XContentParser parser,
1343+
boolean checkTrailingTokens,
1344+
SearchUsageHolder searchUsageHolder,
1345+
Predicate<NodeFeature> clusterSupportsFeature
1346+
) throws IOException {
1347+
return parseXContent(searchRequest, parser, checkTrailingTokens, searchUsageHolder::updateUsage, clusterSupportsFeature);
1348+
}
1349+
13451350
/**
13461351
* Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent.
13471352
*
@@ -1356,7 +1361,27 @@ public SearchSourceBuilder parseXContent(
13561361
SearchUsageHolder searchUsageHolder,
13571362
Predicate<NodeFeature> clusterSupportsFeature
13581363
) throws IOException {
1359-
return parseXContent(parser, checkTrailingTokens, searchUsageHolder::updateUsage, clusterSupportsFeature);
1364+
return parseXContent(null, parser, checkTrailingTokens, searchUsageHolder::updateUsage, clusterSupportsFeature);
1365+
}
1366+
1367+
/**
1368+
* Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent.
1369+
* This variant does not record search features usage. Most times the variant that accepts a {@link SearchUsageHolder} and records
1370+
* usage stats into it is the one to use.
1371+
*
1372+
* @param searchRequest The SearchRequest object that's representing the request we're parsing which shall receive
1373+
* the parsed info.
1374+
* @param parser The xContent parser.
1375+
* @param checkTrailingTokens If true throws a parsing exception when extra tokens are found after the main object.
1376+
* @param clusterSupportsFeature used to check if certain features are available on this cluster
1377+
*/
1378+
public SearchSourceBuilder parseXContent(
1379+
SearchRequest searchRequest,
1380+
XContentParser parser,
1381+
boolean checkTrailingTokens,
1382+
Predicate<NodeFeature> clusterSupportsFeature
1383+
) throws IOException {
1384+
return parseXContent(searchRequest, parser, checkTrailingTokens, s -> {}, clusterSupportsFeature);
13601385
}
13611386

13621387
/**
@@ -1373,10 +1398,11 @@ public SearchSourceBuilder parseXContent(
13731398
boolean checkTrailingTokens,
13741399
Predicate<NodeFeature> clusterSupportsFeature
13751400
) throws IOException {
1376-
return parseXContent(parser, checkTrailingTokens, s -> {}, clusterSupportsFeature);
1401+
return parseXContent(null, parser, checkTrailingTokens, s -> {}, clusterSupportsFeature);
13771402
}
13781403

13791404
private SearchSourceBuilder parseXContent(
1405+
SearchRequest searchRequest,
13801406
XContentParser parser,
13811407
boolean checkTrailingTokens,
13821408
Consumer<SearchUsage> searchUsageConsumer,
@@ -1399,7 +1425,11 @@ private SearchSourceBuilder parseXContent(
13991425
currentFieldName = parser.currentName();
14001426
} else if (token.isValue()) {
14011427
if (PROJECT_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
1402-
projectRouting(parser.text());
1428+
/*
1429+
* If project_routing was specified as a query parameter too, setProjectRouting() will throw
1430+
* an error to prevent setting twice or overwriting previously set value.
1431+
*/
1432+
searchRequest.setProjectRouting(parser.text());
14031433
} else if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
14041434
from(parser.intValue());
14051435
} else if (SIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {

0 commit comments

Comments
 (0)