Search in sources :

Example 1 with SearchOperationListener

use of org.elasticsearch.index.shard.SearchOperationListener in project elasticsearch by elastic.

the class SearchService method executeFetchPhase.

public FetchSearchResult executeFetchPhase(ShardFetchRequest request, SearchTask task) {
    final SearchContext context = findContext(request.id());
    final SearchOperationListener operationListener = context.indexShard().getSearchOperationListener();
    context.incRef();
    try {
        context.setTask(task);
        contextProcessing(context);
        if (request.lastEmittedDoc() != null) {
            context.scrollContext().lastEmittedDoc = request.lastEmittedDoc();
        }
        context.docIdsToLoad(request.docIds(), 0, request.docIdsSize());
        operationListener.onPreFetchPhase(context);
        long time = System.nanoTime();
        fetchPhase.execute(context);
        if (fetchPhaseShouldFreeContext(context)) {
            freeContext(request.id());
        } else {
            contextProcessedSuccessfully(context);
        }
        operationListener.onFetchPhase(context, System.nanoTime() - time);
        return context.fetchResult();
    } catch (Exception e) {
        operationListener.onFailedFetchPhase(context);
        logger.trace("Fetch phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
Also used : 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)

Example 2 with SearchOperationListener

use of org.elasticsearch.index.shard.SearchOperationListener in project elasticsearch by elastic.

the class SearchService method executeQueryPhase.

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

Example 3 with SearchOperationListener

use of org.elasticsearch.index.shard.SearchOperationListener 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 4 with SearchOperationListener

use of org.elasticsearch.index.shard.SearchOperationListener in project elasticsearch by elastic.

the class IndexModuleTests method testAddSearchOperationListener.

public void testAddSearchOperationListener() throws IOException {
    IndexModule module = new IndexModule(IndexSettingsModule.newIndexSettings(index, settings), new AnalysisRegistry(environment, emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()));
    AtomicBoolean executed = new AtomicBoolean(false);
    SearchOperationListener listener = new SearchOperationListener() {

        @Override
        public void onNewContext(SearchContext context) {
            executed.set(true);
        }
    };
    module.addSearchOperationListener(listener);
    expectThrows(IllegalArgumentException.class, () -> module.addSearchOperationListener(listener));
    expectThrows(IllegalArgumentException.class, () -> module.addSearchOperationListener(null));
    IndexService indexService = newIndexService(module);
    assertEquals(2, indexService.getSearchOperationListener().size());
    assertEquals(SearchSlowLog.class, indexService.getSearchOperationListener().get(0).getClass());
    assertSame(listener, indexService.getSearchOperationListener().get(1));
    for (SearchOperationListener l : indexService.getSearchOperationListener()) {
        l.onNewContext(new TestSearchContext(null));
    }
    assertTrue(executed.get());
    indexService.close("simon says", false);
}
Also used : AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSearchContext(org.elasticsearch.test.TestSearchContext) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchOperationListener(org.elasticsearch.index.shard.SearchOperationListener)

Example 5 with SearchOperationListener

use of org.elasticsearch.index.shard.SearchOperationListener in project elasticsearch by elastic.

the class SearchService method executeQueryPhase.

public QuerySearchResult executeQueryPhase(QuerySearchRequest request, SearchTask task) {
    final SearchContext context = findContext(request.id());
    context.setTask(task);
    IndexShard indexShard = context.indexShard();
    SearchOperationListener operationListener = indexShard.getSearchOperationListener();
    context.incRef();
    try {
        contextProcessing(context);
        context.searcher().setAggregatedDfs(request.dfs());
        operationListener.onPreQueryPhase(context);
        long time = System.nanoTime();
        queryPhase.execute(context);
        if (context.queryResult().hasHits() == false && context.scrollContext() == null) {
            // no hits, we can release the context since there will be no fetch phase
            freeContext(context.id());
        } else {
            contextProcessedSuccessfully(context);
        }
        operationListener.onQueryPhase(context, System.nanoTime() - time);
        return context.queryResult();
    } catch (Exception e) {
        operationListener.onFailedQueryPhase(context);
        logger.trace("Query phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) 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)

Aggregations

SearchOperationListener (org.elasticsearch.index.shard.SearchOperationListener)6 SearchContext (org.elasticsearch.search.internal.SearchContext)6 IOException (java.io.IOException)5 ExecutionException (java.util.concurrent.ExecutionException)5 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 AggregationInitializationException (org.elasticsearch.search.aggregations.AggregationInitializationException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AnalysisRegistry (org.elasticsearch.index.analysis.AnalysisRegistry)1 IndexShard (org.elasticsearch.index.shard.IndexShard)1 QueryFetchSearchResult (org.elasticsearch.search.fetch.QueryFetchSearchResult)1 ScrollQueryFetchSearchResult (org.elasticsearch.search.fetch.ScrollQueryFetchSearchResult)1 ScrollQuerySearchResult (org.elasticsearch.search.query.ScrollQuerySearchResult)1 TestSearchContext (org.elasticsearch.test.TestSearchContext)1