use of org.elasticsearch.script.ExplainableSearchScript 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;
}
};
}
Aggregations