本文整理汇总了Java中org.elasticsearch.script.CompiledScript类的典型用法代码示例。如果您正苦于以下问题:Java CompiledScript类的具体用法?Java CompiledScript怎么用?Java CompiledScript使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CompiledScript类属于org.elasticsearch.script包,在下文中一共展示了CompiledScript类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBasics
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
public void testBasics() {
String template = "GET _search {\"query\": " + "{\"boosting\": {"
+ "\"positive\": {\"match\": {\"body\": \"gift\"}},"
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}"
+ "}}, \"negative_boost\": {{boost_val}} } }}";
Map<String, Object> params = Collections.singletonMap("boost_val", "0.2");
Mustache mustache = (Mustache) engine.compile(null, template, Collections.emptyMap());
CompiledScript compiledScript = new CompiledScript(INLINE, "my-name", "mustache", mustache);
ExecutableScript result = engine.executable(compiledScript, params);
assertEquals(
"Mustache templating broken",
"GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.2 } }}",
((BytesReference) result.run()).utf8ToString()
);
}
示例2: ExpressionExecutableScript
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
public ExpressionExecutableScript(CompiledScript compiledScript, Map<String, Object> vars) {
this.compiledScript = compiledScript;
Expression expression = (Expression)this.compiledScript.compiled();
int functionValuesLength = expression.variables.length;
if (vars.size() != functionValuesLength) {
throw new GeneralScriptException("Error using " + compiledScript + ". " +
"The number of variables in an executable expression script [" +
functionValuesLength + "] must match the number of variables in the variable map" +
" [" + vars.size() + "].");
}
functionValuesArray = new ReplaceableConstDoubleValues[functionValuesLength];
functionValuesMap = new HashMap<>();
for (int functionValuesIndex = 0; functionValuesIndex < functionValuesLength; ++functionValuesIndex) {
String variableName = expression.variables[functionValuesIndex];
functionValuesArray[functionValuesIndex] = new ReplaceableConstDoubleValues();
functionValuesMap.put(variableName, functionValuesArray[functionValuesIndex]);
}
for (String varsName : vars.keySet()) {
setNextVar(varsName, vars.get(varsName));
}
}
示例3: testArrayInArrayAccess
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
public void testArrayInArrayAccess() throws Exception {
String template = "{{data.0.0}} {{data.0.1}}";
CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
Map<String, Object> vars = new HashMap<>();
Object data = randomFrom(
new String[][] { new String[] { "foo", "bar" }},
Collections.singletonList(new String[] { "foo", "bar" }),
singleton(new String[] { "foo", "bar" })
);
vars.put("data", data);
Object output = engine.executable(mustache, vars).run();
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
BytesReference bytes = (BytesReference) output;
assertThat(bytes.utf8ToString(), equalTo("foo bar"));
}
示例4: testSizeAccessForCollectionsAndArrays
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
public void testSizeAccessForCollectionsAndArrays() throws Exception {
String[] randomArrayValues = generateRandomStringArray(10, 20, false);
List<String> randomList = Arrays.asList(generateRandomStringArray(10, 20, false));
String template = "{{data.array.size}} {{data.list.size}}";
CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
Map<String, Object> data = new HashMap<>();
data.put("array", randomArrayValues);
data.put("list", randomList);
Map<String, Object> vars = new HashMap<>();
vars.put("data", data);
Object output = engine.executable(mustache, vars).run();
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
BytesReference bytes = (BytesReference) output;
String expectedString = String.format(Locale.ROOT, "%s %s", randomArrayValues.length, randomList.size());
assertThat(bytes.utf8ToString(), equalTo(expectedString));
}
示例5: search
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
/**
* Retrieve a {@link SearchScript} for later use.
* @param compiledScript A previously compiled script.
* @param lookup The object that ultimately allows access to search fields.
* @param vars The variables to be used in the script.
* @return An {@link SearchScript} with the currently specified variables.
*/
@Override
public SearchScript search(final CompiledScript compiledScript, final SearchLookup lookup, final Map<String, Object> vars) {
return new SearchScript() {
/**
* Get the search script that will have access to search field values.
* @param context The LeafReaderContext to be used.
* @return A script that will have the search fields from the current context available for use.
*/
@Override
public LeafSearchScript getLeafSearchScript(final LeafReaderContext context) throws IOException {
return new ScriptImpl((GenericElasticsearchScript) compiledScript.compiled(), vars, lookup.getLeafSearchLookup(context));
}
/**
* Whether or not the score is needed.
*/
@Override
public boolean needsScores() {
return ((GenericElasticsearchScript) compiledScript.compiled()).uses$_score();
}
};
}
示例6: exec
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
/** Compiles and returns the result of {@code script} with access to {@code vars} and compile-time parameters */
public Object exec(String script, Map<String, Object> vars, Map<String,String> compileParams, Scorer scorer, boolean picky) {
// test for ambiguity errors before running the actual script if picky is true
if (picky) {
ScriptInterface scriptInterface = new ScriptInterface(GenericElasticsearchScript.class);
CompilerSettings pickySettings = new CompilerSettings();
pickySettings.setPicky(true);
pickySettings.setRegexesEnabled(CompilerSettings.REGEX_ENABLED.get(scriptEngineSettings()));
Walker.buildPainlessTree(scriptInterface, getTestName(), script, pickySettings, null);
}
// test actual script execution
Object object = scriptEngine.compile(null, script, compileParams);
CompiledScript compiled = new CompiledScript(ScriptType.INLINE, getTestName(), "painless", object);
ExecutableScript executableScript = scriptEngine.executable(compiled, vars);
if (scorer != null) {
((ScorerAware)executableScript).setScorer(scorer);
}
return executableScript.run();
}
示例7: testChangingVarsCrossExecution1
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
public void testChangingVarsCrossExecution1() {
Map<String, Object> vars = new HashMap<>();
Map<String, Object> ctx = new HashMap<>();
vars.put("ctx", ctx);
Object compiledScript = scriptEngine.compile(null,
"return ctx.value;", Collections.emptyMap());
ExecutableScript script = scriptEngine.executable(new CompiledScript(ScriptType.INLINE,
"testChangingVarsCrossExecution1", "painless", compiledScript), vars);
ctx.put("value", 1);
Object o = script.run();
assertEquals(1, ((Number) o).intValue());
ctx.put("value", 2);
o = script.run();
assertEquals(2, ((Number) o).intValue());
}
示例8: executable
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
@Override
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
final String field = (String) compiledScript.compiled();
return new ExecutableScript() {
Map<String, Object> vars = new HashMap<>();
@Override
public void setNextVar(String name, Object value) {
vars.put(name, value);
}
@Override
public Object run() {
Map<String, Object> ctx = (Map<String, Object>) vars.get("ctx");
assertNotNull(ctx);
Map<String, Object> source = (Map<String, Object>) ctx.get("_source");
Number currentValue = (Number) source.get(field);
Number inc = params == null ? 1L : (Number) params.getOrDefault("inc", 1);
source.put(field, currentValue.longValue() + inc.longValue());
return ctx;
}
};
}
示例9: executable
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
@Override
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
String script = (String) compiledScript.compiled();
for (Entry<String, Object> entry : params.entrySet()) {
script = script.replace("{{" + entry.getKey() + "}}", String.valueOf(entry.getValue()));
}
String result = script;
return new ExecutableScript() {
@Override
public void setNextVar(String name, Object value) {
throw new UnsupportedOperationException("setNextVar not supported");
}
@Override
public Object run() {
return new BytesArray(result);
}
};
}
示例10: init
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
@BeforeClass
public static void init() throws IOException {
Path genericConfigFolder = createTempDir();
Settings baseSettings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
.put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder)
.build();
Environment environment = new Environment(baseSettings);
ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new TestEngineService()));
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
scriptService = new ScriptService(baseSettings, environment,
new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings) {
@Override
public CompiledScript compile(Script script, ScriptContext scriptContext) {
return new CompiledScript(ScriptType.INLINE, "mockName", "test", script);
}
};
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
示例11: search
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
@Override
public SearchScript search(CompiledScript compiledScript, final SearchLookup lookup, @Nullable final Map<String, Object> vars) {
final VectorScoreScript.Factory scriptFactory = (VectorScoreScript.Factory) compiledScript.compiled();
final VectorScoreScript script = (VectorScoreScript) scriptFactory.newScript(vars);
return new SearchScript() {
@Override
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
script.setBinaryEmbeddingReader(context.reader().getBinaryDocValues(script.field));
return script;
}
@Override
public boolean needsScores() {
return scriptFactory.needsScores();
}
};
}
开发者ID:lior-k,项目名称:fast-elasticsearch-vector-scoring,代码行数:17,代码来源:VectorScoringScriptEngineService.java
示例12: search
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
@SuppressWarnings({"unchecked"})
@Override
public SearchScript search(final CompiledScript compiledScript, final SearchLookup lookup, @Nullable final Map<String, Object> vars) {
return new SearchScript() {
@Override
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
final LeafSearchLookup leafLookup = lookup.getLeafSearchLookup(context);
boolean debug = true;
if (vars != null && vars.containsKey("debug")) {
debug = (Boolean)vars.get("debug");
}
return ((Factory) compiledScript.compiled()).newScript(leafLookup, debug);
}
@Override
public boolean needsScores() {
// TODO: can we reliably know if a vectorizer script does not make use of _score
return false;
}
};
}
示例13: provideUserScript
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
@Provides
@Singleton
public UserScript provideUserScript(ArangoDbConfig config, ScriptService scriptService) {
String scriptString = config.getArangodbScript();
if (isBlank(scriptString)) {
return new NullUserScript();
}
String scriptType = config.getArangodbScripttype();
if (isBlank(scriptType)) {
return new NullUserScript();
}
CompiledScript compiled = scriptService.compile(scriptType, scriptString, ScriptType.INLINE);
return new ConcreteUserScript(scriptService, compiled);
}
示例14: ExpressionSearchScript
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
ExpressionSearchScript(CompiledScript c, SimpleBindings b, ReplaceableConstDoubleValueSource v, boolean needsScores) {
compiledScript = c;
bindings = b;
source = ((Expression)compiledScript.compiled()).getDoubleValuesSource(bindings);
specialValue = v;
this.needsScores = needsScores;
}
示例15: testSimple
import org.elasticsearch.script.CompiledScript; //导入依赖的package包/类
public void testSimple() throws IOException {
String templateString =
"{"
+ "\"inline\":{\"match_{{template}}\": {}},"
+ "\"params\":{\"template\":\"all\"}"
+ "}";
XContentParser parser = createParser(JsonXContent.jsonXContent, templateString);
Script script = Script.parse(parser);
CompiledScript compiledScript = new CompiledScript(ScriptType.INLINE, null, "mustache",
qe.compile(null, script.getIdOrCode(), Collections.emptyMap()));
ExecutableScript executableScript = qe.executable(compiledScript, script.getParams());
assertThat(((BytesReference) executableScript.run()).utf8ToString(), equalTo("{\"match_all\":{}}"));
}