Search in sources :

Example 1 with ScriptScoreQuery

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

the class ScriptScoreQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (context.allowExpensiveQueries() == false) {
        throw new OpenSearchException("[script score] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false.");
    }
    ScoreScript.Factory factory = context.compile(script, ScoreScript.CONTEXT);
    ScoreScript.LeafFactory scoreScriptFactory = factory.newFactory(script.getParams(), context.lookup());
    final QueryBuilder queryBuilder = this.query;
    Query query = queryBuilder.toQuery(context);
    return new ScriptScoreQuery(query, queryBuilder.queryName(), script, scoreScriptFactory, minScore, context.index().getName(), context.getShardId(), context.indexVersionCreated());
}
Also used : ScoreScript(org.opensearch.script.ScoreScript) Query(org.apache.lucene.search.Query) ScriptScoreQuery(org.opensearch.common.lucene.search.function.ScriptScoreQuery) ScriptScoreQuery(org.opensearch.common.lucene.search.function.ScriptScoreQuery) OpenSearchException(org.opensearch.OpenSearchException) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder)

Example 2 with ScriptScoreQuery

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

the class ScriptScoreQueryTests method testExplainDefault.

public void testExplainDefault() throws IOException {
    Script script = new Script("script without setting explanation");
    ScoreScript.LeafFactory factory = newFactory(script, true, explanation -> 1.5);
    ScriptScoreQuery query = new ScriptScoreQuery(Queries.newMatchAllQuery(), script, factory, null, "index", 0, Version.CURRENT);
    Weight weight = query.createWeight(searcher, ScoreMode.COMPLETE, 1.0f);
    Explanation explanation = weight.explain(leafReaderContext, 0);
    assertNotNull(explanation);
    String description = explanation.getDescription();
    assertThat(description, containsString("script score function, computed with script:"));
    assertThat(description, containsString("script without setting explanation"));
    assertThat(explanation.getDetails(), arrayWithSize(1));
    assertThat(explanation.getDetails()[0].getDescription(), containsString("_score"));
    assertThat(explanation.getValue(), equalTo(1.5f));
}
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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Weight(org.apache.lucene.search.Weight)

Example 3 with ScriptScoreQuery

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

the class ScriptScoreQueryTests method testScriptScoreErrorOnNegativeScore.

public void testScriptScoreErrorOnNegativeScore() {
    Script script = new Script("script that returns a negative score");
    ScoreScript.LeafFactory factory = newFactory(script, false, explanation -> -1000.0);
    ScriptScoreQuery query = new ScriptScoreQuery(Queries.newMatchAllQuery(), script, factory, null, "index", 0, Version.CURRENT);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> searcher.search(query, 1));
    assertTrue(e.getMessage().contains("Must be a non-negative score!"));
}
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)

Example 4 with ScriptScoreQuery

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

the class ScriptScoreQueryTests method testExplainDefaultNoScore.

public void testExplainDefaultNoScore() throws IOException {
    Script script = new Script("script without setting explanation and no score");
    ScoreScript.LeafFactory factory = newFactory(script, false, explanation -> 2.0);
    ScriptScoreQuery query = new ScriptScoreQuery(Queries.newMatchAllQuery(), script, factory, null, "index", 0, Version.CURRENT);
    Weight weight = query.createWeight(searcher, ScoreMode.COMPLETE, 1.0f);
    Explanation explanation = weight.explain(leafReaderContext, 0);
    assertNotNull(explanation);
    String description = explanation.getDescription();
    assertThat(description, containsString("script score function, computed with script:"));
    assertThat(description, containsString("script without setting explanation and no score"));
    assertThat(explanation.getDetails(), arrayWithSize(0));
    assertThat(explanation.getValue(), equalTo(2.0f));
}
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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Weight(org.apache.lucene.search.Weight)

Example 5 with ScriptScoreQuery

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

the class ScriptScoreQueryTests method testExplain.

public void testExplain() 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(), 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));
}
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