1616import org .elasticsearch .cluster .metadata .InferenceFieldMetadata ;
1717import org .elasticsearch .common .io .stream .StreamInput ;
1818import org .elasticsearch .common .io .stream .StreamOutput ;
19+ import org .elasticsearch .core .TimeValue ;
20+ import org .elasticsearch .index .mapper .InferenceMetadataFieldsMapper ;
1921import org .elasticsearch .index .mapper .MappedFieldType ;
2022import org .elasticsearch .index .query .AbstractQueryBuilder ;
2123import org .elasticsearch .index .query .MatchNoneQueryBuilder ;
3133import org .elasticsearch .xcontent .XContentBuilder ;
3234import org .elasticsearch .xcontent .XContentParser ;
3335import org .elasticsearch .xpack .core .inference .action .InferenceAction ;
34- import org .elasticsearch .xpack .core .ml .action .InferModelAction ;
3536import org .elasticsearch .xpack .core .ml .inference .results .ErrorInferenceResults ;
3637import org .elasticsearch .xpack .core .ml .inference .results .MlTextEmbeddingResults ;
3738import org .elasticsearch .xpack .core .ml .inference .results .TextExpansionResults ;
@@ -225,6 +226,10 @@ private SemanticQueryBuilder doRewriteGetInferenceResults(QueryRewriteContext qu
225226 }
226227
227228 String inferenceId = getInferenceIdForForField (resolvedIndices .getConcreteLocalIndicesMetadata ().values (), fieldName );
229+ TimeValue inferenceTimeout = getInferenceTimeeoutForSemanticField (
230+ resolvedIndices .getConcreteLocalIndicesMetadata ().values (),
231+ fieldName
232+ );
228233 SetOnce <InferenceServiceResults > inferenceResultsSupplier = new SetOnce <>();
229234 boolean noInferenceResults = false ;
230235 if (inferenceId != null ) {
@@ -237,7 +242,7 @@ private SemanticQueryBuilder doRewriteGetInferenceResults(QueryRewriteContext qu
237242 List .of (query ),
238243 Map .of (),
239244 InputType .INTERNAL_SEARCH ,
240- InferModelAction . Request . DEFAULT_TIMEOUT_FOR_API ,
245+ inferenceTimeout ,
241246 false
242247 );
243248
@@ -264,6 +269,33 @@ private SemanticQueryBuilder doRewriteGetInferenceResults(QueryRewriteContext qu
264269 return new SemanticQueryBuilder (this , noInferenceResults ? null : inferenceResultsSupplier , null , noInferenceResults );
265270 }
266271
272+ @ SuppressWarnings ("unchecked" )
273+ private TimeValue getInferenceTimeeoutForSemanticField (Collection <IndexMetadata > indexMetadataCollection , String fieldName ) {
274+ TimeValue inferenceTimeout = InferenceMetadataFieldsMapper .DEFAULT_SEMANTIC_TEXT_INFERENCE_TIMEOUT ;
275+ for (IndexMetadata indexMetadata : indexMetadataCollection ) {
276+ boolean fieldExistsInIndex = indexMetadata .mapping ()
277+ .getSourceAsMap ()
278+ .values ()
279+ .stream ()
280+ .filter (v -> v instanceof Map )
281+ .map (v -> (Map <String , Object >) v )
282+ .anyMatch (m -> m .containsKey (fieldName ));
283+
284+ if (fieldExistsInIndex == false ) {
285+ continue ;
286+ }
287+
288+ TimeValue currentInferenceTimeout = indexMetadata .getSettings ()
289+ .getAsTime ("index.semantic_text.inference_timeout" , InferenceMetadataFieldsMapper .DEFAULT_SEMANTIC_TEXT_INFERENCE_TIMEOUT );
290+
291+ if (currentInferenceTimeout .compareTo (inferenceTimeout ) < 0 ) {
292+ inferenceTimeout = currentInferenceTimeout ;
293+ }
294+ }
295+
296+ return inferenceTimeout ;
297+ }
298+
267299 private static InferenceResults validateAndConvertInferenceResults (
268300 SetOnce <InferenceServiceResults > inferenceResultsSupplier ,
269301 String fieldName
0 commit comments