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