Search in sources :

Example 6 with ScriptScoreQuery

use of org.opensearch.common.lucene.search.function.ScriptScoreQuery in project OpenSearch by opensearch-project.

the class ScriptScoreQueryTests method testExplainWithName.

public void testExplainWithName() throws IOException {
    Script script = new Script("script using explain");
    ScoreScript.LeafFactory factory = newFactory(script, true, explanation -> {
        assertNotNull(explanation);
        explanation.set("this explains the score");
        return 1.0;
    });
    ScriptScoreQuery query = new ScriptScoreQuery(Queries.newMatchAllQuery(), "query1", script, factory, null, "index", 0, Version.CURRENT);
    Weight weight = query.createWeight(searcher, ScoreMode.COMPLETE, 1.0f);
    Explanation explanation = weight.explain(leafReaderContext, 0);
    assertNotNull(explanation);
    assertThat(explanation.getDescription(), equalTo("this explains the score"));
    assertThat(explanation.getValue(), equalTo(1.0));
    assertThat(explanation.getDetails(), arrayWithSize(1));
    assertThat(explanation.getDetails()[0].getDescription(), equalTo("*:* (_name: query1)"));
}
Also used : ScoreScript(org.opensearch.script.ScoreScript) Script(org.opensearch.script.Script) ScoreScript(org.opensearch.script.ScoreScript) ScriptScoreQuery(org.opensearch.common.lucene.search.function.ScriptScoreQuery) Explanation(org.apache.lucene.search.Explanation) Weight(org.apache.lucene.search.Weight)

Aggregations

ScriptScoreQuery (org.opensearch.common.lucene.search.function.ScriptScoreQuery)6 ScoreScript (org.opensearch.script.ScoreScript)6 Script (org.opensearch.script.Script)5 Explanation (org.apache.lucene.search.Explanation)4 Weight (org.apache.lucene.search.Weight)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Query (org.apache.lucene.search.Query)1 OpenSearchException (org.opensearch.OpenSearchException)1 AbstractQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder)1 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1