Search in sources :

Example 6 with FunctionScoreQuery

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

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

the class FunctionScoreEquivalenceTests method testMinScoreAllExcluded.

public void testMinScoreAllExcluded() throws Exception {
    Term term = randomTerm();
    Query query = new TermQuery(term);
    FunctionScoreQuery fsq = new FunctionScoreQuery(query, null, Float.POSITIVE_INFINITY, null, Float.POSITIVE_INFINITY);
    assertSameScores(new MatchNoDocsQuery(), fsq);
    FiltersFunctionScoreQuery ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, CombineFunction.MULTIPLY);
    assertSameScores(new MatchNoDocsQuery(), ffsq);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) Term(org.apache.lucene.index.Term)

Example 8 with FunctionScoreQuery

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

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

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

the class DefaultSearchContext method preProcess.

/**
     * Should be called before executing the main query and after all other parameters have been set.
     */
@Override
public void preProcess(boolean rewrite) {
    if (hasOnlySuggest()) {
        return;
    }
    long from = from() == -1 ? 0 : from();
    long size = size() == -1 ? 10 : size();
    long resultWindow = from + size;
    int maxResultWindow = indexService.getIndexSettings().getMaxResultWindow();
    if (resultWindow > maxResultWindow) {
        if (scrollContext == null) {
            throw new QueryPhaseExecutionException(this, "Result window is too large, from + size must be less than or equal to: [" + maxResultWindow + "] but was [" + resultWindow + "]. See the scroll api for a more efficient way to request large data sets. " + "This limit can be set by changing the [" + IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey() + "] index level setting.");
        }
        throw new QueryPhaseExecutionException(this, "Batch size is too large, size must be less than or equal to: [" + maxResultWindow + "] but was [" + resultWindow + "]. Scroll batch sizes cost as much memory as result windows so they are controlled by the [" + IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey() + "] index level setting.");
    }
    if (rescore != null) {
        int maxWindow = indexService.getIndexSettings().getMaxRescoreWindow();
        for (RescoreSearchContext rescoreContext : rescore) {
            if (rescoreContext.window() > maxWindow) {
                throw new QueryPhaseExecutionException(this, "Rescore window [" + rescoreContext.window() + "] is too large. It must " + "be less than [" + maxWindow + "]. This prevents allocating massive heaps for storing the results to be " + "rescored. This limit can be set by changing the [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.getKey() + "] index level setting.");
            }
        }
    }
    if (sliceBuilder != null) {
        int sliceLimit = indexService.getIndexSettings().getMaxSlicesPerScroll();
        int numSlices = sliceBuilder.getMax();
        if (numSlices > sliceLimit) {
            throw new QueryPhaseExecutionException(this, "The number of slices [" + numSlices + "] is too large. It must " + "be less than [" + sliceLimit + "]. This limit can be set by changing the [" + IndexSettings.MAX_SLICES_PER_SCROLL.getKey() + "] index level setting.");
        }
    }
    // initialize the filtering alias based on the provided filters
    try {
        final QueryBuilder queryBuilder = request.filteringAliases();
        aliasFilter = queryBuilder == null ? null : queryBuilder.toFilter(queryShardContext);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    if (query() == null) {
        parsedQuery(ParsedQuery.parsedMatchAllQuery());
    }
    if (queryBoost() != AbstractQueryBuilder.DEFAULT_BOOST) {
        parsedQuery(new ParsedQuery(new FunctionScoreQuery(query(), new WeightFactorFunction(queryBoost)), parsedQuery()));
    }
    this.query = buildFilteredQuery(query);
    if (rewrite) {
        try {
            this.query = searcher.rewrite(query);
        } catch (IOException e) {
            throw new QueryPhaseExecutionException(this, "Failed to rewrite main query", e);
        }
    }
}
Also used : WeightFactorFunction(org.elasticsearch.common.lucene.search.function.WeightFactorFunction) RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) QueryPhaseExecutionException(org.elasticsearch.search.query.QueryPhaseExecutionException) UncheckedIOException(java.io.UncheckedIOException) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Aggregations

FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)18 FiltersFunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)14 TermQuery (org.apache.lucene.search.TermQuery)13 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 Query (org.apache.lucene.search.Query)9 Term (org.apache.lucene.index.Term)8 RandomApproximationQuery (org.apache.lucene.search.RandomApproximationQuery)5 WeightFactorFunction (org.elasticsearch.common.lucene.search.function.WeightFactorFunction)5 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)3 RandomScoreFunction (org.elasticsearch.common.lucene.search.function.RandomScoreFunction)3 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)2 BoostQuery (org.apache.lucene.search.BoostQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 Explanation (org.apache.lucene.search.Explanation)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Weight (org.apache.lucene.search.Weight)2 MultiPhrasePrefixQuery (org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)2 CombineFunction (org.elasticsearch.common.lucene.search.function.CombineFunction)2 FieldValueFactorFunction (org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction)2