Search in sources :

Example 1 with ScoreFunction

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

the class FunctionScoreTests method testFilterFunctionScoreHashCodeAndEquals.

public void testFilterFunctionScoreHashCodeAndEquals() {
    CombineFunction combineFunction = randomFrom(CombineFunction.values());
    ScoreFunction scoreFunction = new DummyScoreFunction(combineFunction);
    Float minScore = randomBoolean() ? null : 1.0f;
    Float maxBoost = randomBoolean() ? Float.POSITIVE_INFINITY : randomFloat();
    FilterScoreFunction function = new FilterScoreFunction(new TermQuery(new Term("filter", "query")), scoreFunction);
    FunctionScoreQuery q = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction, minScore, maxBoost);
    FunctionScoreQuery q1 = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction, minScore, maxBoost);
    assertEquals(q, q);
    assertEquals(q.hashCode(), q.hashCode());
    assertEquals(q, q1);
    assertEquals(q.hashCode(), q1.hashCode());
    FunctionScoreQuery diffCombineFunc = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction == CombineFunction.AVG ? CombineFunction.MAX : CombineFunction.AVG, minScore, maxBoost);
    FunctionScoreQuery diffQuery = new FunctionScoreQuery(new TermQuery(new Term("foo", "baz")), function, combineFunction, minScore, maxBoost);
    FunctionScoreQuery diffMaxBoost = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction, minScore, maxBoost == 1.0f ? 0.9f : 1.0f);
    FunctionScoreQuery diffMinScore = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction, minScore == null ? 0.9f : null, maxBoost);
    FilterScoreFunction otherFunc = new FilterScoreFunction(new TermQuery(new Term("filter", "other_query")), scoreFunction);
    FunctionScoreQuery diffFunc = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), randomFrom(ScoreMode.values()), randomBoolean() ? new ScoreFunction[] { function, otherFunc } : new ScoreFunction[] { otherFunc }, combineFunction, minScore, maxBoost);
    FunctionScoreQuery[] queries = new FunctionScoreQuery[] { diffQuery, diffMaxBoost, diffMinScore, diffFunc, q, diffCombineFunc };
    final int numIters = randomIntBetween(20, 100);
    for (int i = 0; i < numIters; i++) {
        FunctionScoreQuery left = randomFrom(queries);
        FunctionScoreQuery right = randomFrom(queries);
        if (left == right) {
            assertEquals(left, right);
            assertEquals(left.hashCode(), right.hashCode());
        } else {
            assertNotEquals(left + " == " + right, left, right);
        }
    }
}
Also used : CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) Term(org.apache.lucene.index.Term) LeafScoreFunction(org.opensearch.common.lucene.search.function.LeafScoreFunction) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Example 2 with ScoreFunction

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

the class FunctionScoreTests method testFunctionScoreHashCodeAndEquals.

public void testFunctionScoreHashCodeAndEquals() {
    Float minScore = randomBoolean() ? null : 1.0f;
    CombineFunction combineFunction = randomFrom(CombineFunction.values());
    float maxBoost = randomBoolean() ? Float.POSITIVE_INFINITY : randomFloat();
    ScoreFunction function = new DummyScoreFunction(combineFunction);
    FunctionScoreQuery q = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction, minScore, maxBoost);
    FunctionScoreQuery q1 = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction, minScore, maxBoost);
    assertEquals(q, q);
    assertEquals(q.hashCode(), q.hashCode());
    assertEquals(q, q1);
    assertEquals(q.hashCode(), q1.hashCode());
    FunctionScoreQuery diffQuery = new FunctionScoreQuery(new TermQuery(new Term("foo", "baz")), function, combineFunction, minScore, maxBoost);
    FunctionScoreQuery diffMinScore = new FunctionScoreQuery(q.getSubQuery(), function, combineFunction, minScore == null ? 1.0f : null, maxBoost);
    ScoreFunction otherFunction = new DummyScoreFunction(combineFunction);
    FunctionScoreQuery diffFunction = new FunctionScoreQuery(q.getSubQuery(), otherFunction, combineFunction, minScore, maxBoost);
    FunctionScoreQuery diffMaxBoost = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, combineFunction, minScore, maxBoost == 1.0f ? 0.9f : 1.0f);
    FunctionScoreQuery[] queries = new FunctionScoreQuery[] { diffFunction, diffMinScore, diffQuery, q, diffMaxBoost };
    final int numIters = randomIntBetween(20, 100);
    for (int i = 0; i < numIters; i++) {
        FunctionScoreQuery left = randomFrom(queries);
        FunctionScoreQuery right = randomFrom(queries);
        if (left == right) {
            assertEquals(left, right);
            assertEquals(left.hashCode(), right.hashCode());
        } else {
            assertNotEquals(left + " == " + right, left, right);
        }
    }
}
Also used : CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) Term(org.apache.lucene.index.Term) LeafScoreFunction(org.opensearch.common.lucene.search.function.LeafScoreFunction) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Example 3 with ScoreFunction

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

