Search in sources :

Example 1 with ShardSearchRequest

use of org.opensearch.search.internal.ShardSearchRequest in project OpenSearch by opensearch-project.

the class TransportValidateQueryAction method shardOperation.

@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request, Task task) throws IOException {
    boolean valid;
    String explanation = null;
    String error = null;
    ShardSearchRequest shardSearchLocalRequest = new ShardSearchRequest(request.shardId(), request.nowInMillis(), request.filteringAliases());
    SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
    try {
        ParsedQuery parsedQuery = searchContext.getQueryShardContext().toQuery(request.query());
        searchContext.parsedQuery(parsedQuery);
        searchContext.preProcess(request.rewrite());
        valid = true;
        explanation = explain(searchContext, request.rewrite());
    } catch (QueryShardException | ParsingException e) {
        valid = false;
        error = e.getDetailedMessage();
    } catch (AssertionError e) {
        valid = false;
        error = e.getMessage();
    } finally {
        Releasables.close(searchContext);
    }
    return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
Also used : ParsedQuery(org.opensearch.index.query.ParsedQuery) ParsingException(org.opensearch.common.ParsingException) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchContext(org.opensearch.search.internal.SearchContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 2 with ShardSearchRequest

use of org.opensearch.search.internal.ShardSearchRequest in project OpenSearch by opensearch-project.

the class AbstractSearchAsyncAction method buildShardSearchRequest.

@Override
public final ShardSearchRequest buildShardSearchRequest(SearchShardIterator shardIt) {
    AliasFilter filter = aliasFilter.get(shardIt.shardId().getIndex().getUUID());
    assert filter != null;
    float indexBoost = concreteIndexBoosts.getOrDefault(shardIt.shardId().getIndex().getUUID(), DEFAULT_INDEX_BOOST);
    String indexName = shardIt.shardId().getIndex().getName();
    final String[] routings = indexRoutings.getOrDefault(indexName, Collections.emptySet()).toArray(new String[0]);
    ShardSearchRequest shardRequest = new ShardSearchRequest(shardIt.getOriginalIndices(), request, shardIt.shardId(), getNumShards(), filter, indexBoost, timeProvider.getAbsoluteStartMillis(), shardIt.getClusterAlias(), routings, shardIt.getSearchContextId(), shardIt.getSearchContextKeepAlive());
    // if we already received a search result we can inform the shard that it
    // can return a null response if the request rewrites to match none rather
    // than creating an empty response in the search thread pool.
    // Note that, we have to disable this shortcut for queries that create a context (scroll and search context).
    shardRequest.canReturnNullResponseIfMatchNoDocs(hasShardResponse.get() && shardRequest.scroll() == null);
    return shardRequest;
}
Also used : AliasFilter(org.opensearch.search.internal.AliasFilter) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest)

Example 3 with ShardSearchRequest

use of org.opensearch.search.internal.ShardSearchRequest in project OpenSearch by opensearch-project.

the class TransportFieldCapabilitiesIndexAction method canMatchShard.

private boolean canMatchShard(FieldCapabilitiesIndexRequest req) throws IOException {
    if (req.indexFilter() == null || req.indexFilter() instanceof MatchAllQueryBuilder) {
        return true;
    }
    assert req.nowInMillis() != 0L;
    ShardSearchRequest searchRequest = new ShardSearchRequest(req.shardId(), req.nowInMillis(), AliasFilter.EMPTY);
    searchRequest.source(new SearchSourceBuilder().query(req.indexFilter()));
    return searchService.canMatch(searchRequest).canMatch();
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 4 with ShardSearchRequest

use of org.opensearch.search.internal.ShardSearchRequest in project OpenSearch by opensearch-project.

the class SearchService method executeQueryPhase.

public void executeQueryPhase(QuerySearchRequest request, SearchShardTask task, ActionListener<QuerySearchResult> listener) {
    final ReaderContext readerContext = findReaderContext(request.contextId(), request.shardSearchRequest());
    final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(request.shardSearchRequest());
    final Releasable markAsUsed = readerContext.markAsUsed(getKeepAlive(shardSearchRequest));
    runAsync(getExecutor(readerContext.indexShard()), () -> {
        readerContext.setAggregatedDfs(request.dfs());
        try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, true);
            SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(searchContext)) {
            searchContext.searcher().setAggregatedDfs(request.dfs());
            queryPhase.execute(searchContext);
            if (searchContext.queryResult().hasSearchContext() == false && readerContext.singleSession()) {
                // no hits, we can release the context since there will be no fetch phase
                freeReaderContext(readerContext.id());
            }
            executor.success();
            // Pass the rescoreDocIds to the queryResult to send them the coordinating node and receive them back in the fetch phase.
            // We also pass the rescoreDocIds to the LegacyReaderContext in case the search state needs to stay in the data node.
            final RescoreDocIds rescoreDocIds = searchContext.rescoreDocIds();
            searchContext.queryResult().setRescoreDocIds(rescoreDocIds);
            readerContext.setRescoreDocIds(rescoreDocIds);
            return searchContext.queryResult();
        } catch (Exception e) {
            assert TransportActions.isShardNotAvailableException(e) == false : new AssertionError(e);
            logger.trace("Query phase failed", e);
            // we handle the failure in the failure listener below
            throw e;
        }
    }, wrapFailureListener(listener, readerContext, markAsUsed));
}
Also used : LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchContext(org.opensearch.search.internal.SearchContext) Releasable(org.opensearch.common.lease.Releasable) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AggregationInitializationException(org.opensearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OpenSearchException(org.opensearch.OpenSearchException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 5 with ShardSearchRequest

use of org.opensearch.search.internal.ShardSearchRequest in project OpenSearch by opensearch-project.

the class SearchService method executeFetchPhase.

public void executeFetchPhase(InternalScrollSearchRequest request, SearchShardTask task, ActionListener<ScrollQueryFetchSearchResult> listener) {
    final LegacyReaderContext readerContext = (LegacyReaderContext) findReaderContext(request.contextId(), request);
    final Releasable markAsUsed;
    try {
        markAsUsed = readerContext.markAsUsed(getScrollKeepAlive(request.scroll()));
    } catch (Exception e) {
        // We need to release the reader context of the scroll when we hit any exception (here the keep_alive can be too large)
        freeReaderContext(readerContext.id());
        throw e;
    }
    runAsync(getExecutor(readerContext.indexShard()), () -> {
        final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(null);
        try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, false);
            SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(searchContext)) {
            searchContext.assignRescoreDocIds(readerContext.getRescoreDocIds(null));
            searchContext.searcher().setAggregatedDfs(readerContext.getAggregatedDfs(null));
            processScroll(request, readerContext, searchContext);
            queryPhase.execute(searchContext);
            final long afterQueryTime = executor.success();
            QueryFetchSearchResult fetchSearchResult = executeFetchPhase(readerContext, searchContext, afterQueryTime);
            return new ScrollQueryFetchSearchResult(fetchSearchResult, searchContext.shardTarget());
        } catch (Exception e) {
            assert TransportActions.isShardNotAvailableException(e) == false : new AssertionError(e);
            logger.trace("Fetch phase failed", e);
            // we handle the failure in the failure listener below
            throw e;
        }
    }, wrapFailureListener(listener, readerContext, markAsUsed));
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) ScrollQueryFetchSearchResult(org.opensearch.search.fetch.ScrollQueryFetchSearchResult) QueryFetchSearchResult(org.opensearch.search.fetch.QueryFetchSearchResult) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) SearchContext(org.opensearch.search.internal.SearchContext) Releasable(org.opensearch.common.lease.Releasable) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AggregationInitializationException(org.opensearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OpenSearchException(org.opensearch.OpenSearchException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ScrollQueryFetchSearchResult(org.opensearch.search.fetch.ScrollQueryFetchSearchResult)

Aggregations

ShardSearchRequest (org.opensearch.search.internal.ShardSearchRequest)35 AliasFilter (org.opensearch.search.internal.AliasFilter)17 IOException (java.io.IOException)16 IndexService (org.opensearch.index.IndexService)14 IndexShard (org.opensearch.index.shard.IndexShard)14 ShardId (org.opensearch.index.shard.ShardId)13 SearchRequest (org.opensearch.action.search.SearchRequest)12 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ExecutionException (java.util.concurrent.ExecutionException)11 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)11 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)11 ReaderContext (org.opensearch.search.internal.ReaderContext)11 SearchContext (org.opensearch.search.internal.SearchContext)11 IndicesService (org.opensearch.indices.IndicesService)10 OriginalIndices (org.opensearch.action.OriginalIndices)8 OpenSearchException (org.opensearch.OpenSearchException)7 ActionListener (org.opensearch.action.ActionListener)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6