Search in sources :

Example 1 with QueryFetchSearchResult

use of org.opensearch.search.fetch.QueryFetchSearchResult 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)

Example 2 with QueryFetchSearchResult

use of org.opensearch.search.fetch.QueryFetchSearchResult in project OpenSearch by opensearch-project.

the class SearchTransportService method sendExecuteQuery.

public void sendExecuteQuery(Transport.Connection connection, final ShardSearchRequest request, SearchTask task, final SearchActionListener<SearchPhaseResult> listener) {
    // we optimize this and expect a QueryFetchSearchResult if we only have a single shard in the search request
    // this used to be the QUERY_AND_FETCH which doesn't exist anymore.
    final boolean fetchDocuments = request.numberOfShards() == 1;
    Writeable.Reader<SearchPhaseResult> reader = fetchDocuments ? QueryFetchSearchResult::new : QuerySearchResult::new;
    final ActionListener handler = responseWrapper.apply(connection, listener);
    transportService.sendChildRequest(connection, QUERY_ACTION_NAME, request, task, new ConnectionCountingHandler<>(handler, reader, clientConnections, connection.getNode().getId()));
}
Also used : ActionListener(org.opensearch.action.ActionListener) ChannelActionListener(org.opensearch.action.support.ChannelActionListener) QuerySearchResult(org.opensearch.search.query.QuerySearchResult) ScrollQuerySearchResult(org.opensearch.search.query.ScrollQuerySearchResult) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) ScrollQueryFetchSearchResult(org.opensearch.search.fetch.ScrollQueryFetchSearchResult) QueryFetchSearchResult(org.opensearch.search.fetch.QueryFetchSearchResult) Writeable(org.opensearch.common.io.stream.Writeable)

Example 3 with QueryFetchSearchResult

use of org.opensearch.search.fetch.QueryFetchSearchResult in project OpenSearch by opensearch-project.

the class SearchExecutionStatsCollector method onResponse.

@Override
public void onResponse(SearchPhaseResult response) {
    if (response instanceof QueryFetchSearchResult) {
        response.queryResult().getShardSearchRequest().setOutboundNetworkTime(0);
        response.queryResult().getShardSearchRequest().setInboundNetworkTime(0);
    }
    QuerySearchResult queryResult = response.queryResult();
    if (response.getShardSearchRequest() != null) {
        if (response.remoteAddress() != null) {
            // update outbound network time for request sent over network for shard requests
            response.getShardSearchRequest().setOutboundNetworkTime(Math.max(0, System.currentTimeMillis() - response.getShardSearchRequest().getOutboundNetworkTime()));
        } else {
            // reset inbound and outbound network time to 0 for local request for shard requests
            response.getShardSearchRequest().setOutboundNetworkTime(0);
            response.getShardSearchRequest().setInboundNetworkTime(0);
        }
    }
    if (nodeId != null && queryResult != null) {
        final long serviceTimeEWMA = queryResult.serviceTimeEWMA();
        final int queueSize = queryResult.nodeQueueSize();
        final long responseDuration = System.nanoTime() - startNanos;
        // EWMA/queue size may be -1 if the query node doesn't support capturing it
        if (serviceTimeEWMA > 0 && queueSize >= 0) {
            collector.addNodeStatistics(nodeId, queueSize, responseDuration, serviceTimeEWMA);
        }
    }
    listener.onResponse(response);
}
Also used : QuerySearchResult(org.opensearch.search.query.QuerySearchResult) QueryFetchSearchResult(org.opensearch.search.fetch.QueryFetchSearchResult)

Example 4 with QueryFetchSearchResult

use of org.opensearch.search.fetch.QueryFetchSearchResult in project OpenSearch by opensearch-project.

the class FetchSearchPhaseTests method testShortcutQueryAndFetchOptimization.