the class FunctionScoreTests method testExceptionOnNegativeScores.

public void testExceptionOnNegativeScores() {
    IndexSearcher localSearcher = new IndexSearcher(reader);
    TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));
    // test that field_value_factor function throws an exception on negative scores
    FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.NONE;
    final ScoreFunction fvfFunction = new FieldValueFactorFunction(FIELD, -10, modifier, 1.0, new IndexNumericFieldDataStub());
    FunctionScoreQuery fsQuery1 = new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
    assertThat(exc.getMessage(), containsString("field value function must not produce negative scores"));
    assertThat(exc.getMessage(), not(containsString("consider using ln1p or ln2p instead of ln to avoid negative scores")));
    assertThat(exc.getMessage(), not(containsString("consider using log1p or log2p instead of log to avoid negative scores")));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) Term(org.apache.lucene.index.Term) LeafScoreFunction(org.opensearch.common.lucene.search.function.LeafScoreFunction) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Example 4 with ScoreFunction

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

the class FunctionScoreTests method testExceptionOnLogNegativeScores.

public void testExceptionOnLogNegativeScores() {
    IndexSearcher localSearcher = new IndexSearcher(reader);
    TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));
    // test that field_value_factor function using modifier log throws an exception on negative scores
    FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.LOG;
    final ScoreFunction fvfFunction = new FieldValueFactorFunction(FIELD, 0.5f, modifier, 1.0, new IndexNumericFieldDataStub());
    FunctionScoreQuery fsQuery1 = new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
    assertThat(exc.getMessage(), containsString("consider using log1p or log2p instead of log to avoid negative scores"));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) Term(org.apache.lucene.index.Term) LeafScoreFunction(org.opensearch.common.lucene.search.function.LeafScoreFunction) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Example 5 with ScoreFunction

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

the class FunctionScoreQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    ScoreFunction[] filterFunctions = new ScoreFunction[filterFunctionBuilders.length];
    int i = 0;
    for (FilterFunctionBuilder filterFunctionBuilder : filterFunctionBuilders) {
        ScoreFunction scoreFunction = filterFunctionBuilder.getScoreFunction().toFunction(context);
        final QueryBuilder builder = filterFunctionBuilder.getFilter();
        if (builder.getName().equals(MatchAllQueryBuilder.NAME)) {
            filterFunctions[i++] = scoreFunction;
        } else {
            Query filter = builder.toQuery(context);
            filterFunctions[i++] = new FunctionScoreQuery.FilterScoreFunction(filter, scoreFunction, builder.queryName());
        }
    }
    final QueryBuilder builder = this.query;
    Query query = builder.toQuery(context);
    if (query == null) {
        query = new MatchAllDocsQuery();
    }
    CombineFunction boostMode = this.boostMode == null ? DEFAULT_BOOST_MODE : this.boostMode;
    // handle cases where only one score function and no filter was provided. In this case we create a FunctionScoreQuery.
    if (filterFunctions.length == 0) {
        return new FunctionScoreQuery(query, builder.queryName(), minScore, maxBoost);
    } else if (filterFunctions.length == 1 && filterFunctions[0] instanceof FunctionScoreQuery.FilterScoreFunction == false) {
        return new FunctionScoreQuery(query, builder.queryName(), filterFunctions[0], boostMode, minScore, maxBoost);
    }
    // in all other cases we create a FunctionScoreQuery with filters
    return new FunctionScoreQuery(query, builder.queryName(), scoreMode, filterFunctions, boostMode, minScore, maxBoost);
}
Also used : CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) Query(org.apache.lucene.search.Query) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Aggregations

ScoreFunction (org.opensearch.common.lucene.search.function.ScoreFunction)7 FunctionScoreQuery (org.opensearch.common.lucene.search.function.FunctionScoreQuery)6 FilterScoreFunction (org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction)6 LeafScoreFunction (org.opensearch.common.lucene.search.function.LeafScoreFunction)6 RandomScoreFunction (org.opensearch.common.lucene.search.function.RandomScoreFunction)6 Term (org.apache.lucene.index.Term)5 TermQuery (org.apache.lucene.search.TermQuery)5 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 CombineFunction (org.opensearch.common.lucene.search.function.CombineFunction)3 FieldValueFactorFunction (org.opensearch.common.lucene.search.function.FieldValueFactorFunction)3 IOException (java.io.IOException)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Query (org.apache.lucene.search.Query)1 WeightFactorFunction (org.opensearch.common.lucene.search.function.WeightFactorFunction)1 AbstractQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder)1 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)1 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1