Search in sources :

Example 1 with Response

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));
}
Also used : ScriptService(org.opensearch.script.ScriptService) Response(org.opensearch.painless.action.PainlessExecuteAction.Response) Script(org.opensearch.script.Script) BytesArray(org.opensearch.common.bytes.BytesArray) IndexService(org.opensearch.index.IndexService) Request(org.opensearch.painless.action.PainlessExecuteAction.Request)

Example 2 with Response

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]"));
}
Also used : ScriptService(org.opensearch.script.ScriptService) Response(org.opensearch.painless.action.PainlessExecuteAction.Response) Script(org.opensearch.script.Script) HashMap(java.util.HashMap) Request(org.opensearch.painless.action.PainlessExecuteAction.Request) IOException(java.io.IOException) ScriptException(org.opensearch.script.ScriptException)

Example 3 with Response

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));
}
Also used : ScriptService(org.opensearch.script.ScriptService) Response(org.opensearch.painless.action.PainlessExecuteAction.Response) Script(org.opensearch.script.Script) BytesArray(org.opensearch.common.bytes.BytesArray) IndexService(org.opensearch.index.IndexService) Request(org.opensearch.painless.action.PainlessExecuteAction.Request) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder)

Aggregations

Request (org.opensearch.painless.action.PainlessExecuteAction.Request)3 Response (org.opensearch.painless.action.PainlessExecuteAction.Response)3 Script (org.opensearch.script.Script)3 ScriptService (org.opensearch.script.ScriptService)3 BytesArray (org.opensearch.common.bytes.BytesArray)2 IndexService (org.opensearch.index.IndexService)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)1 ScriptException (org.opensearch.script.ScriptException)1