Search in sources :

Example 1 with WeightFactorFunction

use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.

the class FunctionScoreTests method testExplanationAndScoreEqualsEvenIfNoFunctionMatches.

public void testExplanationAndScoreEqualsEvenIfNoFunctionMatches() throws IOException {
    IndexSearcher localSearcher = newSearcher(reader);
    CombineFunction combineFunction = randomFrom(new CombineFunction[] { CombineFunction.SUM, CombineFunction.AVG, CombineFunction.MIN, CombineFunction.MAX, CombineFunction.MULTIPLY, CombineFunction.REPLACE });
    // check for document that has no matching function
    FunctionScoreQuery query = new FunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), new FilterScoreFunction(new TermQuery(new Term("_uid", "2")), new WeightFactorFunction(10)), combineFunction, Float.NEGATIVE_INFINITY, Float.MAX_VALUE);
    TopDocs searchResult = localSearcher.search(query, 1);
    Explanation explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
    assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
    // check for document that has a matching function
    query = new FunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), new FilterScoreFunction(new TermQuery(new Term("_uid", "1")), new WeightFactorFunction(10)), combineFunction, Float.NEGATIVE_INFINITY, Float.MAX_VALUE);
    searchResult = localSearcher.search(query, 1);
    explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
    assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) WeightFactorFunction(org.opensearch.common.lucene.search.function.WeightFactorFunction) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) Explanation(org.apache.lucene.search.Explanation) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) Term(org.apache.lucene.index.Term)

Example 2 with WeightFactorFunction

use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.

the class FunctionScoreTests method testExplainFunctionScoreQueryWithName.

public void testExplainFunctionScoreQueryWithName() throws IOException {
    Explanation functionExplanation = getFunctionScoreExplanation(searcher, RANDOM_SCORE_FUNCTION_NAMED);
    checkFunctionScoreExplanation(functionExplanation, "random score function (seed: 0, field: test, _name: func1)");
    assertThat(functionExplanation.getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, FIELD_VALUE_FACTOR_FUNCTION_NAMED);
    checkFunctionScoreExplanation(functionExplanation, "field value function(_name: func1): ln(doc['test'].value?:1.0 * factor=1.0)");
    assertThat(functionExplanation.getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, GAUSS_DECAY_FUNCTION_NAMED);
    checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = exp(-0.5*pow(MAX[Math.max(Math.abs" + "(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)],2.0)/0.21714724095162594, _name: func1)\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, EXP_DECAY_FUNCTION_NAMED);
    checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = exp(- MAX[Math.max(Math.abs(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)] * 2.3025850929940455, _name: func1)\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, LIN_DECAY_FUNCTION_NAMED);
    checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = max(0.0, ((1.1111111111111112" + " - MAX[Math.max(Math.abs(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)])/1.1111111111111112, _name: func1)\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, new WeightFactorFunction(4, RANDOM_SCORE_FUNCTION_NAMED));
    checkFunctionScoreExplanation(functionExplanation, "product of:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), endsWith("random score function (seed: 0, field: test, _name: func1)\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[1].toString(), equalTo("4.0 = weight\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    assertThat(functionExplanation.getDetails()[0].getDetails()[1].getDetails().length, equalTo(0));
}
Also used : WeightFactorFunction(org.opensearch.common.lucene.search.function.WeightFactorFunction) Explanation(org.apache.lucene.search.Explanation)

Example 3 with WeightFactorFunction

use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.

the class FunctionScoreQueryBuilderTests method testWeight1fStillProducesWeightFunction.

public void testWeight1fStillProducesWeightFunction() throws IOException {
    String queryString = Strings.toString(jsonBuilder().startObject().startObject("function_score").startArray("functions").startObject().startObject("field_value_factor").field("field", INT_FIELD_NAME).endObject().field("weight", 1.0).endObject().endArray().endObject().endObject());
    QueryBuilder query = parseQuery(queryString);
    assertThat(query, instanceOf(FunctionScoreQueryBuilder.class));
    FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) query;
    assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(FieldValueFactorFunctionBuilder.class));
    FieldValueFactorFunctionBuilder fieldValueFactorFunctionBuilder = (FieldValueFactorFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
    assertThat(fieldValueFactorFunctionBuilder.fieldName(), equalTo(INT_FIELD_NAME));
    assertThat(fieldValueFactorFunctionBuilder.factor(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_FACTOR));
    assertThat(fieldValueFactorFunctionBuilder.modifier(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_MODIFIER));
    assertThat(fieldValueFactorFunctionBuilder.getWeight(), equalTo(1f));
    assertThat(fieldValueFactorFunctionBuilder.missing(), nullValue());
    Query luceneQuery = query.toQuery(createShardContext());
    assertThat(luceneQuery, instanceOf(FunctionScoreQuery.class));
    FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) luceneQuery;
    assertThat(functionScoreQuery.getFunctions().length, equalTo(1));
    assertThat(functionScoreQuery.getFunctions()[0], instanceOf(WeightFactorFunction.class));
    WeightFactorFunction weightFactorFunction = (WeightFactorFunction) functionScoreQuery.getFunctions()[0];
    assertThat(weightFactorFunction.getWeight(), equalTo(1.0f));
    assertThat(weightFactorFunction.getScoreFunction(), instanceOf(FieldValueFactorFunction.class));
}
Also used : WeightFactorFunction(org.opensearch.common.lucene.search.function.WeightFactorFunction) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) QueryBuilders.matchAllQuery(org.opensearch.index.query.QueryBuilders.matchAllQuery) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) QueryBuilders.functionScoreQuery(org.opensearch.index.query.QueryBuilders.functionScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) Matchers.containsString(org.hamcrest.Matchers.containsString) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) RandomQueryBuilder(org.opensearch.index.query.RandomQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 4 with WeightFactorFunction

use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.

the class FunctionScoreTests method testSimpleWeightedFunction.

public void testSimpleWeightedFunction() throws IOException, ExecutionException, InterruptedException {
    int numFunctions = randomIntBetween(1, 3);
    float[] weights = randomPositiveFloats(numFunctions);
    double[] scores = randomPositiveDoubles(numFunctions);
    ScoreFunctionStub[] scoreFunctionStubs = new ScoreFunctionStub[numFunctions];
    for (int i = 0; i < numFunctions; i++) {
        scoreFunctionStubs[i] = new ScoreFunctionStub(scores[i]);
    }
    WeightFactorFunction[] weightFunctionStubs = new WeightFactorFunction[numFunctions];
    for (int i = 0; i < numFunctions; i++) {
        weightFunctionStubs[i] = new WeightFactorFunction(weights[i], scoreFunctionStubs[i]);
    }
    FunctionScoreQuery functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.MULTIPLY, CombineFunction.REPLACE, weightFunctionStubs);
    TopDocs topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
    float scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double score = 1;
    for (int i = 0; i < weights.length; i++) {
        score *= weights[i] * scores[i];
    }
    assertThat(scoreWithWeight / (float) score, is(1f));
    float explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.SUM, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double sum = 0;
    for (int i = 0; i < weights.length; i++) {
        sum += weights[i] * scores[i];
    }
    assertThat(scoreWithWeight / (float) sum, is(1f));
    explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.AVG, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double norm = 0;
    sum = 0;
    for (int i = 0; i < weights.length; i++) {
        norm += weights[i];
        sum += weights[i] * scores[i];
    }
    assertThat(scoreWithWeight / (float) (sum / norm), is(1f));
    explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.MIN, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double min = Double.POSITIVE_INFINITY;
    for (int i = 0; i < weights.length; i++) {
        min = Math.min(min, weights[i] * scores[i]);
    }
    assertThat(scoreWithWeight / (float) min, is(1f));
    explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.MAX, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double max = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < weights.length; i++) {
        max = Math.max(max, weights[i] * scores[i]);
    }
    assertThat(scoreWithWeight / (float) max, is(1f));
    explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) WeightFactorFunction(org.opensearch.common.lucene.search.function.WeightFactorFunction) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery)

