use of org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder in project OpenSearch by opensearch-project.
the class ScriptScoreQueryBuilderTests method testMustRewrite.
@Override
public void testMustRewrite() throws IOException {
QueryShardContext context = createShardContext();
context.setAllowUnmappedFields(true);
TermQueryBuilder termQueryBuilder = new TermQueryBuilder("unmapped_field", "foo");
String scriptStr = "1";
Script script = new Script(ScriptType.INLINE, MockScriptEngine.NAME, scriptStr, Collections.emptyMap());
ScriptScoreQueryBuilder scriptScoreQueryBuilder = new ScriptScoreQueryBuilder(termQueryBuilder, script);
IllegalStateException e = expectThrows(IllegalStateException.class, () -> scriptScoreQueryBuilder.toQuery(context));
assertEquals("Rewrite first", e.getMessage());
}
use of org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder in project OpenSearch by opensearch-project.
the class ScriptScoreQueryBuilderTests method testCacheability.
/**
* Check that this query is cacheable
*/
@Override
public void testCacheability() throws IOException {
Script script = new Script(ScriptType.INLINE, MockScriptEngine.NAME, "1", Collections.emptyMap());
ScriptScoreQueryBuilder queryBuilder = new ScriptScoreQueryBuilder(new TermQueryBuilder(KEYWORD_FIELD_NAME, "value"), script);
QueryShardContext context = createShardContext();
QueryBuilder rewriteQuery = rewriteQuery(queryBuilder, new QueryShardContext(context));
assertNotNull(rewriteQuery.toQuery(context));
assertTrue("query should be cacheable: " + queryBuilder.toString(), context.isCacheable());
}
use of org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder in project OpenSearch by opensearch-project.
the class ScriptScoreQueryBuilderTests method testFromJson.
public void testFromJson() throws IOException {
String json = "{\n" + " \"script_score\" : {\n" + " \"query\" : { \"match_all\" : {} },\n" + " \"script\" : {\n" + " \"source\" : \"doc['field'].value\" \n" + " },\n" + " \"min_score\" : 2.0\n" + " }\n" + "}";
ScriptScoreQueryBuilder parsed = (ScriptScoreQueryBuilder) parseQuery(json);
assertEquals(json, 2, parsed.getMinScore(), 0.0001);
}
use of org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder in project OpenSearch by opensearch-project.
the class ScriptScoreQueryBuilderTests method testIllegalArguments.
public void testIllegalArguments() {
String scriptStr = "1";
Script script = new Script(ScriptType.INLINE, MockScriptEngine.NAME, scriptStr, Collections.emptyMap());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ScriptScoreQueryBuilder(matchAllQuery(), null));
assertEquals("script_score: script must not be null", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> new ScriptScoreQueryBuilder(null, script));
assertEquals("script_score: query must not be null", e.getMessage());
}
use of org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder in project OpenSearch by opensearch-project.
the class ScriptScoreQueryBuilderTests method testDisallowExpensiveQueries.
public void testDisallowExpensiveQueries() {
QueryShardContext queryShardContext = mock(QueryShardContext.class);
when(queryShardContext.allowExpensiveQueries()).thenReturn(false);
ScriptScoreQueryBuilder queryBuilder = doCreateTestQueryBuilder();
OpenSearchException e = expectThrows(OpenSearchException.class, () -> queryBuilder.toQuery(queryShardContext));
assertEquals("[script score] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage());
}
Aggregations