Search in sources :

Example 1 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 2 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 3 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)

Example 4 with FunctionScoreQuery

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

the class QueryAnalyzerTests method testFunctionScoreQuery.

public void testFunctionScoreQuery() {
    TermQuery termQuery = new TermQuery(new Term("_field", "_value"));
    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(termQuery, new RandomScoreFunction());
    Result result = analyze(functionScoreQuery);
    assertThat(result.verified, is(true));
    assertTermsEqual(result.terms, new Term("_field", "_value"));
    functionScoreQuery = new FunctionScoreQuery(termQuery, new RandomScoreFunction(), 1f, null, 10f);
    result = analyze(functionScoreQuery);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, new Term("_field", "_value"));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) RandomScoreFunction(org.elasticsearch.common.lucene.search.function.RandomScoreFunction) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 5 with FunctionScoreQuery

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

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