Search in sources :

Example 1 with ExplainableSearchScript

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;
        }
    };
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) Explanation(org.apache.lucene.search.Explanation) GeneralScriptException(org.elasticsearch.script.GeneralScriptException) ExplainableSearchScript(org.elasticsearch.script.ExplainableSearchScript)

Aggregations

Explanation (org.apache.lucene.search.Explanation)1 ExplainableSearchScript (org.elasticsearch.script.ExplainableSearchScript)1 GeneralScriptException (org.elasticsearch.script.GeneralScriptException)1 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)1