Search in sources :

Example 31 with SearchContext

use of org.elasticsearch.search.internal.SearchContext in project elasticsearch by elastic.

the class SearchService method createAndPutContext.

final SearchContext createAndPutContext(ShardSearchRequest request) throws IOException {
    SearchContext context = createContext(request, null);
    boolean success = false;
    try {
        putContext(context);
        if (request.scroll() != null) {
            context.indexShard().getSearchOperationListener().onNewScrollContext(context);
        }
        context.indexShard().getSearchOperationListener().onNewContext(context);
        success = true;
        return context;
    } finally {
        if (!success) {
            freeContext(context.id());
        }
    }
}
Also used : SearchContext(org.elasticsearch.search.internal.SearchContext)

Example 32 with SearchContext

use of org.elasticsearch.search.internal.SearchContext 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)

Example 33 with SearchContext

use of org.elasticsearch.search.internal.SearchContext in project elasticsearch by elastic.

the class SearchService method executeQueryPhase.

public QuerySearchResultProvider executeQueryPhase(ShardSearchRequest request, SearchTask task) throws IOException {
    final SearchContext context = createAndPutContext(request);
    final SearchOperationListener operationListener = context.indexShard().getSearchOperationListener();
    context.incRef();
    try {
        context.setTask(task);
        operationListener.onPreQueryPhase(context);
        long time = System.nanoTime();
        contextProcessing(context);
        loadOrExecuteQueryPhase(request, context);
        if (context.queryResult().hasHits() == false && context.scrollContext() == null) {
            freeContext(context.id());
        } else {
            contextProcessedSuccessfully(context);
        }
        final long afterQueryTime = System.nanoTime();
        operationListener.onQueryPhase(context, afterQueryTime - time);
        if (request.numberOfShards() == 1) {
            return executeFetchPhase(context, operationListener, afterQueryTime);
        }
        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 ElasticsearchException(e.getCause());
        }
        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) ElasticsearchException(org.elasticsearch.ElasticsearchException) SearchOperationListener(org.elasticsearch.index.shard.SearchOperationListener) ExecutionException(java.util.concurrent.ExecutionException) ElasticsearchException(org.elasticsearch.ElasticsearchException) AggregationInitializationException(org.elasticsearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

SearchContext (org.elasticsearch.search.internal.SearchContext)33 IOException (java.io.IOException)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)7 ExecutionException (java.util.concurrent.ExecutionException)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 SearchOperationListener (org.elasticsearch.index.shard.SearchOperationListener)6 AggregationInitializationException (org.elasticsearch.search.aggregations.AggregationInitializationException)6 TestSearchContext (org.elasticsearch.test.TestSearchContext)6 ArrayList (java.util.ArrayList)5 FetchSubPhase (org.elasticsearch.search.fetch.FetchSubPhase)5 Term (org.apache.lucene.index.Term)4 Encoder (org.apache.lucene.search.highlight.Encoder)4 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)4 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)4 FetchPhaseExecutionException (org.elasticsearch.search.fetch.FetchPhaseExecutionException)4 Analyzer (org.apache.lucene.analysis.Analyzer)3 TermQuery (org.apache.lucene.search.TermQuery)3 InnerHitsContext (org.elasticsearch.search.fetch.subphase.InnerHitsContext)3 Document (org.apache.lucene.document.Document)2 IndexReader (org.apache.lucene.index.IndexReader)2