Search in sources :

Example 1 with FilterFunction

use of org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction in project elasticsearch by elastic.

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, null, 0f, null, Float.POSITIVE_INFINITY);
    Explanation fsqExpl = searcher.explain(fsq, 0);
    assertTrue(fsqExpl.isMatch());
    assertEquals(queryExpl.getValue(), fsqExpl.getValue(), 0f);
    assertEquals(queryExpl.getDescription(), fsqExpl.getDescription());
    fsq = new FunctionScoreQuery(query, null, 10f, null, 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());
    FiltersFunctionScoreQuery ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, 0f, CombineFunction.MULTIPLY);
    Explanation ffsqExpl = searcher.explain(ffsq, 0);
    assertTrue(ffsqExpl.isMatch());
    assertEquals(queryExpl.getValue(), ffsqExpl.getValue(), 0f);
    assertEquals(queryExpl.getDescription(), ffsqExpl.getDetails()[0].getDescription());
    ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, 10f, CombineFunction.MULTIPLY);
    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 : FilterFunction(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) Explanation(org.apache.lucene.search.Explanation) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 2 with FilterFunction

use of org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction in project elasticsearch by elastic.

the class FunctionScoreTests method testExplanationAndScoreEqualsEvenIfNoFunctionMatches.

public void testExplanationAndScoreEqualsEvenIfNoFunctionMatches() throws IOException {
    IndexSearcher localSearcher = newSearcher(reader);
    ScoreMode scoreMode = randomFrom(new ScoreMode[] { ScoreMode.SUM, ScoreMode.AVG, ScoreMode.FIRST, ScoreMode.MIN, ScoreMode.MAX, ScoreMode.MULTIPLY });
    CombineFunction combineFunction = randomFrom(new CombineFunction[] { CombineFunction.SUM, CombineFunction.AVG, CombineFunction.MIN, CombineFunction.MAX, CombineFunction.MULTIPLY, CombineFunction.REPLACE });
    // check for document that has no macthing function
    FiltersFunctionScoreQuery query = new FiltersFunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), scoreMode, new FilterFunction[] { new FilterFunction(new TermQuery(new Term("_uid", "2")), new WeightFactorFunction(10)) }, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, combineFunction);
    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 FiltersFunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), scoreMode, new FilterFunction[] { new FilterFunction(new TermQuery(new Term("_uid", "1")), new WeightFactorFunction(10)) }, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, combineFunction);
    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.elasticsearch.common.lucene.search.function.CombineFunction) TopDocs(org.apache.lucene.search.TopDocs) ScoreMode(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.ScoreMode) TermQuery(org.apache.lucene.search.TermQuery) WeightFactorFunction(org.elasticsearch.common.lucene.search.function.WeightFactorFunction) FilterFunction(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) Explanation(org.apache.lucene.search.Explanation) Term(org.apache.lucene.index.Term)

Example 3 with FilterFunction

use of org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction in project elasticsearch by elastic.

the class FunctionScoreQueryBuilder method doToQuery.

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

Example 4 with FilterFunction

use of org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction in project elasticsearch by elastic.

the class FunctionScoreTests method testFilterFunctionScoreHashCodeAndEquals.

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

Aggregations

FiltersFunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)4 FilterFunction (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction)4 TermQuery (org.apache.lucene.search.TermQuery)3 CombineFunction (org.elasticsearch.common.lucene.search.function.CombineFunction)3 Term (org.apache.lucene.index.Term)2 Explanation (org.apache.lucene.search.Explanation)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 Query (org.apache.lucene.search.Query)2 ScoreMode (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.ScoreMode)2 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)2 ScoreFunction (org.elasticsearch.common.lucene.search.function.ScoreFunction)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 RandomApproximationQuery (org.apache.lucene.search.RandomApproximationQuery)1 TopDocs (org.apache.lucene.search.TopDocs)1 LeafScoreFunction (org.elasticsearch.common.lucene.search.function.LeafScoreFunction)1 RandomScoreFunction (org.elasticsearch.common.lucene.search.function.RandomScoreFunction)1 WeightFactorFunction (org.elasticsearch.common.lucene.search.function.WeightFactorFunction)1