1111
1212import org .elasticsearch .cluster .metadata .MappingMetadata ;
1313import org .elasticsearch .core .Booleans ;
14- import org .elasticsearch .core .Nullable ;
1514import org .elasticsearch .index .IndexService ;
16- import org .elasticsearch .index .engine .Engine ;
1715import org .elasticsearch .index .mapper .MappedFieldType ;
1816import org .elasticsearch .index .mapper .RuntimeField ;
1917import org .elasticsearch .index .query .MatchAllQueryBuilder ;
2018import org .elasticsearch .index .query .QueryBuilder ;
21- import org .elasticsearch .index .query .SearchExecutionContext ;
19+ import org .elasticsearch .index .query .QueryRewriteContext ;
2220import org .elasticsearch .index .shard .IndexShard ;
2321import org .elasticsearch .index .shard .ShardId ;
2422import org .elasticsearch .indices .IndicesService ;
@@ -65,28 +63,11 @@ FieldCapabilitiesIndexResponse fetch(
6563 ) throws IOException {
6664 final IndexService indexService = indicesService .indexServiceSafe (shardId .getIndex ());
6765 final IndexShard indexShard = indexService .getShard (shardId .getId ());
68- final Engine .Searcher searcher ;
6966 if (alwaysMatches (indexFilter )) {
7067 // no need to open a searcher if we aren't filtering, but make sure we are reading from an up-to-dated shard
7168 indexShard .readAllowed ();
72- searcher = null ;
73- } else {
74- searcher = indexShard .acquireSearcher (Engine .CAN_MATCH_SEARCH_SOURCE );
75- }
76- try (searcher ) {
77- return doFetch (
78- task ,
79- shardId ,
80- fieldNameFilter ,
81- filters ,
82- fieldTypes ,
83- indexFilter ,
84- nowInMillis ,
85- runtimeFields ,
86- indexService ,
87- searcher
88- );
8969 }
70+ return doFetch (task , shardId , fieldNameFilter , filters , fieldTypes , indexFilter , nowInMillis , runtimeFields , indexService );
9071 }
9172
9273 private FieldCapabilitiesIndexResponse doFetch (
@@ -98,22 +79,13 @@ private FieldCapabilitiesIndexResponse doFetch(
9879 QueryBuilder indexFilter ,
9980 long nowInMillis ,
10081 Map <String , Object > runtimeFields ,
101- IndexService indexService ,
102- @ Nullable Engine .Searcher searcher
82+ IndexService indexService
10383 ) throws IOException {
104- final SearchExecutionContext searchExecutionContext = indexService .newSearchExecutionContext (
105- shardId .id (),
106- 0 ,
107- searcher ,
108- () -> nowInMillis ,
109- null ,
110- runtimeFields
111- );
112- var indexMode = searchExecutionContext .getIndexSettings ().getMode ();
113- if (searcher != null && canMatchShard (shardId , indexFilter , nowInMillis , searchExecutionContext ) == false ) {
84+ QueryRewriteContext queryRewriteContext = indexService .newQueryRewriteContext (() -> nowInMillis , runtimeFields , null );
85+ var indexMode = indexService .getIndexSettings ().getMode ();
86+ if (canMatchShard (shardId , indexFilter , nowInMillis , queryRewriteContext ) == false ) {
11487 return new FieldCapabilitiesIndexResponse (shardId .getIndexName (), null , Collections .emptyMap (), false , indexMode );
11588 }
116-
11789 final MappingMetadata mapping = indexService .getMetadata ().mapping ();
11890 String indexMappingHash ;
11991 if (includeEmptyFields || enableFieldHasValue == false ) {
@@ -135,7 +107,7 @@ private FieldCapabilitiesIndexResponse doFetch(
135107 }
136108 task .ensureNotCancelled ();
137109 final Map <String , IndexFieldCapabilities > responseMap = retrieveFieldCaps (
138- searchExecutionContext ,
110+ queryRewriteContext ,
139111 fieldNameFilter ,
140112 filters ,
141113 fieldTypes ,
@@ -150,7 +122,7 @@ private FieldCapabilitiesIndexResponse doFetch(
150122 }
151123
152124 static Map <String , IndexFieldCapabilities > retrieveFieldCaps (
153- SearchExecutionContext context ,
125+ QueryRewriteContext context ,
154126 Predicate <String > fieldNameFilter ,
155127 String [] filters ,
156128 String [] types ,
@@ -236,20 +208,20 @@ private static boolean canMatchShard(
236208 ShardId shardId ,
237209 QueryBuilder indexFilter ,
238210 long nowInMillis ,
239- SearchExecutionContext searchExecutionContext
211+ QueryRewriteContext queryRewriteContext
240212 ) throws IOException {
241213 assert alwaysMatches (indexFilter ) == false : "should not be called for always matching [" + indexFilter + "]" ;
242214 assert nowInMillis != 0L ;
243215 ShardSearchRequest searchRequest = new ShardSearchRequest (shardId , nowInMillis , AliasFilter .EMPTY );
244216 searchRequest .source (new SearchSourceBuilder ().query (indexFilter ));
245- return SearchService .queryStillMatchesAfterRewrite (searchRequest , searchExecutionContext );
217+ return SearchService .queryStillMatchesAfterRewrite (searchRequest , queryRewriteContext );
246218 }
247219
248220 private static boolean alwaysMatches (QueryBuilder indexFilter ) {
249221 return indexFilter == null || indexFilter instanceof MatchAllQueryBuilder ;
250222 }
251223
252- private static Predicate <MappedFieldType > buildFilter (String [] filters , String [] fieldTypes , SearchExecutionContext context ) {
224+ private static Predicate <MappedFieldType > buildFilter (String [] filters , String [] fieldTypes , QueryRewriteContext context ) {
253225 // security filters don't exclude metadata fields
254226 Predicate <MappedFieldType > fcf = null ;
255227 if (fieldTypes .length > 0 ) {
0 commit comments