Search in sources :

Example 1 with FilterScoreFunction

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

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

Aggregations

Term (org.apache.lucene.index.Term)2 TermQuery (org.apache.lucene.search.TermQuery)2 CombineFunction (org.opensearch.common.lucene.search.function.CombineFunction)2 FunctionScoreQuery (org.opensearch.common.lucene.search.function.FunctionScoreQuery)2 FilterScoreFunction (org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction)2 Explanation (org.apache.lucene.search.Explanation)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 TopDocs (org.apache.lucene.search.TopDocs)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 WeightFactorFunction (org.opensearch.common.lucene.search.function.WeightFactorFunction)1