Search in sources :

Example 1 with ScoreFunction

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

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 = randomBoolean() ? null : new DummyScoreFunction(combineFunction);
    FunctionScoreQuery q = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, minScore, combineFunction, maxBoost);
    FunctionScoreQuery q1 = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, minScore, combineFunction, 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, minScore, combineFunction, maxBoost);
    FunctionScoreQuery diffMinScore = new FunctionScoreQuery(q.getSubQuery(), function, minScore == null ? 1.0f : null, combineFunction, maxBoost);
    ScoreFunction otherFunciton = function == null ? new DummyScoreFunction(combineFunction) : null;
    FunctionScoreQuery diffFunction = new FunctionScoreQuery(q.getSubQuery(), otherFunciton, minScore, combineFunction, maxBoost);
    FunctionScoreQuery diffMaxBoost = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, minScore, combineFunction, 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.elasticsearch.common.lucene.search.function.CombineFunction) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) 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)

Example 2 with ScoreFunction

use of org.elasticsearch.common.lucene.search.function.ScoreFunction 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 3 with ScoreFunction

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

Example 4 with ScoreFunction

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

the class FunctionScoreTests method testWeightFactorNeedsScore.

public void testWeightFactorNeedsScore() {
    for (boolean needsScore : new boolean[] { true, false }) {
        WeightFactorFunction function = new WeightFactorFunction(10.0f, new ScoreFunction(CombineFunction.REPLACE) {

            @Override
            public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) throws IOException {
                return null;
            }

            @Override
            public boolean needsScores() {
                return needsScore;
            }

            @Override
            protected boolean doEquals(ScoreFunction other) {
                return false;
            }

            @Override
            protected int doHashCode() {
                return 0;
            }
        });
        assertEquals(needsScore, function.needsScores());
    }
}
Also used : WeightFactorFunction(org.elasticsearch.common.lucene.search.function.WeightFactorFunction) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) IOException(java.io.IOException) LeafScoreFunction(org.elasticsearch.common.lucene.search.function.LeafScoreFunction) 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

ScoreFunction (org.elasticsearch.common.lucene.search.function.ScoreFunction)4 CombineFunction (org.elasticsearch.common.lucene.search.function.CombineFunction)3 FiltersFunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)3 LeafScoreFunction (org.elasticsearch.common.lucene.search.function.LeafScoreFunction)3 RandomScoreFunction (org.elasticsearch.common.lucene.search.function.RandomScoreFunction)3 Term (org.apache.lucene.index.Term)2 TermQuery (org.apache.lucene.search.TermQuery)2 FilterFunction (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction)2 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)2 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 ScoreMode (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.ScoreMode)1 WeightFactorFunction (org.elasticsearch.common.lucene.search.function.WeightFactorFunction)1