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());
}
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));
}
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!"));
}
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));
}
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));
}
Aggregations