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));
}
}
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;
}
};
}
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"));
}
Aggregations