Search in sources :

Example 1 with FiltersFunctionScoreQuery

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

the class CustomUnifiedHighlighter method rewriteCustomQuery.

/**
     * Translate custom queries in queries that are supported by the unified highlighter.
     */
private Collection<Query> rewriteCustomQuery(Query query) {
    if (query instanceof MultiPhrasePrefixQuery) {
        MultiPhrasePrefixQuery mpq = (MultiPhrasePrefixQuery) query;
        Term[][] terms = mpq.getTerms();
        int[] positions = mpq.getPositions();
        SpanQuery[] positionSpanQueries = new SpanQuery[positions.length];
        int sizeMinus1 = terms.length - 1;
        for (int i = 0; i < positions.length; i++) {
            SpanQuery[] innerQueries = new SpanQuery[terms[i].length];
            for (int j = 0; j < terms[i].length; j++) {
                if (i == sizeMinus1) {
                    innerQueries[j] = new SpanMultiTermQueryWrapper(new PrefixQuery(terms[i][j]));
                } else {
                    innerQueries[j] = new SpanTermQuery(terms[i][j]);
                }
            }
            if (innerQueries.length > 1) {
                positionSpanQueries[i] = new SpanOrQuery(innerQueries);
            } else {
                positionSpanQueries[i] = innerQueries[0];
            }
        }
        // sum position increments beyond 1
        int positionGaps = 0;
        if (positions.length >= 2) {
            // positions are in increasing order.   max(0,...) is just a safeguard.
            positionGaps = Math.max(0, positions[positions.length - 1] - positions[0] - positions.length + 1);
        }
        //if original slop is 0 then require inOrder
        boolean inorder = (mpq.getSlop() == 0);
        return Collections.singletonList(new SpanNearQuery(positionSpanQueries, mpq.getSlop() + positionGaps, inorder));
    } else if (query instanceof CommonTermsQuery) {
        CommonTermsQuery ctq = (CommonTermsQuery) query;
        List<Query> tqs = new ArrayList<>();
        for (Term term : ctq.getTerms()) {
            tqs.add(new TermQuery(term));
        }
        return tqs;
    } else if (query instanceof AllTermQuery) {
        AllTermQuery atq = (AllTermQuery) query;
        return Collections.singletonList(new TermQuery(atq.getTerm()));
    } else if (query instanceof FunctionScoreQuery) {
        return Collections.singletonList(((FunctionScoreQuery) query).getSubQuery());
    } else if (query instanceof FiltersFunctionScoreQuery) {
        return Collections.singletonList(((FiltersFunctionScoreQuery) query).getSubQuery());
    } else {
        return null;
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) Term(org.apache.lucene.index.Term) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ArrayList(java.util.ArrayList) List(java.util.List) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 2 with FiltersFunctionScoreQuery

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

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

the class FunctionScoreTests method testSimpleWeightedFunction.

public void testSimpleWeightedFunction() throws IOException, ExecutionException, InterruptedException {
    int numFunctions = randomIntBetween(1, 3);
    float[] weights = randomFloats(numFunctions);
    double[] scores = randomDoubles(numFunctions);
    ScoreFunctionStub[] scoreFunctionStubs = new ScoreFunctionStub[numFunctions];
    for (int i = 0; i < numFunctions; i++) {
        scoreFunctionStubs[i] = new ScoreFunctionStub(scores[i]);
    }
    WeightFactorFunction[] weightFunctionStubs = new WeightFactorFunction[numFunctions];
    for (int i = 0; i < numFunctions; i++) {
        weightFunctionStubs[i] = new WeightFactorFunction(weights[i], scoreFunctionStubs[i]);
    }
    FiltersFunctionScoreQuery filtersFunctionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY, CombineFunction.REPLACE, weightFunctionStubs);
    TopDocs topDocsWithWeights = searcher.search(filtersFunctionScoreQueryWithWeights, 1);
    float scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double score = 1;
    for (int i = 0; i < weights.length; i++) {
        score *= weights[i] * scores[i];
    }
    assertThat(scoreWithWeight / (float) score, is(1f));
    float explainedScore = getExplanation(searcher, filtersFunctionScoreQueryWithWeights).getValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    filtersFunctionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FiltersFunctionScoreQuery.ScoreMode.SUM, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(filtersFunctionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double sum = 0;
    for (int i = 0; i < weights.length; i++) {
        sum += weights[i] * scores[i];
    }
    assertThat(scoreWithWeight / (float) sum, is(1f));
    explainedScore = getExplanation(searcher, filtersFunctionScoreQueryWithWeights).getValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    filtersFunctionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FiltersFunctionScoreQuery.ScoreMode.AVG, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(filtersFunctionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double norm = 0;
    sum = 0;
    for (int i = 0; i < weights.length; i++) {
        norm += weights[i];
        sum += weights[i] * scores[i];
    }
    assertThat(scoreWithWeight / (float) (sum / norm), is(1f));
    explainedScore = getExplanation(searcher, filtersFunctionScoreQueryWithWeights).getValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    filtersFunctionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FiltersFunctionScoreQuery.ScoreMode.MIN, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(filtersFunctionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double min = Double.POSITIVE_INFINITY;
    for (int i = 0; i < weights.length; i++) {
        min = Math.min(min, weights[i] * scores[i]);
    }
    assertThat(scoreWithWeight / (float) min, is(1f));
    explainedScore = getExplanation(searcher, filtersFunctionScoreQueryWithWeights).getValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
    filtersFunctionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FiltersFunctionScoreQuery.ScoreMode.MAX, CombineFunction.REPLACE, weightFunctionStubs);
    topDocsWithWeights = searcher.search(filtersFunctionScoreQueryWithWeights, 1);
    scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
    double max = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < weights.length; i++) {
        max = Math.max(max, weights[i] * scores[i]);
    }
    assertThat(scoreWithWeight / (float) max, is(1f));
    explainedScore = getExplanation(searcher, filtersFunctionScoreQueryWithWeights).getValue();
    assertThat(explainedScore / scoreWithWeight, is(1f));
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) WeightFactorFunction(org.elasticsearch.common.lucene.search.function.WeightFactorFunction) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)

Example 4 with FiltersFunctionScoreQuery

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

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, null, null, Float.POSITIVE_INFINITY);
    for (boolean needsScores : new boolean[] { true, false }) {
        Weight weight = searcher.createWeight(fsq, needsScores);
        Scorer scorer = weight.scorer(reader.leaves().get(0));
        assertNotNull(scorer.twoPhaseIterator());
    }
    FiltersFunctionScoreQuery ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, null, CombineFunction.MULTIPLY);
    for (boolean needsScores : new boolean[] { true, false }) {
        Weight weight = searcher.createWeight(ffsq, needsScores);
        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) 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) 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 5 with FiltersFunctionScoreQuery

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

Aggregations

FiltersFunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)11 TermQuery (org.apache.lucene.search.TermQuery)9 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)8 Term (org.apache.lucene.index.Term)7 Query (org.apache.lucene.search.Query)6 RandomApproximationQuery (org.apache.lucene.search.RandomApproximationQuery)5 FilterFunction (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 CombineFunction (org.elasticsearch.common.lucene.search.function.CombineFunction)3 Explanation (org.apache.lucene.search.Explanation)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 TopDocs (org.apache.lucene.search.TopDocs)2 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)2 MultiPhrasePrefixQuery (org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)2 ScoreMode (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.ScoreMode)2 ScoreFunction (org.elasticsearch.common.lucene.search.function.ScoreFunction)2 WeightFactorFunction (org.elasticsearch.common.lucene.search.function.WeightFactorFunction)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1