@Test public void testNativeScript() throws InterruptedException { Settings settings = Settings.settingsBuilder() .put("script.native.my.type", MyNativeScriptFactory.class.getName()) .put("name", "testNativeScript") .put("path.home", createTempDir()) .build(); Injector injector = new ModulesBuilder() .add( new EnvironmentModule(new Environment(settings)), new ThreadPoolModule(new ThreadPool(settings)), new SettingsModule(settings), new ScriptModule(settings)) .createInjector(); ScriptService scriptService = injector.getInstance(ScriptService.class); ExecutableScript executable = scriptService.executable( new Script("my", ScriptType.INLINE, NativeScriptEngineService.NAME, null), ScriptContext.Standard.SEARCH); assertThat(executable.run().toString(), equalTo("test")); terminate(injector.getInstance(ThreadPool.class)); }
@Override public SignificanceHeuristic parse(XContentParser parser) throws IOException, QueryParsingException { NAMES_FIELD.match(parser.currentName(), ParseField.EMPTY_FLAGS); String script = null; String scriptLang; XContentParser.Token token; Map<String, Object> params = new HashMap<>(); String currentFieldName = null; ScriptService.ScriptType scriptType = ScriptService.ScriptType.INLINE; ScriptParameterParser scriptParameterParser = new ScriptParameterParser(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token.equals(XContentParser.Token.FIELD_NAME)) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if ("params".equals(currentFieldName)) { params = parser.map(); } else { throw new ElasticsearchParseException( "unknown object " + currentFieldName + " in script_heuristic"); } } else if (!scriptParameterParser.token(currentFieldName, token, parser)) { throw new ElasticsearchParseException( "unknown field " + currentFieldName + " in script_heuristic"); } } ScriptParameterParser.ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue(); if (scriptValue != null) { script = scriptValue.script(); scriptType = scriptValue.scriptType(); } scriptLang = scriptParameterParser.lang(); if (script == null) { throw new ElasticsearchParseException("No script found in script_heuristic"); } ExecutableScript searchScript; try { searchScript = scriptService.executable( scriptLang, script, scriptType, ScriptContext.Standard.AGGS, params); } catch (Exception e) { throw new ElasticsearchParseException("The script [" + script + "] could not be loaded", e); } return new ScriptHeuristic(searchScript, scriptLang, script, scriptType, params); }