-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add support for project_routing for _search and _async_search
#137566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
3e94dc8
6ac9d8c
2d0f4c6
c09e00a
deb7b49
692c5a2
bad85a6
922cb53
97c184b
121d0f6
8b14d89
65a978c
301d2a0
a3dc375
0c7ee5d
888ed14
e0681d3
58f66d4
69155f8
2794828
7e8e30f
b1e06ef
52f5e32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -620,7 +620,8 @@ public void onFailure(Exception e) { | |
| }), | ||
| forceConnectTimeoutSecs, | ||
| resolvesCrossProject, | ||
| rewritten.getResolvedIndexExpressions() | ||
| rewritten.getResolvedIndexExpressions(), | ||
| rewritten.getProjectRouting() | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -1065,7 +1066,8 @@ static void collectSearchShards( | |
| ActionListener<Map<String, SearchShardsResponse>> listener, | ||
| TimeValue forceConnectTimeoutSecs, | ||
| boolean resolvesCrossProject, | ||
| ResolvedIndexExpressions originResolvedIdxExpressions | ||
| ResolvedIndexExpressions originResolvedIdxExpressions, | ||
| String projectRouting | ||
| ) { | ||
| RemoteClusterService remoteClusterService = transportService.getRemoteClusterService(); | ||
| final CountDown responsesCountDown = new CountDown(remoteIndicesByCluster.size()); | ||
|
|
@@ -1104,7 +1106,7 @@ Map<String, SearchShardsResponse> createFinalResponse() { | |
| // We do not use the relaxed index options here when validating indices' existence. | ||
| ElasticsearchException validationEx = CrossProjectIndexResolutionValidator.validate( | ||
| originalIdxOpts, | ||
| null, | ||
| projectRouting, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @piergm - Can you help me understand why we want projectRouting to be passed into That doesn't seem right to me, but maybe I'm missing a use case here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Consider these indices: Here's how it works, using Search as an example: the SAF uses project routing to resolve the projects that are in scope. Say, the routing is |
||
| originResolvedIdxExpressions, | ||
| resolvedIndexExpressions | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| import org.elasticsearch.rest.action.RestRefCountedChunkedToXContentListener; | ||
| import org.elasticsearch.search.SearchService; | ||
| import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
| import org.elasticsearch.search.crossproject.CrossProjectModeDecider; | ||
| import org.elasticsearch.search.fetch.StoredFieldsContext; | ||
| import org.elasticsearch.search.fetch.subphase.FetchSourceContext; | ||
| import org.elasticsearch.search.internal.SearchContext; | ||
|
|
@@ -68,15 +69,13 @@ public class RestSearchAction extends BaseRestHandler { | |
| private final SearchUsageHolder searchUsageHolder; | ||
| private final Predicate<NodeFeature> clusterSupportsFeature; | ||
| private final Settings settings; | ||
|
|
||
| public RestSearchAction(SearchUsageHolder searchUsageHolder, Predicate<NodeFeature> clusterSupportsFeature) { | ||
| this(searchUsageHolder, clusterSupportsFeature, null); | ||
| } | ||
| private final CrossProjectModeDecider crossProjectModeDecider; | ||
|
|
||
| public RestSearchAction(SearchUsageHolder searchUsageHolder, Predicate<NodeFeature> clusterSupportsFeature, Settings settings) { | ||
| this.searchUsageHolder = searchUsageHolder; | ||
| this.clusterSupportsFeature = clusterSupportsFeature; | ||
| this.settings = settings; | ||
| this.crossProjectModeDecider = new CrossProjectModeDecider(settings); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -109,10 +108,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC | |
| // this might be set by old clients | ||
| request.param("min_compatible_shard_node"); | ||
|
|
||
| final boolean crossProjectEnabled = settings != null && settings.getAsBoolean("serverless.cross_project.enabled", false); | ||
| final boolean crossProjectEnabled = crossProjectModeDecider.crossProjectEnabled(); | ||
| if (crossProjectEnabled) { | ||
| // accept but drop project_routing param until fully supported | ||
| request.param("project_routing"); | ||
| searchRequest.setProjectRouting(request.param("project_routing")); | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -252,7 +250,7 @@ public static void parseSearchRequest( | |
| } | ||
| searchRequest.indicesOptions(indicesOptions); | ||
|
|
||
| validateSearchRequest(request, searchRequest); | ||
| validateSearchRequest(request, searchRequest, crossProjectEnabled); | ||
|
||
|
|
||
| if (searchRequest.pointInTimeBuilder() != null) { | ||
| preparePointInTime(searchRequest, request); | ||
|
|
@@ -412,10 +410,50 @@ static void preparePointInTime(SearchRequest request, RestRequest restRequest) { | |
| * might modify the search request to align certain parameters. | ||
| */ | ||
| public static void validateSearchRequest(RestRequest restRequest, SearchRequest searchRequest) { | ||
| validateSearchRequest(restRequest, searchRequest, false); | ||
| } | ||
|
|
||
| private static void validateSearchRequest(RestRequest restRequest, SearchRequest searchRequest, boolean crossProjectEnabled) { | ||
| checkRestTotalHits(restRequest, searchRequest); | ||
| checkSearchType(restRequest, searchRequest); | ||
| // ensures that the rest param is consumed | ||
| restRequest.paramAsBoolean(INCLUDE_NAMED_QUERIES_SCORE_PARAM, false); | ||
| checkProjectRouting(searchRequest, crossProjectEnabled); | ||
| } | ||
|
|
||
| private static void checkProjectRouting(SearchRequest searchRequest, boolean crossProjectEnabled) { | ||
| /* | ||
| * There are 2 ways of specifying project_routing: | ||
| * - as a query parameter: /_search?project_routing=..., and, | ||
| * - within the request's body. | ||
| * | ||
| * Because we do not have access to `IndicesRequest/SearchRequest` from `SearchSourceBuilder`, and, project_routing | ||
| * can be potentially specified in 2 different places, we need to explicitly check this scenario. | ||
| */ | ||
| if (searchRequest.source() == null) { | ||
| return; | ||
| } | ||
|
|
||
| String projectRoutingInBody = searchRequest.source().projectRouting(); | ||
| // If it's null, either the query parameter is also null or it isn't. Either way, we're fine with it. | ||
| if (projectRoutingInBody != null) { | ||
| if (crossProjectEnabled == false) { | ||
| throw new IllegalArgumentException("project_routing is allowed only when CPS is enabled and the endpoint supports CPS"); | ||
| } | ||
|
|
||
| // Query parameter was also set. This is not allowed, irrespective of the values. | ||
| if (searchRequest.getProjectRouting() != null) { | ||
| throw new IllegalArgumentException( | ||
| "project_routing is specified in both the places: as query parameter and in the request's body" | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
|
|
||
| /* | ||
| * Bring forward the project_routing value so that TransportSearchAction can pick it up. Although TSA can pick it up | ||
| * from `SearchSourceBuilder`, let's use `IndicesRequest#getProjectRouting()` since that's the intended way. | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| searchRequest.setProjectRouting(projectRoutingInBody); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,6 +129,7 @@ public final class SearchSourceBuilder implements Writeable, ToXContentObject, R | |
| public static final ParseField POINT_IN_TIME = new ParseField("pit"); | ||
| public static final ParseField RUNTIME_MAPPINGS_FIELD = new ParseField("runtime_mappings"); | ||
| public static final ParseField RETRIEVER = new ParseField("retriever"); | ||
| public static final ParseField PROJECT_ROUTING = new ParseField("project_routing"); | ||
|
|
||
| private static final boolean RANK_SUPPORTED = Booleans.parseBoolean(System.getProperty("es.search.rank_supported"), true); | ||
|
|
||
|
|
@@ -212,6 +213,8 @@ public static HighlightBuilder highlight() { | |
|
|
||
| private boolean skipInnerHits = false; | ||
|
|
||
| private String projectRouting; | ||
|
|
||
| /** | ||
| * Constructs a new search source builder. | ||
| */ | ||
|
|
@@ -609,6 +612,19 @@ public TimeValue timeout() { | |
| return timeout; | ||
| } | ||
|
|
||
| public String projectRouting() { | ||
| return projectRouting; | ||
| } | ||
|
|
||
| public SearchSourceBuilder projectRouting(String projectRouting) { | ||
| if (this.projectRouting != null) { | ||
| throw new IllegalArgumentException("project_routing is already set"); | ||
| } | ||
|
|
||
| this.projectRouting = projectRouting; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * An optional terminate_after to terminate the search after collecting | ||
| * <code>terminateAfter</code> documents | ||
|
|
@@ -1382,7 +1398,9 @@ private SearchSourceBuilder parseXContent( | |
| if (token == XContentParser.Token.FIELD_NAME) { | ||
| currentFieldName = parser.currentName(); | ||
| } else if (token.isValue()) { | ||
| if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { | ||
| if (PROJECT_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) { | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| projectRouting(parser.text()); | ||
|
||
| } else if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { | ||
| from(parser.intValue()); | ||
| } else if (SIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { | ||
| size(parser.intValue()); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.