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());
}
}
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));
}
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));
}
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"));
}
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());
}
Aggregations