@@ -1942,10 +1942,11 @@ public void canMatch(CanMatchNodeRequest request, ActionListener<CanMatchNodeRes
19421942 final IndexService indexService = indicesService .indexServiceSafe (shardSearchRequest .shardId ().getIndex ());
19431943 final IndexShard indexShard = indexService .getShard (shardSearchRequest .shardId ().id ());
19441944 try {
1945- // TODO remove the exception handling as it's now in canMatch itself
1946- responses .add (new CanMatchNodeResponse .ResponseOrFailure (canMatch (shardSearchRequest )));
1947- indexShard .getSearchOperationListener ()
1948- .onCanMatchPhase (shardSearchRequest , System .nanoTime () - shardCanMatchStartTimeInNanos );
1945+ // TODO remove the exception handling as it's now in canMatch itself - the failure no longer needs to be serialized
1946+ CanMatchContext canMatchContext = createCanMatchContext (shardSearchRequest );
1947+ CanMatchShardResponse canMatchShardResponse = canMatch (canMatchContext , true );
1948+ responses .add (new CanMatchNodeResponse .ResponseOrFailure (canMatchShardResponse ));
1949+ indexShard .getSearchOperationListener ().onCanMatchPhase (System .nanoTime () - shardCanMatchStartTimeInNanos );
19491950 } catch (Exception e ) {
19501951 responses .add (new CanMatchNodeResponse .ResponseOrFailure (e ));
19511952 }
@@ -1959,16 +1960,14 @@ public void canMatch(CanMatchNodeRequest request, ActionListener<CanMatchNodeRes
19591960 * won't match any documents on the current shard. Exceptions are handled within the method, and never re-thrown.
19601961 */
19611962 public CanMatchShardResponse canMatch (ShardSearchRequest request ) {
1962- CanMatchContext canMatchContext = new CanMatchContext (
1963- request ,
1964- indicesService ::indexServiceSafe ,
1965- this ::findReaderContext ,
1966- defaultKeepAlive ,
1967- maxKeepAlive
1968- );
1963+ CanMatchContext canMatchContext = createCanMatchContext (request );
19691964 return canMatch (canMatchContext , true );
19701965 }
19711966
1967+ CanMatchContext createCanMatchContext (ShardSearchRequest request ) {
1968+ return new CanMatchContext (request , indicesService ::indexServiceSafe , this ::findReaderContext , defaultKeepAlive , maxKeepAlive );
1969+ }
1970+
19721971 static class CanMatchContext {
19731972 private final ShardSearchRequest request ;
19741973 private final Function <Index , IndexService > indexServiceLookup ;
@@ -1978,6 +1977,8 @@ static class CanMatchContext {
19781977
19791978 private IndexService indexService ;
19801979
1980+ private Long timeRangeFilterFromMillis ;
1981+
19811982 CanMatchContext (
19821983 ShardSearchRequest request ,
19831984 Function <Index , IndexService > indexServiceLookup ,
@@ -2025,12 +2026,21 @@ IndexService getIndexService() {
20252026 }
20262027 return this .indexService ;
20272028 }
2029+
2030+ void setTimeRangeFilterFromMillis (Long timeRangeFilterFromMillis ) {
2031+ this .timeRangeFilterFromMillis = timeRangeFilterFromMillis ;
2032+ }
2033+
2034+ Long getTimeRangeFilterFromMillis () {
2035+ return timeRangeFilterFromMillis ;
2036+ }
20282037 }
20292038
20302039 static CanMatchShardResponse canMatch (CanMatchContext canMatchContext , boolean checkRefreshPending ) {
20312040 assert canMatchContext .request .searchType () == SearchType .QUERY_THEN_FETCH
20322041 : "unexpected search type: " + canMatchContext .request .searchType ();
20332042 Releasable releasable = null ;
2043+ QueryRewriteContext queryRewriteContext = null ;
20342044 try {
20352045 IndexService indexService ;
20362046 final boolean hasRefreshPending ;
@@ -2043,7 +2053,7 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20432053 readerContext = canMatchContext .findReaderContext ();
20442054 releasable = readerContext .markAsUsed (canMatchContext .getKeepAlive ());
20452055 indexService = readerContext .indexService ();
2046- QueryRewriteContext queryRewriteContext = canMatchContext .getQueryRewriteContext (indexService );
2056+ queryRewriteContext = canMatchContext .getQueryRewriteContext (indexService );
20472057 if (queryStillMatchesAfterRewrite (canMatchContext .request , queryRewriteContext ) == false ) {
20482058 return new CanMatchShardResponse (false , null );
20492059 }
@@ -2052,10 +2062,8 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20522062 if (canMatchContext .request .readerId ().isRetryable () == false ) {
20532063 return new CanMatchShardResponse (true , null );
20542064 }
2055- if (queryStillMatchesAfterRewrite (
2056- canMatchContext .request ,
2057- canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ())
2058- ) == false ) {
2065+ queryRewriteContext = canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ());
2066+ if (queryStillMatchesAfterRewrite (canMatchContext .request , queryRewriteContext ) == false ) {
20592067 return new CanMatchShardResponse (false , null );
20602068 }
20612069 final Engine .SearcherSupplier searcherSupplier = canMatchContext .getShard ().acquireSearcherSupplier ();
@@ -2068,10 +2076,8 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20682076 }
20692077 canMatchSearcher = searcher ;
20702078 } else {
2071- if (queryStillMatchesAfterRewrite (
2072- canMatchContext .request ,
2073- canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ())
2074- ) == false ) {
2079+ queryRewriteContext = canMatchContext .getQueryRewriteContext (canMatchContext .getIndexService ());
2080+ if (queryStillMatchesAfterRewrite (canMatchContext .request , queryRewriteContext ) == false ) {
20752081 return new CanMatchShardResponse (false , null );
20762082 }
20772083 boolean needsWaitForRefresh = canMatchContext .request .waitForCheckpoint () != UNASSIGNED_SEQ_NO ;
@@ -2084,6 +2090,7 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20842090 }
20852091 try (canMatchSearcher ) {
20862092 SearchExecutionContext context = canMatchContext .getSearchExecutionContext (canMatchSearcher );
2093+ queryRewriteContext = context ;
20872094 final boolean canMatch = queryStillMatchesAfterRewrite (canMatchContext .request , context );
20882095 if (canMatch || hasRefreshPending ) {
20892096 FieldSortBuilder sortBuilder = FieldSortBuilder .getPrimaryFieldSortOrNull (canMatchContext .request .source ());
@@ -2095,6 +2102,9 @@ static CanMatchShardResponse canMatch(CanMatchContext canMatchContext, boolean c
20952102 } catch (Exception e ) {
20962103 return new CanMatchShardResponse (true , null );
20972104 } finally {
2105+ if (queryRewriteContext != null ) {
2106+ canMatchContext .setTimeRangeFilterFromMillis (queryRewriteContext .getTimeRangeFilterFromMillis ());
2107+ }
20982108 Releasables .close (releasable );
20992109 }
21002110 }
0 commit comments