Search in sources :

Example 1 with QueryFetchSearchResult

use of org.elasticsearch.search.fetch.QueryFetchSearchResult in project elasticsearch by elastic.

the class SearchService method executeFetchPhase.

public ScrollQueryFetchSearchResult executeFetchPhase(InternalScrollSearchRequest request, SearchTask task) {
    final SearchContext context = findContext(request.id());
    context.incRef();
    try {
        context.setTask(task);
        contextProcessing(context);
        SearchOperationListener operationListener = context.indexShard().getSearchOperationListener();
        processScroll(request, context);
        operationListener.onPreQueryPhase(context);
        final long time = System.nanoTime();
        try {
            queryPhase.execute(context);
        } catch (Exception e) {
            operationListener.onFailedQueryPhase(context);
            throw ExceptionsHelper.convertToRuntime(e);
        }
        long afterQueryTime = System.nanoTime();
        operationListener.onQueryPhase(context, afterQueryTime - time);
        QueryFetchSearchResult fetchSearchResult = executeFetchPhase(context, operationListener, afterQueryTime);
        return new ScrollQueryFetchSearchResult(fetchSearchResult, context.shardTarget());
    } catch (Exception e) {
        logger.trace("Fetch phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
Also used : ScrollQueryFetchSearchResult(org.elasticsearch.search.fetch.ScrollQueryFetchSearchResult) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchOperationListener(org.elasticsearch.index.shard.SearchOperationListener) ElasticsearchException(org.elasticsearch.ElasticsearchException) AggregationInitializationException(org.elasticsearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ScrollQueryFetchSearchResult(org.elasticsearch.search.fetch.ScrollQueryFetchSearchResult)

Example 2 with QueryFetchSearchResult

use of org.elasticsearch.search.fetch.QueryFetchSearchResult in project elasticsearch by elastic.

the class FetchSearchPhaseTests method testShortcutQueryAndFetchOptimization.

public void testShortcutQueryAndFetchOptimization() throws IOException {
    SearchPhaseController controller = new SearchPhaseController(Settings.EMPTY, BigArrays.NON_RECYCLING_INSTANCE, null);
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> results = controller.newSearchPhaseResults(mockSearchPhaseContext.getRequest(), 1);
    AtomicReference<SearchResponse> responseRef = new AtomicReference<>();
    boolean hasHits = randomBoolean();
    final int numHits;
    if (hasHits) {
        QuerySearchResult queryResult = new QuerySearchResult();
        queryResult.topDocs(new TopDocs(1, 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) }, 1, 1.0F));
        results.consumeResult(0, new QueryFetchSearchResult(queryResult, fetchResult));
        numHits = 1;
    } else {
        numHits = 0;
    }
    FetchSearchPhase phase = new FetchSearchPhase(results, controller, mockSearchPhaseContext, (searchResponse) -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            responseRef.set(searchResponse);
        }
    });
    assertEquals("fetch", phase.getName());
    phase.run();
    mockSearchPhaseContext.assertNoFailure();
    assertNotNull(responseRef.get());
    assertEquals(numHits, responseRef.get().getHits().totalHits);
    if (numHits != 0) {
        assertEquals(42, responseRef.get().getHits().getAt(0).docId());
    }
    assertTrue(mockSearchPhaseContext.releasedSearchContexts.isEmpty());
}
Also used : QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) SearchHit(org.elasticsearch.search.SearchHit) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchHits(org.elasticsearch.search.SearchHits)

Aggregations

IOException (java.io.IOException)2 QueryFetchSearchResult (org.elasticsearch.search.fetch.QueryFetchSearchResult)2 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1 TopDocs (org.apache.lucene.search.TopDocs)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 SearchOperationListener (org.elasticsearch.index.shard.SearchOperationListener)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1 AggregationInitializationException (org.elasticsearch.search.aggregations.AggregationInitializationException)1 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)1 ScrollQueryFetchSearchResult (org.elasticsearch.search.fetch.ScrollQueryFetchSearchResult)1 SearchContext (org.elasticsearch.search.internal.SearchContext)1 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)1 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)1