@@ -541,18 +541,38 @@ public static TemplateScript.Factory compileTemplate(
541541 Script script = new Script (ScriptType .INLINE , DEFAULT_TEMPLATE_LANG , propertyValue , Map .of ());
542542 return scriptService .compile (script , TemplateScript .CONTEXT );
543543 } else {
544- return (params ) -> new TemplateScript (params ) {
545- @ Override
546- public String execute () {
547- return propertyValue ;
548- }
549- };
544+ return new ConstantTemplateScriptFactory (propertyValue );
550545 }
551546 } catch (Exception e ) {
552547 throw ConfigurationUtils .newConfigurationException (processorType , processorTag , propertyName , e );
553548 }
554549 }
555550
551+ /**
552+ * A 'template script' that ignores the model to which it is applied and just always returns a constant String.
553+ * <p>
554+ * Having a separate named class for this allows for some hot code paths to pre-apply the 'template script' statically,
555+ * rather than bothering to invoke it per-document. Note that this is probably only useful if something expensive
556+ * is being done with the result of calling the script, and the code can then avoid doing that thing per-document.
557+ */
558+ public static class ConstantTemplateScriptFactory implements TemplateScript .Factory {
559+ final TemplateScript script ;
560+
561+ private ConstantTemplateScriptFactory (String value ) {
562+ this .script = new TemplateScript (Map .of ()) {
563+ @ Override
564+ public String execute () {
565+ return value ;
566+ }
567+ };
568+ }
569+
570+ @ Override
571+ public TemplateScript newInstance (Map <String , Object > params ) {
572+ return script ;
573+ }
574+ }
575+
556576 private static void addMetadataToException (
557577 ElasticsearchException exception ,
558578 String processorType ,
0 commit comments