Search in sources :

Example 1 with GeneralScriptException

use of org.elasticsearch.script.GeneralScriptException in project elasticsearch by elastic.

the class MoreExpressionTests method testExecutableScripts.

// series of unit test for using expressions as executable scripts
public void testExecutableScripts() throws Exception {
    assumeTrue("test creates classes directly, cannot run with security manager", System.getSecurityManager() == null);
    Map<String, Object> vars = new HashMap<>();
    vars.put("a", 2.5);
    vars.put("b", 3);
    vars.put("xyz", -1);
    Expression expr = JavascriptCompiler.compile("a+b+xyz");
    CompiledScript compiledScript = new CompiledScript(ScriptType.INLINE, "", "expression", expr);
    ExpressionExecutableScript ees = new ExpressionExecutableScript(compiledScript, vars);
    assertEquals((Double) ees.run(), 4.5, 0.001);
    ees.setNextVar("b", -2.5);
    assertEquals((Double) ees.run(), -1, 0.001);
    ees.setNextVar("a", -2.5);
    ees.setNextVar("b", -2.5);
    ees.setNextVar("xyz", -2.5);
    assertEquals((Double) ees.run(), -7.5, 0.001);
    String message;
    try {
        vars = new HashMap<>();
        vars.put("a", 1);
        ees = new ExpressionExecutableScript(compiledScript, vars);
        ees.run();
        fail("An incorrect number of variables were allowed to be used in an expression.");
    } catch (GeneralScriptException se) {
        message = se.getMessage();
        assertThat(message + " should have contained number of variables", message.contains("number of variables"), equalTo(true));
    }
    try {
        vars = new HashMap<>();
        vars.put("a", 1);
        vars.put("b", 3);
        vars.put("c", -1);
        ees = new ExpressionExecutableScript(compiledScript, vars);
        ees.run();
        fail("A variable was allowed to be set that does not exist in the expression.");
    } catch (GeneralScriptException se) {
        message = se.getMessage();
        assertThat(message + " should have contained does not exist in", message.contains("does not exist in"), equalTo(true));
    }
    try {
        vars = new HashMap<>();
        vars.put("a", 1);
        vars.put("b", 3);
        vars.put("xyz", "hello");
        ees = new ExpressionExecutableScript(compiledScript, vars);
        ees.run();
        fail("A non-number was allowed to be use in the expression.");
    } catch (GeneralScriptException se) {
        message = se.getMessage();
        assertThat(message + " should have contained process numbers", message.contains("process numbers"), equalTo(true));
    }
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) GeneralScriptException(org.elasticsearch.script.GeneralScriptException)

Example 2 with GeneralScriptException

use of org.elasticsearch.script.GeneralScriptException in project elasticsearch by elastic.

the class ScriptScoreFunction method getLeafScoreFunction.

@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) throws IOException {
    final LeafSearchScript leafScript = script.getLeafSearchScript(ctx);
    final CannedScorer scorer = new CannedScorer();
    leafScript.setScorer(scorer);
    return new LeafScoreFunction() {

        @Override
        public double score(int docId, float subQueryScore) {
            leafScript.setDocument(docId);
            scorer.docid = docId;
            scorer.score = subQueryScore;
            double result = leafScript.runAsDouble();
            if (Double.isNaN(result)) {
                throw new GeneralScriptException("script_score returned NaN");
            }
            return result;
        }

        @Override
        public Explanation explainScore(int docId, Explanation subQueryScore) throws IOException {
            Explanation exp;
            if (leafScript instanceof ExplainableSearchScript) {
                leafScript.setDocument(docId);
                scorer.docid = docId;
                scorer.score = subQueryScore.getValue();
                exp = ((ExplainableSearchScript) leafScript).explain(subQueryScore);
            } else {
                double score = score(docId, subQueryScore.getValue());
                String explanation = "script score function, computed with script:\"" + sScript + "\"";
                if (sScript.getParams() != null) {
                    explanation += " and parameters: \n" + sScript.getParams().toString();
                }
                Explanation scoreExp = Explanation.match(subQueryScore.getValue(), "_score: ", subQueryScore);
                return Explanation.match(CombineFunction.toFloat(score), explanation, scoreExp);
            }
            return exp;
        }
    };
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) Explanation(org.apache.lucene.search.Explanation) GeneralScriptException(org.elasticsearch.script.GeneralScriptException) ExplainableSearchScript(org.elasticsearch.script.ExplainableSearchScript)

Example 3 with GeneralScriptException

use of org.elasticsearch.script.GeneralScriptException in project elasticsearch by elastic.

the class ScriptScoreFunctionTests method testScriptScoresReturnsNaN.

/**
     * Tests https://github.com/elastic/elasticsearch/issues/2426
     */
public void testScriptScoresReturnsNaN() throws IOException {
    // script that always returns NaN
    ScoreFunction scoreFunction = new ScriptScoreFunction(new Script("Double.NaN"), new SearchScript() {

        @Override
        public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
            return new AbstractDoubleSearchScript() {

                @Override
                public double runAsDouble() {
                    return Double.NaN;
                }

                @Override
                public void setDocument(int doc) {
                // do nothing: we are a fake with no lookup
                }
            };
        }

        @Override
        public boolean needsScores() {
            return false;
        }
    });
    LeafScoreFunction leafScoreFunction = scoreFunction.getLeafScoreFunction(null);
    GeneralScriptException expected = expectThrows(GeneralScriptException.class, () -> {
        leafScoreFunction.score(randomInt(), randomFloat());
    });
    assertTrue(expected.getMessage().contains("returned NaN"));
}
Also used : Script(org.elasticsearch.script.Script) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) GeneralScriptException(org.elasticsearch.script.GeneralScriptException) IOException(java.io.IOException)

Aggregations

GeneralScriptException (org.elasticsearch.script.GeneralScriptException)3 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Expression (org.apache.lucene.expressions.Expression)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Explanation (org.apache.lucene.search.Explanation)1 AbstractDoubleSearchScript (org.elasticsearch.script.AbstractDoubleSearchScript)1 CompiledScript (org.elasticsearch.script.CompiledScript)1 ExplainableSearchScript (org.elasticsearch.script.ExplainableSearchScript)1 Script (org.elasticsearch.script.Script)1 SearchScript (org.elasticsearch.script.SearchScript)1