use of org.opensearch.painless.action.PainlessExecuteAction.Response in project OpenSearch by opensearch-project.
the class PainlessExecuteApiTests method testFilterExecutionContext.
public void testFilterExecutionContext() throws IOException {
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "field", "type=long");
Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null);
contextSetup.setXContentType(XContentType.JSON);
Request request = new Request(new Script("doc['field'].value >= 3"), "filter", contextSetup);
Response response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(true));
contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null);
contextSetup.setXContentType(XContentType.JSON);
request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), "filter", contextSetup);
response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(true));
contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 2}"), null);
contextSetup.setXContentType(XContentType.JSON);
request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), "filter", contextSetup);
response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(false));
}
use of org.opensearch.painless.action.PainlessExecuteAction.Response in project OpenSearch by opensearch-project.
the class PainlessExecuteApiTests method testDefaults.
public void testDefaults() throws IOException {
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
Request request = new Request(new Script("100.0 / 1000.0"), null, null);
Response response = innerShardOperation(request, scriptService, null);
assertThat(response.getResult(), equalTo("0.1"));
Map<String, Object> params = new HashMap<>();
params.put("count", 100.0D);
params.put("total", 1000.0D);
request = new Request(new Script(ScriptType.INLINE, "painless", "params.count / params.total", params), null, null);
response = innerShardOperation(request, scriptService, null);
assertThat(response.getResult(), equalTo("0.1"));
Exception e = expectThrows(ScriptException.class, () -> {
Request r = new Request(new Script(ScriptType.INLINE, "painless", "params.count / params.total + doc['constant']", params), null, null);
innerShardOperation(r, scriptService, null);
});
assertThat(e.getCause().getMessage(), equalTo("cannot resolve symbol [doc]"));
}
use of org.opensearch.painless.action.PainlessExecuteAction.Response in project OpenSearch by opensearch-project.
the class PainlessExecuteApiTests method testScoreExecutionContext.
public void testScoreExecutionContext() throws IOException {
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=text");
Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), new MatchQueryBuilder("text", "fox"));
contextSetup.setXContentType(XContentType.JSON);
Request request = new Request(new Script(ScriptType.INLINE, "painless", "Math.round((_score + (doc['rank'].value / params.max_rank)) * 100.0) / 100.0", singletonMap("max_rank", 5.0)), "score", contextSetup);
Response response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(0.93D));
}
Aggregations