public void testShortcutQueryAndFetchOptimization() {
    SearchPhaseController controller = new SearchPhaseController(writableRegistry(), s -> InternalAggregationTestCase.emptyReduceContextBuilder());
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    QueryPhaseResultConsumer results = controller.newSearchPhaseResults(OpenSearchExecutors.newDirectExecutorService(), new NoopCircuitBreaker(CircuitBreaker.REQUEST), SearchProgressListener.NOOP, mockSearchPhaseContext.getRequest(), 1, exc -> {
    });
    boolean hasHits = randomBoolean();
    final int numHits;
    if (hasHits) {
        QuerySearchResult queryResult = new QuerySearchResult();
        queryResult.setSearchShardTarget(new SearchShardTarget("node0", new ShardId("index", "index", 0), null, OriginalIndices.NONE));
        queryResult.topDocs(new TopDocsAndMaxScore(new TopDocs(new TotalHits(1, TotalHits.Relation.EQUAL_TO), new ScoreDoc[] { new ScoreDoc(42, 1.0F) }), 1.0F), new DocValueFormat[0]);
        queryResult.size(1);
        FetchSearchResult fetchResult = new FetchSearchResult();
        fetchResult.hits(new SearchHits(new SearchHit[] { new SearchHit(42) }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F));
        QueryFetchSearchResult fetchSearchResult = new QueryFetchSearchResult(queryResult, fetchResult);
        fetchSearchResult.setShardIndex(0);
        results.consumeResult(fetchSearchResult, () -> {
        });
        numHits = 1;
    } else {
        numHits = 0;
    }
    FetchSearchPhase phase = new FetchSearchPhase(results, controller, null, mockSearchPhaseContext, (searchResponse, scrollId) -> new SearchPhase("test") {

        @Override
        public void run() {
            mockSearchPhaseContext.sendSearchResponse(searchResponse, null);
        }
    });
    assertEquals("fetch", phase.getName());
    phase.run();
    mockSearchPhaseContext.assertNoFailure();
    SearchResponse searchResponse = mockSearchPhaseContext.searchResponse.get();
    assertNotNull(searchResponse);
    assertEquals(numHits, searchResponse.getHits().getTotalHits().value);
    if (numHits != 0) {
        assertEquals(42, searchResponse.getHits().getAt(0).docId());
    }
    assertTrue(mockSearchPhaseContext.releasedSearchContexts.isEmpty());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) SearchHit(org.opensearch.search.SearchHit) QueryFetchSearchResult(org.opensearch.search.fetch.QueryFetchSearchResult) FetchSearchResult(org.opensearch.search.fetch.FetchSearchResult) QueryFetchSearchResult(org.opensearch.search.fetch.QueryFetchSearchResult) TopDocsAndMaxScore(org.opensearch.common.lucene.search.TopDocsAndMaxScore) ScoreDoc(org.apache.lucene.search.ScoreDoc) ShardId(org.opensearch.index.shard.ShardId) TopDocs(org.apache.lucene.search.TopDocs) QuerySearchResult(org.opensearch.search.query.QuerySearchResult) SearchShardTarget(org.opensearch.search.SearchShardTarget) SearchHits(org.opensearch.search.SearchHits) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker)

Aggregations

QueryFetchSearchResult (org.opensearch.search.fetch.QueryFetchSearchResult)4 QuerySearchResult (org.opensearch.search.query.QuerySearchResult)3 ScrollQueryFetchSearchResult (org.opensearch.search.fetch.ScrollQueryFetchSearchResult)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1 TopDocs (org.apache.lucene.search.TopDocs)1 TotalHits (org.apache.lucene.search.TotalHits)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 ChannelActionListener (org.opensearch.action.support.ChannelActionListener)1 NoopCircuitBreaker (org.opensearch.common.breaker.NoopCircuitBreaker)1 Writeable (org.opensearch.common.io.stream.Writeable)1 Releasable (org.opensearch.common.lease.Releasable)1 TopDocsAndMaxScore (org.opensearch.common.lucene.search.TopDocsAndMaxScore)1 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)1 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)1 ShardId (org.opensearch.index.shard.ShardId)1 SearchHit (org.opensearch.search.SearchHit)1 SearchHits (org.opensearch.search.SearchHits)1