<p dir="auto">Templates exist to resolve placeholders in pieces of text and can only be used in indexed scripts and ingest. While scripts are used in many other places and exist to flexible execute custom logic related to the context it is running in.</p>
<p dir="auto">Templates are integrated into the script infrastructure (mustache implements <code class="notranslate">ScriptEngineService</code> interface, <code class="notranslate">Template</code> extends from <code class="notranslate">Script</code> and all places that use templates are hardcoded to use mustache language). I think it makes sense to strip templates from the script infrastructure and let it be its own thing. For example <code class="notranslate">Template</code> should be not extend from Script, template implementations would implement <code class="notranslate">TemplateEngineService</code> and all of the template features would be accessible from a <code class="notranslate">TemplateService</code>.</p>
<p dir="auto">The following is a list of tasks to work towards a separated <code class="notranslate">TemplateService</code>:</p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> Add <code class="notranslate">ScriptService#compileTemplate</code> and cut some script compilations to that method (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="223722245" data-permission-text="Title is private" data-url="https://github.com/elastic/elasticsearch/issues/24280" data-hovercard-type="pull_request" data-hovercard-url="/elastic/elasticsearch/pull/24280/hovercard" href="https://github.com/elastic/elasticsearch/pull/24280">#24280</a>)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> Cut all script compilation to <code class="notranslate">ScriptService#compileTemplate</code> (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="223722245" data-permission-text="Title is private" data-url="https://github.com/elastic/elasticsearch/issues/24280" data-hovercard-type="pull_request" data-hovercard-url="/elastic/elasticsearch/pull/24280/hovercard" href="https://github.com/elastic/elasticsearch/pull/24280">#24280</a>)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Create a <code class="notranslate">Template</code> class similar to the <code class="notranslate">Script</code> class that we can use to parse templates and use that with <code class="notranslate">ScriptService#compileTemplate</code></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Make a replacement for <code class="notranslate">ScriptEngineService</code> for templates, us it in <code class="notranslate">ScriptService#compileTemplate</code>, and make mustache implement that instead.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Remove template specific stuff from <code class="notranslate">Script</code> and <code class="notranslate">ScriptEngineService</code> (I'm starting to get fuzzy on exactly what this is so we'll have to clarify later)</li>
</ul>
<p dir="auto">could you also change src/main/java/org/elasticsearch/index/mapper/core/TypeParsers.java (line 66) to support empty array for "fields"?</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="   } else if (fieldName.equals(&quot;fields&quot;)) {
       Map&lt;String, Object&gt; fieldsNode = (Map&lt;String, Object&gt;) fieldNode;"><pre class="notranslate"><code class="notranslate">   } else if (fieldName.equals("fields")) {
       Map&lt;String, Object&gt; fieldsNode = (Map&lt;String, Object&gt;) fieldNode;
</code></pre></div>
<p dir="auto">similar to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="31872705" data-permission-text="Title is private" data-url="https://github.com/elastic/elasticsearch/issues/5887" data-hovercard-type="issue" data-hovercard-url="/elastic/elasticsearch/issues/5887/hovercard" href="https://github.com/elastic/elasticsearch/issues/5887">#5887</a></p>