Search in sources :

Example 21 with FunctionScoreQuery

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

the class FunctionScoreTests method testPropagatesApproximations.

public void testPropagatesApproximations() throws IOException {
    Query query = new RandomApproximationQuery(new MatchAllDocsQuery(), random());
    IndexSearcher searcher = newSearcher(reader);
    // otherwise we could get a cached entry that does not have approximations
    searcher.setQueryCache(null);
    FunctionScoreQuery fsq = new FunctionScoreQuery(query, null, Float.POSITIVE_INFINITY);
    for (org.apache.lucene.search.ScoreMode scoreMode : org.apache.lucene.search.ScoreMode.values()) {
        Weight weight = searcher.createWeight(fsq, scoreMode, 1f);
        Scorer scorer = weight.scorer(reader.leaves().get(0));
        assertNotNull(scorer.twoPhaseIterator());
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) Scorer(org.apache.lucene.search.Scorer) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Weight(org.apache.lucene.search.Weight)

Example 22 with FunctionScoreQuery

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

the class FunctionScoreTests method testWithInvalidScores.

public void testWithInvalidScores() {
    IndexSearcher localSearcher = new IndexSearcher(reader);
    FunctionScoreQuery query1 = new FunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), new ConstantScoreFunction(Float.NaN), CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
    OpenSearchException exc = expectThrows(OpenSearchException.class, () -> localSearcher.search(query1, 1));
    assertThat(exc.getMessage(), containsString("function score query returned an invalid score: " + Float.NaN));
    FunctionScoreQuery query2 = new FunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), new ConstantScoreFunction(Float.NEGATIVE_INFINITY), CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
    exc = expectThrows(OpenSearchException.class, () -> localSearcher.search(query2, 1));
    assertThat(exc.getMessage(), containsString("function score query returned an invalid score: " + Float.NEGATIVE_INFINITY));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) OpenSearchException(org.opensearch.OpenSearchException) Term(org.apache.lucene.index.Term)

Example 23 with FunctionScoreQuery

use of org.opensearch.common.lucene.search.function.FunctionScoreQuery 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)

Example 24 with FunctionScoreQuery

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

the class FunctionScoreTests method testExceptionOnLnNegativeScores.

public void testExceptionOnLnNegativeScores() {
    IndexSearcher localSearcher = new IndexSearcher(reader);
    TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));
    // test that field_value_factor function using modifier ln throws an exception on negative scores
    FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.LN;
    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 ln1p or ln2p instead of ln 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 25 with FunctionScoreQuery

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

the class FunctionScoreTests method testMinScoreExplain.

public void testMinScoreExplain() throws IOException {
    Query query = new MatchAllDocsQuery();
    Explanation queryExpl = searcher.explain(query, 0);
    FunctionScoreQuery fsq = new FunctionScoreQuery(query, 0f, Float.POSITIVE_INFINITY);
    Explanation fsqExpl = searcher.explain(fsq, 0);
    assertTrue(fsqExpl.isMatch());
    assertEquals(queryExpl.getValue(), fsqExpl.getValue());
    assertEquals(queryExpl.getDescription(), fsqExpl.getDescription());
    fsq = new FunctionScoreQuery(query, 10f, Float.POSITIVE_INFINITY);
    fsqExpl = searcher.explain(fsq, 0);
    assertFalse(fsqExpl.isMatch());
    assertEquals("Score value is too low, expected at least 10.0 but got 1.0", fsqExpl.getDescription());
    FunctionScoreQuery ffsq = new FunctionScoreQuery(query, 0f, Float.POSITIVE_INFINITY);
    Explanation ffsqExpl = searcher.explain(ffsq, 0);
    assertTrue(ffsqExpl.isMatch());
    assertEquals(queryExpl.getValue(), ffsqExpl.getValue());
    assertEquals(queryExpl.getDescription(), ffsqExpl.getDescription());
    ffsq = new FunctionScoreQuery(query, 10f, Float.POSITIVE_INFINITY);
    ffsqExpl = searcher.explain(ffsq, 0);
    assertFalse(ffsqExpl.isMatch());
    assertEquals("Score value is too low, expected at least 10.0 but got 1.0", ffsqExpl.getDescription());
}
Also used : Query(org.apache.lucene.search.Query) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) Explanation(org.apache.lucene.search.Explanation) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

FunctionScoreQuery (org.opensearch.common.lucene.search.function.FunctionScoreQuery)27 TermQuery (org.apache.lucene.search.TermQuery)21 Term (org.apache.lucene.index.Term)15 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)13 Query (org.apache.lucene.search.Query)12 IndexSearcher (org.apache.lucene.search.IndexSearcher)9 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 RandomScoreFunction (org.opensearch.common.lucene.search.function.RandomScoreFunction)8 ScoreFunction (org.opensearch.common.lucene.search.function.ScoreFunction)6 RandomApproximationQuery (org.apache.lucene.search.RandomApproximationQuery)5 FieldValueFactorFunction (org.opensearch.common.lucene.search.function.FieldValueFactorFunction)5 FilterScoreFunction (org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction)5 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)4 TopDocs (org.apache.lucene.search.TopDocs)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 CombineFunction (org.opensearch.common.lucene.search.function.CombineFunction)4 LeafScoreFunction (org.opensearch.common.lucene.search.function.LeafScoreFunction)4 WeightFactorFunction (org.opensearch.common.lucene.search.function.WeightFactorFunction)4 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)3 Explanation (org.apache.lucene.search.Explanation)3