Search in sources :

Example 1 with ScriptScoreFunctionBuilder

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

the class MoreExpressionIT method testScore.

public void testScore() throws Exception {
    createIndex("test");
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test").setId("1").setSource("text", "hello goodbye"), client().prepareIndex("test").setId("2").setSource("text", "hello hello hello goodbye"), client().prepareIndex("test").setId("3").setSource("text", "hello hello goodebye"));
    ScriptScoreFunctionBuilder score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap()));
    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE));
    // make sure DF is consistent
    req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    SearchResponse rsp = req.get();
    assertSearchResponse(rsp);
    SearchHits hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits().value);
    assertEquals("1", hits.getAt(0).getId());
    assertEquals("3", hits.getAt(1).getId());
    assertEquals("2", hits.getAt(2).getId());
    req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE));
    score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap()));
    req.addAggregation(AggregationBuilders.max("max_score").script((score).getScript()));
    // make sure DF is consistent
    req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    rsp = req.get();
    assertSearchResponse(rsp);
}
Also used : PipelineAggregatorBuilders.bucketScript(org.opensearch.search.aggregations.PipelineAggregatorBuilders.bucketScript) Script(org.opensearch.script.Script) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchHits(org.opensearch.search.SearchHits) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with ScriptScoreFunctionBuilder

use of org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder 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 3 with ScriptScoreFunctionBuilder

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

the class TransportTwoNodesSearchIT method testFailedMultiSearchWithWrongQueryWithFunctionScore.

public void testFailedMultiSearchWithWrongQueryWithFunctionScore() throws Exception {
    prepareData();
    logger.info("Start Testing failed multi search with a wrong query");
    MultiSearchResponse response = client().prepareMultiSearch().add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1), new ScriptScoreFunctionBuilder(new Script(ScriptType.INLINE, "bar", "foo", Collections.emptyMap()))))).add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2))).add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())).get();
    assertThat(response.getResponses().length, equalTo(3));
    assertThat(response.getResponses()[0].getFailureMessage(), notNullValue());
    assertThat(response.getResponses()[1].getFailureMessage(), nullValue());
    assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1));
    assertThat(response.getResponses()[2].getFailureMessage(), nullValue());
    assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10));
    logger.info("Done Testing failed search");
}
Also used : MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) Script(org.opensearch.script.Script) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder)

Example 4 with ScriptScoreFunctionBuilder

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

the class SearchModule method registerScoreFunctions.

private void registerScoreFunctions(List<SearchPlugin> plugins) {
    // ScriptScoreFunctionBuilder has it own named writable because of a new script_score query
    namedWriteables.add(new NamedWriteableRegistry.Entry(ScriptScoreFunctionBuilder.class, ScriptScoreFunctionBuilder.NAME, ScriptScoreFunctionBuilder::new));
    registerScoreFunction(new ScoreFunctionSpec<>(ScriptScoreFunctionBuilder.NAME, ScriptScoreFunctionBuilder::new, ScriptScoreFunctionBuilder::fromXContent));
    registerScoreFunction(new ScoreFunctionSpec<>(GaussDecayFunctionBuilder.NAME, GaussDecayFunctionBuilder::new, GaussDecayFunctionBuilder.PARSER));
    registerScoreFunction(new ScoreFunctionSpec<>(LinearDecayFunctionBuilder.NAME, LinearDecayFunctionBuilder::new, LinearDecayFunctionBuilder.PARSER));
    registerScoreFunction(new ScoreFunctionSpec<>(ExponentialDecayFunctionBuilder.NAME, ExponentialDecayFunctionBuilder::new, ExponentialDecayFunctionBuilder.PARSER));
    registerScoreFunction(new ScoreFunctionSpec<>(RandomScoreFunctionBuilder.NAME, RandomScoreFunctionBuilder::new, RandomScoreFunctionBuilder::fromXContent));
    registerScoreFunction(new ScoreFunctionSpec<>(FieldValueFactorFunctionBuilder.NAME, FieldValueFactorFunctionBuilder::new, FieldValueFactorFunctionBuilder::fromXContent));
    // weight doesn't have its own parser, so every function supports it out of the box.
    // Can be a single function too when not associated to any other function, which is why it needs to be registered manually here.
    namedWriteables.add(new NamedWriteableRegistry.Entry(ScoreFunctionBuilder.class, WeightBuilder.NAME, WeightBuilder::new));
    registerFromPlugin(plugins, SearchPlugin::getScoreFunctions, this::registerScoreFunction);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) SearchPlugin(org.opensearch.plugins.SearchPlugin) ScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScoreFunctionBuilder) RandomScoreFunctionBuilder(org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) Entry(org.opensearch.common.io.stream.NamedWriteableRegistry.Entry) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder)

Aggregations

ScriptScoreFunctionBuilder (org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder)4 Script (org.opensearch.script.Script)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 BytesRef (org.apache.lucene.util.BytesRef)1 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)1 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 BytesArray (org.opensearch.common.bytes.BytesArray)1 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)1 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)1 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)1 Entry (org.opensearch.common.io.stream.NamedWriteableRegistry.Entry)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