Example 5 with WeightFactorFunction

use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.

the class FunctionScoreTests method testWeightOnlyCreatesBoostFunction.

public void testWeightOnlyCreatesBoostFunction() throws IOException {
    FunctionScoreQuery filtersFunctionScoreQueryWithWeights = new FunctionScoreQuery(new MatchAllDocsQuery(), new WeightFactorFunction(2), CombineFunction.MULTIPLY, 0.0f, 100);
    TopDocs topDocsWithWeights = searcher.search(filtersFunctionScoreQueryWithWeights, 1);
    float score = topDocsWithWeights.scoreDocs[0].score;
    assertThat(score, equalTo(2.0f));
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) WeightFactorFunction(org.opensearch.common.lucene.search.function.WeightFactorFunction) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

WeightFactorFunction (org.opensearch.common.lucene.search.function.WeightFactorFunction)6 FunctionScoreQuery (org.opensearch.common.lucene.search.function.FunctionScoreQuery)4 TopDocs (org.apache.lucene.search.TopDocs)3 Explanation (org.apache.lucene.search.Explanation)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 FilterScoreFunction (org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction)2 IOException (java.io.IOException)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Term (org.apache.lucene.index.Term)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Query (org.apache.lucene.search.Query)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 CombineFunction (org.opensearch.common.lucene.search.function.CombineFunction)1 FieldValueFactorFunction (org.opensearch.common.lucene.search.function.FieldValueFactorFunction)1 LeafScoreFunction (org.opensearch.common.lucene.search.function.LeafScoreFunction)1 RandomScoreFunction (org.opensearch.common.lucene.search.function.RandomScoreFunction)1 ScoreFunction (org.opensearch.common.lucene.search.function.ScoreFunction)1 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)1