Search in sources :

Example 1 with ExplainableScoreScript

use of org.opensearch.script.ExplainableScoreScript in project OpenSearch by opensearch-project.

the class ScriptScoreFunction method getLeafScoreFunction.

@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) throws IOException {
    final ScoreScript leafScript = script.newInstance(ctx);
    final CannedScorer scorer = new CannedScorer();
    leafScript.setScorer(scorer);
    leafScript._setIndexName(indexName);
    leafScript._setShard(shardId);
    leafScript._setIndexVersion(indexVersion);
    return new LeafScoreFunction() {

        @Override
        public double score(int docId, float subQueryScore) throws IOException {
            leafScript.setDocument(docId);
            scorer.docid = docId;
            scorer.score = subQueryScore;
            double result = leafScript.execute(null);
            if (result < 0f) {
                throw new IllegalArgumentException("script score function must not produce negative scores, but got: [" + result + "]");
            }
            return result;
        }

        @Override
        public Explanation explainScore(int docId, Explanation subQueryScore) throws IOException {
            Explanation exp;
            if (leafScript instanceof ExplainableScoreScript) {
                leafScript.setDocument(docId);
                scorer.docid = docId;
                scorer.score = subQueryScore.getValue().floatValue();
                exp = ((ExplainableScoreScript) leafScript).explain(subQueryScore, functionName);
            } else {
                double score = score(docId, subQueryScore.getValue().floatValue());
                // info about params already included in sScript
                String explanation = "script score function" + Functions.nameOrEmptyFunc(functionName) + ", computed with script:\"" + sScript + "\"";
                Explanation scoreExp = Explanation.match(subQueryScore.getValue(), "_score: ", subQueryScore);
                return Explanation.match((float) score, explanation, scoreExp);
            }
            return exp;
        }
    };
}
Also used : ScoreScript(org.opensearch.script.ScoreScript) ExplainableScoreScript(org.opensearch.script.ExplainableScoreScript) Explanation(org.apache.lucene.search.Explanation) ExplainableScoreScript(org.opensearch.script.ExplainableScoreScript)

Aggregations

Explanation (org.apache.lucene.search.Explanation)1 ExplainableScoreScript (org.opensearch.script.ExplainableScoreScript)1 ScoreScript (org.opensearch.script.ScoreScript)1