Search in sources :

Example 1 with ScriptQueryBuilder

use of org.opensearch.index.query.ScriptQueryBuilder in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testImplicitlySetDefaultScriptLang.

public void testImplicitlySetDefaultScriptLang() throws Exception {
    addQueryFieldMappings();
    XContentBuilder query = jsonBuilder();
    query.startObject();
    query.startObject("script");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("source", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
    BytesRef querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
        try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
            // Query builder's content is stored via BinaryFieldMapper, which has a custom encoding
            // to encode multiple binary values into a single binary doc values field.
            // This is the reason we need to first need to read the number of values and
            // then the length of the field value in bytes.
            input.readVInt();
            input.readVInt();
            ScriptQueryBuilder queryBuilder = (ScriptQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
            assertEquals(Script.DEFAULT_SCRIPT_LANG, queryBuilder.script().getLang());
        }
    }
    query = jsonBuilder();
    query.startObject();
    query.startObject("function_score");
    query.startArray("functions");
    query.startObject();
    query.startObject("script_score");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("source", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    query.endArray();
    query.endObject();
    query.endObject();
    doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
    querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
        try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
            input.readVInt();
            input.readVInt();
            FunctionScoreQueryBuilder queryBuilder = (FunctionScoreQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
            ScriptScoreFunctionBuilder function = (ScriptScoreFunctionBuilder) queryBuilder.filterFunctionBuilders()[0].getScoreFunction();
            assertEquals(Script.DEFAULT_SCRIPT_LANG, function.getScript().getLang());
        }
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SourceToParse(org.opensearch.index.mapper.SourceToParse) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 2 with ScriptQueryBuilder

use of org.opensearch.index.query.ScriptQueryBuilder in project OpenSearch by opensearch-project.

the class SearchIT method testMultiSearch_failure.

public void testMultiSearch_failure() throws Exception {
    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
    SearchRequest searchRequest1 = new SearchRequest("index1");
    searchRequest1.source().query(new ScriptQueryBuilder(new Script(ScriptType.INLINE, "invalid", "code", Collections.emptyMap())));
    multiSearchRequest.add(searchRequest1);
    SearchRequest searchRequest2 = new SearchRequest("index2");
    searchRequest2.source().query(new ScriptQueryBuilder(new Script(ScriptType.INLINE, "invalid", "code", Collections.emptyMap())));
    multiSearchRequest.add(searchRequest2);
    MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync);
    assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L));
    assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(2));
    assertThat(multiSearchResponse.getResponses()[0].isFailure(), Matchers.is(true));
    assertThat(multiSearchResponse.getResponses()[0].getFailure().getMessage(), containsString("search_phase_execution_exception"));
    assertThat(multiSearchResponse.getResponses()[0].getResponse(), nullValue());
    assertThat(multiSearchResponse.getResponses()[1].isFailure(), Matchers.is(true));
    assertThat(multiSearchResponse.getResponses()[1].getFailure().getMessage(), containsString("search_phase_execution_exception"));
    assertThat(multiSearchResponse.getResponses()[1].getResponse(), nullValue());
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) Script(org.opensearch.script.Script) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder)

Aggregations

ScriptQueryBuilder (org.opensearch.index.query.ScriptQueryBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 BytesRef (org.apache.lucene.util.BytesRef)1 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)1 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 BytesArray (org.opensearch.common.bytes.BytesArray)1 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)1 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)1 StreamInput (org.opensearch.common.io.stream.StreamInput)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)1 SourceToParse (org.opensearch.index.mapper.SourceToParse)1 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)1 BoostingQueryBuilder (org.opensearch.index.query.BoostingQueryBuilder)1 ConstantScoreQueryBuilder (org.opensearch.index.query.ConstantScoreQueryBuilder)1 DisMaxQueryBuilder (org.opensearch.index.query.DisMaxQueryBuilder)1 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1