|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.knn.plugin.script; |
| 7 | + |
| 8 | +import com.github.mustachejava.DefaultMustacheFactory; |
| 9 | +import com.github.mustachejava.Mustache; |
| 10 | +import com.github.mustachejava.MustacheException; |
| 11 | +import org.opensearch.script.GeneralScriptException; |
| 12 | +import org.opensearch.script.ScriptContext; |
| 13 | +import org.opensearch.script.ScriptEngine; |
| 14 | +import org.opensearch.script.ScriptException; |
| 15 | +import org.opensearch.script.TemplateScript; |
| 16 | +import org.opensearch.knn.plugin.stats.KNNCounter; |
| 17 | +import org.apache.logging.log4j.LogManager; |
| 18 | +import org.apache.logging.log4j.Logger; |
| 19 | + |
| 20 | +import java.io.StringReader; |
| 21 | +import java.io.StringWriter; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +/** |
| 27 | + * Mustache script engine for k-NN query templating. |
| 28 | + */ |
| 29 | +public class KNNMustacheEngine implements ScriptEngine { |
| 30 | + |
| 31 | + public static final String NAME = "knn-mustache"; |
| 32 | + private static final Logger logger = LogManager.getLogger(KNNMustacheEngine.class); |
| 33 | + |
| 34 | + @Override |
| 35 | + public String getType() { |
| 36 | + return NAME; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public <T> T compile(String templateName, String templateSource, ScriptContext<T> context, Map<String, String> options) { |
| 41 | + KNNCounter.SCRIPT_COMPILATIONS.increment(); |
| 42 | + |
| 43 | + if (!context.instanceClazz.equals(TemplateScript.class)) { |
| 44 | + KNNCounter.SCRIPT_COMPILATION_ERRORS.increment(); |
| 45 | + throw new IllegalArgumentException("knn-mustache engine only supports TemplateScript context, got [" + context.name + "]"); |
| 46 | + } |
| 47 | + |
| 48 | + try (StringReader reader = new StringReader(templateSource)) { |
| 49 | + DefaultMustacheFactory factory = new DefaultMustacheFactory(); |
| 50 | + Mustache template = factory.compile(reader, templateName != null ? templateName : "knn-template"); |
| 51 | + |
| 52 | + TemplateScript.Factory compiled = params -> new KNNMustacheTemplateScript(template, params); |
| 53 | + return context.factoryClazz.cast(compiled); |
| 54 | + } catch (MustacheException ex) { |
| 55 | + KNNCounter.SCRIPT_COMPILATION_ERRORS.increment(); |
| 56 | + logger.error("Failed to compile k-NN Mustache template [{}]: {}", templateName, ex.getMessage()); |
| 57 | + throw new ScriptException(ex.getMessage(), ex, Collections.emptyList(), templateSource, NAME); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Set<ScriptContext<?>> getSupportedContexts() { |
| 63 | + return Collections.singleton(TemplateScript.CONTEXT); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Template script implementation for k-NN query generation. |
| 68 | + */ |
| 69 | + private static class KNNMustacheTemplateScript extends TemplateScript { |
| 70 | + private final Mustache template; |
| 71 | + private final Map<String, Object> params; |
| 72 | + |
| 73 | + KNNMustacheTemplateScript(Mustache template, Map<String, Object> params) { |
| 74 | + super(params); |
| 75 | + this.template = template; |
| 76 | + this.params = params; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String execute() { |
| 81 | + StringWriter writer = new StringWriter(); |
| 82 | + try { |
| 83 | + template.execute(writer, params); |
| 84 | + return writer.toString(); |
| 85 | + } catch (Exception e) { |
| 86 | + logger.error("Error executing k-NN mustache template: {}", e.getMessage()); |
| 87 | + throw new GeneralScriptException("Error executing k-NN mustache template: " + e.getMessage(), e); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments