Search in sources :

Example 1 with ReaderContext

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

the class SearchService method executeQueryPhase.

private SearchPhaseResult executeQueryPhase(ShardSearchRequest request, SearchShardTask task, boolean keepStatesInContext) throws Exception {
    final ReaderContext readerContext = createOrGetReaderContext(request, keepStatesInContext);
    try (Releasable ignored = readerContext.markAsUsed(getKeepAlive(request));
        SearchContext context = createContext(readerContext, request, task, true)) {
        final long afterQueryTime;
        try (SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(context)) {
            loadOrExecuteQueryPhase(request, context);
            if (context.queryResult().hasSearchContext() == false && readerContext.singleSession()) {
                freeReaderContext(readerContext.id());
            }
            afterQueryTime = executor.success();
        }
        if (request.numberOfShards() == 1) {
            return executeFetchPhase(readerContext, context, afterQueryTime);
        } else {
            // 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 = context.rescoreDocIds();
            context.queryResult().setRescoreDocIds(rescoreDocIds);
            readerContext.setRescoreDocIds(rescoreDocIds);
            return context.queryResult();
        }
    } catch (Exception e) {
        // execution exception can happen while loading the cache, strip it
        if (e instanceof ExecutionException) {
            e = (e.getCause() == null || e.getCause() instanceof Exception) ? (Exception) e.getCause() : new OpenSearchException(e.getCause());
        }
        logger.trace("Query phase failed", e);
        processFailure(readerContext, e);
        throw e;
    }
}
Also used : LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) SearchContext(org.opensearch.search.internal.SearchContext) Releasable(org.opensearch.common.lease.Releasable) OpenSearchException(org.opensearch.OpenSearchException) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) 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 2 with ReaderContext

use of org.opensearch.search.internal.ReaderContext 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 3 with ReaderContext

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

the class SearchService method createSearchContext.

public DefaultSearchContext createSearchContext(ShardSearchRequest request, TimeValue timeout) throws IOException {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    final IndexShard indexShard = indexService.getShard(request.shardId().getId());
    final Engine.SearcherSupplier reader = indexShard.acquireSearcherSupplier();
    final ShardSearchContextId id = new ShardSearchContextId(sessionId, idGenerator.incrementAndGet());
    try (ReaderContext readerContext = new ReaderContext(id, indexService, indexShard, reader, -1L, true)) {
        DefaultSearchContext searchContext = createSearchContext(readerContext, request, timeout);
        searchContext.addReleasable(readerContext.markAsUsed(0L));
        return searchContext;
    }
}
Also used : ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) Engine(org.opensearch.index.engine.Engine)

Example 4 with ReaderContext

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

the class SearchService method createAndPutReaderContext.

final ReaderContext createAndPutReaderContext(ShardSearchRequest request, IndexService indexService, IndexShard shard, Engine.SearcherSupplier reader, boolean keepStatesInContext) {
    assert request.readerId() == null;
    assert request.keepAlive() == null;
    ReaderContext readerContext = null;
    Releasable decreaseScrollContexts = null;
    try {
        if (request.scroll() != null) {
            decreaseScrollContexts = openScrollContexts::decrementAndGet;
            if (openScrollContexts.incrementAndGet() > maxOpenScrollContext) {
                throw new OpenSearchRejectedExecutionException("Trying to create too many scroll contexts. Must be less than or equal to: [" + maxOpenScrollContext + "]. " + "This limit can be set by changing the [" + MAX_OPEN_SCROLL_CONTEXT.getKey() + "] setting.");
            }
        }
        final long keepAlive = getKeepAlive(request);
        final ShardSearchContextId id = new ShardSearchContextId(sessionId, idGenerator.incrementAndGet());
        if (keepStatesInContext || request.scroll() != null) {
            readerContext = new LegacyReaderContext(id, indexService, shard, reader, request, keepAlive);
            if (request.scroll() != null) {
                readerContext.addOnClose(decreaseScrollContexts);
                decreaseScrollContexts = null;
            }
        } else {
            readerContext = new ReaderContext(id, indexService, shard, reader, keepAlive, request.keepAlive() == null);
        }
        reader = null;
        final ReaderContext finalReaderContext = readerContext;
        final SearchOperationListener searchOperationListener = shard.getSearchOperationListener();
        searchOperationListener.onNewReaderContext(finalReaderContext);
        if (finalReaderContext.scrollContext() != null) {
            searchOperationListener.onNewScrollContext(finalReaderContext);
        }
        readerContext.addOnClose(() -> {
            try {
                if (finalReaderContext.scrollContext() != null) {
                    searchOperationListener.onFreeScrollContext(finalReaderContext);
                }
            } finally {
                searchOperationListener.onFreeReaderContext(finalReaderContext);
            }
        });
        putReaderContext(finalReaderContext);
        readerContext = null;
        return finalReaderContext;
    } finally {
        Releasables.close(reader, readerContext, decreaseScrollContexts);
    }
}
Also used : ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) Releasable(org.opensearch.common.lease.Releasable) SearchOperationListener(org.opensearch.index.shard.SearchOperationListener)

Example 5 with ReaderContext

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

the class SearchServiceTests method testMaxDocvalueFieldsSearch.

/**
 * test that getting more than the allowed number of docvalue_fields throws an exception
 */
public void testMaxDocvalueFieldsSearch() throws IOException {
    createIndex("index");
    final SearchService service = getInstanceFromNode(SearchService.class);
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    final IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    final IndexShard indexShard = indexService.getShard(0);
    SearchRequest searchRequest = new SearchRequest().allowPartialSearchResults(true);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchRequest.source(searchSourceBuilder);
    // adding the maximum allowed number of docvalue_fields to retrieve
    for (int i = 0; i < indexService.getIndexSettings().getMaxDocvalueFields(); i++) {
        searchSourceBuilder.docValueField("field" + i);
    }
    final ShardSearchRequest request = new ShardSearchRequest(OriginalIndices.NONE, searchRequest, indexShard.shardId(), 1, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f, -1, null, null);
    try (ReaderContext reader = createReaderContext(indexService, indexShard);
        SearchContext context = service.createContext(reader, request, null, randomBoolean())) {
        assertNotNull(context);
    }
    searchSourceBuilder.docValueField("one_field_too_much");
    try (ReaderContext reader = createReaderContext(indexService, indexShard)) {
        IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> service.createContext(reader, request, null, randomBoolean()));
        assertEquals("Trying to retrieve too many docvalue_fields. Must be less than or equal to: [100] but was [101]. " + "This limit can be set by changing the [index.max_docvalue_fields_search] index level setting.", ex.getMessage());
    }
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) AliasFilter(org.opensearch.search.internal.AliasFilter) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) ReaderContext(org.opensearch.search.internal.ReaderContext) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) IndicesService(org.opensearch.indices.IndicesService) SearchContext(org.opensearch.search.internal.SearchContext) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

ReaderContext (org.opensearch.search.internal.ReaderContext)19 IndexService (org.opensearch.index.IndexService)11 IndexShard (org.opensearch.index.shard.IndexShard)10 LegacyReaderContext (org.opensearch.search.internal.LegacyReaderContext)10 ShardSearchRequest (org.opensearch.search.internal.ShardSearchRequest)10 SearchContext (org.opensearch.search.internal.SearchContext)8 IOException (java.io.IOException)5 SearchRequest (org.opensearch.action.search.SearchRequest)5 Releasable (org.opensearch.common.lease.Releasable)5 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)5 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)5 Engine (org.opensearch.index.engine.Engine)5 IndicesService (org.opensearch.indices.IndicesService)5 AliasFilter (org.opensearch.search.internal.AliasFilter)5 ExecutionException (java.util.concurrent.ExecutionException)4 OpenSearchException (org.opensearch.OpenSearchException)4 AggregationInitializationException (org.opensearch.search.aggregations.AggregationInitializationException)4 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)4 QueryShardContext (org.opensearch.index.query.QueryShardContext)3 SearchOperationListener (org.opensearch.index.shard.SearchOperationListener)3