Search in sources :

Example 1 with ScriptScoreQueryBuilder

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());
}
Also used : Script(org.opensearch.script.Script) ScriptScoreQueryBuilder(org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder)

Example 2 with ScriptScoreQueryBuilder

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());
}
Also used : Script(org.opensearch.script.Script) ScriptScoreQueryBuilder(org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder) ScriptScoreQueryBuilder(org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder)

Example 3 with ScriptScoreQueryBuilder

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);
}
Also used : ScriptScoreQueryBuilder(org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder)

Example 4 with ScriptScoreQueryBuilder

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());
}
Also used : Script(org.opensearch.script.Script) ScriptScoreQueryBuilder(org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder)

Example 5 with ScriptScoreQueryBuilder

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());
}
Also used : ScriptScoreQueryBuilder(org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

ScriptScoreQueryBuilder (org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder)7 Script (org.opensearch.script.Script)5 OpenSearchException (org.opensearch.OpenSearchException)1