Search in sources :

Example 1 with QueryPhaseExecutionException

use of org.opensearch.search.query.QueryPhaseExecutionException in project OpenSearch by opensearch-project.

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 IllegalArgumentException("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 IllegalArgumentException("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) {
        if (sort != null) {
            throw new IllegalArgumentException("Cannot use [sort] option in conjunction with [rescore].");
        }
        int maxWindow = indexService.getIndexSettings().getMaxRescoreWindow();
        for (RescoreContext rescoreContext : rescore()) {
            if (rescoreContext.getWindowSize() > maxWindow) {
                throw new IllegalArgumentException("Rescore window [" + rescoreContext.getWindowSize() + "] 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 IllegalArgumentException("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.getAliasFilter().getQueryBuilder();
        aliasFilter = queryBuilder == null ? null : queryBuilder.toQuery(queryShardContext);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    if (query() == null) {
        parsedQuery(ParsedQuery.parsedMatchAllQuery());
    }
    if (queryBoost() != AbstractQueryBuilder.DEFAULT_BOOST) {
        parsedQuery(new ParsedQuery(new BoostQuery(query(), queryBoost), parsedQuery()));
    }
    this.query = buildFilteredQuery(query);
    if (rewrite) {
        try {
            this.query = searcher.rewrite(query);
        } catch (IOException e) {
            throw new QueryPhaseExecutionException(shardTarget, "Failed to rewrite main query", e);
        }
    }
}
Also used : RescoreContext(org.opensearch.search.rescore.RescoreContext) ParsedQuery(org.opensearch.index.query.ParsedQuery) QueryPhaseExecutionException(org.opensearch.search.query.QueryPhaseExecutionException) UncheckedIOException(java.io.UncheckedIOException) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 2 with QueryPhaseExecutionException

use of org.opensearch.search.query.QueryPhaseExecutionException in project OpenSearch by opensearch-project.

the class AggregationPhase method execute.

public void execute(SearchContext context) {
    if (context.aggregations() == null) {
        context.queryResult().aggregations(null);
        return;
    }
    if (context.queryResult().hasAggs()) {
        // no need to compute the aggs twice, they should be computed on a per context basis
        return;
    }
    Aggregator[] aggregators = context.aggregations().aggregators();
    List<Aggregator> globals = new ArrayList<>();
    for (int i = 0; i < aggregators.length; i++) {
        if (aggregators[i] instanceof GlobalAggregator) {
            globals.add(aggregators[i]);
        }
    }
    // optimize the global collector based execution
    if (!globals.isEmpty()) {
        BucketCollector globalsCollector = MultiBucketCollector.wrap(globals);
        Query query = context.buildFilteredQuery(Queries.newMatchAllQuery());
        try {
            final Collector collector;
            if (context.getProfilers() == null) {
                collector = globalsCollector;
            } else {
                InternalProfileCollector profileCollector = new InternalProfileCollector(globalsCollector, CollectorResult.REASON_AGGREGATION_GLOBAL, // TODO: report on sub collectors
                Collections.emptyList());
                collector = profileCollector;
                // start a new profile with this collector
                context.getProfilers().addQueryProfiler().setCollector(profileCollector);
            }
            globalsCollector.preCollection();
            context.searcher().search(query, collector);
        } catch (Exception e) {
            throw new QueryPhaseExecutionException(context.shardTarget(), "Failed to execute global aggregators", e);
        }
    }
    List<InternalAggregation> aggregations = new ArrayList<>(aggregators.length);
    context.aggregations().resetBucketMultiConsumer();
    for (Aggregator aggregator : context.aggregations().aggregators()) {
        try {
            aggregator.postCollection();
            aggregations.add(aggregator.buildTopLevel());
        } catch (IOException e) {
            throw new AggregationExecutionException("Failed to build aggregation [" + aggregator.name() + "]", e);
        }
    }
    context.queryResult().aggregations(new InternalAggregations(aggregations, context.request().source().aggregations()::buildPipelineTree));
    // disable aggregations so that they don't run on next pages in case of scrolling
    context.aggregations(null);
    context.queryCollectors().remove(AggregationPhase.class);
}
Also used : Query(org.apache.lucene.search.Query) QueryPhaseExecutionException(org.opensearch.search.query.QueryPhaseExecutionException) ArrayList(java.util.ArrayList) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) IOException(java.io.IOException) QueryPhaseExecutionException(org.opensearch.search.query.QueryPhaseExecutionException) IOException(java.io.IOException) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) Collector(org.apache.lucene.search.Collector) InternalProfileCollector(org.opensearch.search.profile.query.InternalProfileCollector) InternalProfileCollector(org.opensearch.search.profile.query.InternalProfileCollector)

Aggregations

IOException (java.io.IOException)2 QueryPhaseExecutionException (org.opensearch.search.query.QueryPhaseExecutionException)2 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 Collector (org.apache.lucene.search.Collector)1 Query (org.apache.lucene.search.Query)1 AbstractQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder)1 ParsedQuery (org.opensearch.index.query.ParsedQuery)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1 GlobalAggregator (org.opensearch.search.aggregations.bucket.global.GlobalAggregator)1 InternalProfileCollector (org.opensearch.search.profile.query.InternalProfileCollector)1 RescoreContext (org.opensearch.search.rescore.RescoreContext)1