@@ -721,13 +721,7 @@ public void executeQueryPhase(ShardSearchRequest request, CancellableTask task,
721721 if (orig .canReturnNullResponseIfMatchNoDocs ()) {
722722 assert orig .scroll () == null ;
723723 ShardSearchRequest clone = new ShardSearchRequest (orig );
724- CanMatchContext canMatchContext = new CanMatchContext (
725- clone ,
726- indicesService ::indexServiceSafe ,
727- this ::findReaderContext ,
728- defaultKeepAlive ,
729- maxKeepAlive
730- );
724+ CanMatchContext canMatchContext = createCanMatchContext (clone );
731725 CanMatchShardResponse canMatchResp = canMatch (canMatchContext , false );
732726 if (canMatchResp .canMatch () == false ) {
733727 l .onResponse (QuerySearchResult .nullInstance ());
@@ -1942,8 +1936,10 @@ public void canMatch(CanMatchNodeRequest request, ActionListener<CanMatchNodeRes
19421936 final IndexService indexService = indicesService .indexServiceSafe (shardSearchRequest .shardId ().getIndex ());
19431937 final IndexShard indexShard = indexService .getShard (shardSearchRequest .shardId ().id ());
19441938 try {
1945- // TODO remove the exception handling as it's now in canMatch itself
1946- responses .add (new CanMatchNodeResponse .ResponseOrFailure (canMatch (shardSearchRequest )));
1939+ // TODO remove the exception handling as it's now in canMatch itself - the failure no longer needs to be serialized
1940+ CanMatchContext canMatchContext = createCanMatchContext (shardSearchRequest );
1941+ CanMatchShardResponse canMatchShardResponse = canMatch (canMatchContext , true );
1942+ responses .add (new CanMatchNodeResponse .ResponseOrFailure (canMatchShardResponse ));
19471943 indexShard .getSearchOperationListener ().onCanMatchPhase (System .nanoTime () - shardCanMatchStartTimeInNanos );
19481944 } catch (Exception e ) {
19491945 responses .add (new CanMatchNodeResponse .ResponseOrFailure (e ));
@@ -1958,16 +1954,14 @@ public void canMatch(CanMatchNodeRequest request, ActionListener<CanMatchNodeRes
19581954 * won't match any documents on the current shard. Exceptions are handled within the method, and never re-thrown.
19591955 */
19601956 public CanMatchShardResponse canMatch (ShardSearchRequest request ) {
1961- CanMatchContext canMatchContext = new CanMatchContext (
1962- request ,
1963- indicesService ::indexServiceSafe ,
1964- this ::findReaderContext ,
1965- defaultKeepAlive ,
1966- maxKeepAlive
1967- );
1957+ CanMatchContext canMatchContext = createCanMatchContext (request );
19681958 return canMatch (canMatchContext , true );
19691959 }
19701960
1961+ CanMatchContext createCanMatchContext (ShardSearchRequest request ) {
1962+ return new CanMatchContext (request , indicesService ::indexServiceSafe , this ::findReaderContext , defaultKeepAlive , maxKeepAlive );
1963+ }
1964+
19711965 static class CanMatchContext {
19721966 private final ShardSearchRequest request ;
19731967 private final Function <Index , IndexService > indexServiceLookup ;
@@ -1977,6 +1971,8 @@ static class CanMatchContext {
19771971
19781972 private IndexService indexService ;
19791973
1974+ private Long timeRangeFilterFromMillis ;
1975+
19801976 CanMatchContext (
19811977 ShardSearchRequest request ,
19821978 Function <Index , IndexService > indexServiceLookup ,
@@ -2024,12 +2020,21 @@ IndexService getIndexService() {
20242020 }
20252021 return this .indexService ;
20262022 }
2023+
2024+ void setTimeRangeFilterFromMillis (Long timeRangeFilterFromMillis ) {
2025+ this .timeRangeFilterFromMillis = timeRangeFilterFromMillis ;
2026+ }
2027+
2028+ Long getTimeRangeFilterFromMillis () {
2029+ return timeRangeFilterFromMillis ;
2030+ }
20272031 }
20282032
20292033 static CanMatchShardResponse canMatch (CanMatchContext canMatchContext , boolean checkRefreshPending ) {
20302034 assert canMatchContext .request .searchType () == SearchType .QUERY_THEN_FETCH
20312035 : "unexpected search type: " + canMatchContext .request .searchType ();
20322036 Releasable releasable = null ;
2037+ QueryRewriteContext queryRewriteContext = null ;
20332038 try {
20342039 IndexService indexService ;
20352040 final boolean hasRefreshPending ;
@@ -2042,7 +2047,7 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20422047 readerContext = canMatchContext .findReaderContext ();
20432048 releasable = readerContext .markAsUsed (canMatchContext .getKeepAlive ());
20442049 indexService = readerContext .indexService ();
2045- QueryRewriteContext queryRewriteContext = canMatchContext .getQueryRewriteContext (indexService );
2050+ queryRewriteContext = canMatchContext .getQueryRewriteContext (indexService );
20462051 if (queryStillMatchesAfterRewrite (canMatchContext .request , queryRewriteContext ) == false ) {
20472052 return new CanMatchShardResponse (false , null );
20482053 }
@@ -2051,10 +2056,8 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20512056 if (canMatchContext .request .readerId ().isRetryable () == false ) {
20522057 return new CanMatchShardResponse (true , null );
20532058 }
2054- if (queryStillMatchesAfterRewrite (
2055- canMatchContext .request ,
2056- canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ())
2057- ) == false ) {
2059+ queryRewriteContext = canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ());
2060+ if (queryStillMatchesAfterRewrite (canMatchContext .request , queryRewriteContext ) == false ) {
20582061 return new CanMatchShardResponse (false , null );
20592062 }
20602063 final Engine .SearcherSupplier searcherSupplier = canMatchContext .getShard ().acquireSearcherSupplier ();
@@ -2067,10 +2070,8 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20672070 }
20682071 canMatchSearcher = searcher ;
20692072 } else {
2070- if (queryStillMatchesAfterRewrite (
2071- canMatchContext .request ,
2072- canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ())
2073- ) == false ) {
2073+ queryRewriteContext = canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ());
2074+ if (queryStillMatchesAfterRewrite (canMatchContext .request , queryRewriteContext ) == false ) {
20742075 return new CanMatchShardResponse (false , null );
20752076 }
20762077 boolean needsWaitForRefresh = canMatchContext .request .waitForCheckpoint () != UNASSIGNED_SEQ_NO ;
@@ -2083,6 +2084,7 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20832084 }
20842085 try (canMatchSearcher ) {
20852086 SearchExecutionContext context = canMatchContext .getSearchExecutionContext (canMatchSearcher );
2087+ queryRewriteContext = context ;
20862088 final boolean canMatch = queryStillMatchesAfterRewrite (canMatchContext .request , context );
20872089 if (canMatch || hasRefreshPending ) {
20882090 FieldSortBuilder sortBuilder = FieldSortBuilder .getPrimaryFieldSortOrNull (canMatchContext .request .source ());
@@ -2094,6 +2096,9 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20942096 } catch (Exception e ) {
20952097 return new CanMatchShardResponse (true , null );
20962098 } finally {
2099+ if (queryRewriteContext != null ) {
2100+ canMatchContext .setTimeRangeFilterFromMillis (queryRewriteContext .getTimeRangeFilterFromMillis ());
2101+ }
20972102 Releasables .close (releasable );
20982103 }
20992104 }
0 commit comments