Java Code Examples for org.elasticsearch.ExceptionsHelper#convertToRuntime()

The following examples show how to use org.elasticsearch.ExceptionsHelper#convertToRuntime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private Query queryFromInnerFunction(Function function, Context context) {
    for (Symbol symbol : function.arguments()) {
        if (symbol.symbolType() == SymbolType.FUNCTION) {
            String functionName = ((Function) symbol).info().ident().name();
            InnerFunctionToQuery functionToQuery = innerFunctions.get(functionName);
            if (functionToQuery != null) {
                try {
                    Query query = functionToQuery.apply(function, (Function)symbol, context);
                    if (query != null) {
                        return query;
                    }
                } catch (IOException e) {
                    throw ExceptionsHelper.convertToRuntime(e);
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public ScrollQuerySearchResult executeQueryPhase(InternalScrollSearchRequest request) {
    final SearchContext context = findContext(request.id());
    ShardSearchStats shardSearchStats = context.indexShard().searchService();
    try {
        shardSearchStats.onPreQueryPhase(context);
        long time = System.nanoTime();
        contextProcessing(context);
        processScroll(request, context);
        queryPhase.execute(context);
        contextProcessedSuccessfully(context);
        shardSearchStats.onQueryPhase(context, System.nanoTime() - time);
        return new ScrollQuerySearchResult(context.queryResult(), context.shardTarget());
    } catch (Throwable e) {
        shardSearchStats.onFailedQueryPhase(context);
        logger.trace("Query phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
 
Example 3
Source File: LuceneQueryBuilder.java    From crate with Apache License 2.0 6 votes vote down vote up
private Query queryFromInnerFunction(Function function, Context context) {
    for (Symbol symbol : function.arguments()) {
        if (symbol.symbolType() == SymbolType.FUNCTION) {
            String functionName = ((Function) symbol).name();
            InnerFunctionToQuery functionToQuery = innerFunctions.get(functionName);
            if (functionToQuery != null) {
                try {
                    Query query = functionToQuery.apply(function, (Function) symbol, context);
                    if (query != null) {
                        return query;
                    }
                } catch (IOException e) {
                    throw ExceptionsHelper.convertToRuntime(e);
                }
            }
        }
    }
    return null;
}
 
Example 4
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public DfsSearchResult executeDfsPhase(ShardSearchRequest request) {
    final SearchContext context = createAndPutContext(request);
    try {
        contextProcessing(context);
        dfsPhase.execute(context);
        contextProcessedSuccessfully(context);
        return context.dfsResult();
    } catch (Throwable e) {
        logger.trace("Dfs phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
 
Example 5
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public QuerySearchResultProvider executeQueryPhase(ShardSearchRequest request) {
    final SearchContext context = createAndPutContext(request);
    final ShardSearchStats shardSearchStats = context.indexShard().searchService();
    try {
        shardSearchStats.onPreQueryPhase(context);
        long time = System.nanoTime();
        contextProcessing(context);

        loadOrExecuteQueryPhase(request, context, queryPhase);

        if (context.queryResult().topDocs().scoreDocs.length == 0 && context.scrollContext() == null) {
            freeContext(context.id());
        } else {
            contextProcessedSuccessfully(context);
        }
        shardSearchStats.onQueryPhase(context, System.nanoTime() - time);

        return context.queryResult();
    } catch (Throwable e) {
        // execution exception can happen while loading the cache, strip it
        if (e instanceof ExecutionException) {
            e = e.getCause();
        }
        shardSearchStats.onFailedQueryPhase(context);
        logger.trace("Query phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
 
Example 6
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public QuerySearchResult executeQueryPhase(QuerySearchRequest request) {
    final SearchContext context = findContext(request.id());
    contextProcessing(context);
    context.searcher().setAggregatedDfs(request.dfs());
    IndexShard indexShard = context.indexShard();
    ShardSearchStats shardSearchStats = indexShard.searchService();
    try {
        shardSearchStats.onPreQueryPhase(context);
        long time = System.nanoTime();
        queryPhase.execute(context);
        if (context.queryResult().topDocs().scoreDocs.length == 0 && context.scrollContext() == null) {
            // no hits, we can release the context since there will be no fetch phase
            freeContext(context.id());
        } else {
            contextProcessedSuccessfully(context);
        }
        shardSearchStats.onQueryPhase(context, System.nanoTime() - time);
        return context.queryResult();
    } catch (Throwable e) {
        shardSearchStats.onFailedQueryPhase(context);
        logger.trace("Query phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
 
Example 7
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public FetchSearchResult executeFetchPhase(ShardFetchRequest request) {
    final SearchContext context = findContext(request.id());
    contextProcessing(context);
    final ShardSearchStats shardSearchStats = context.indexShard().searchService();
    try {
        if (request.lastEmittedDoc() != null) {
            context.scrollContext().lastEmittedDoc = request.lastEmittedDoc();
        }
        context.docIdsToLoad(request.docIds(), 0, request.docIdsSize());
        shardSearchStats.onPreFetchPhase(context);
        long time = System.nanoTime();
        fetchPhase.execute(context);
        if (fetchPhaseShouldFreeContext(context)) {
            freeContext(request.id());
        } else {
            contextProcessedSuccessfully(context);
        }
        shardSearchStats.onFetchPhase(context, System.nanoTime() - time);
        return context.fetchResult();
    } catch (Throwable e) {
        shardSearchStats.onFailedFetchPhase(context);
        logger.trace("Fetch phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
 
Example 8
Source File: IndexShard.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Replays translog operations from the provided translog {@code snapshot} to the current engine using the given {@code origin}.
 * The callback {@code onOperationRecovered} is notified after each translog operation is replayed successfully.
 */
int runTranslogRecovery(Engine engine, Translog.Snapshot snapshot, Engine.Operation.Origin origin,
                        Runnable onOperationRecovered) throws IOException {
    int opsRecovered = 0;
    Translog.Operation operation;
    while ((operation = snapshot.next()) != null) {
        try {
            logger.trace("[translog] recover op {}", operation);
            Engine.Result result = applyTranslogOperation(engine, operation, origin);
            switch (result.getResultType()) {
                case FAILURE:
                    throw result.getFailure();
                case MAPPING_UPDATE_REQUIRED:
                    throw new IllegalArgumentException("unexpected mapping update: " + result.getRequiredMappingUpdate());
                case SUCCESS:
                    break;
                default:
                    throw new AssertionError("Unknown result type [" + result.getResultType() + "]");
            }

            opsRecovered++;
            onOperationRecovered.run();
        } catch (Exception e) {
            if (ExceptionsHelper.status(e) == RestStatus.BAD_REQUEST) {
                // mainly for MapperParsingException and Failure to detect xcontent
                logger.info("ignoring recovery of a corrupt translog entry", e);
            } else {
                throw ExceptionsHelper.convertToRuntime(e);
            }
        }
    }
    return opsRecovered;
}
 
Example 9
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
final SearchContext createContext(ShardSearchRequest request, @Nullable Engine.Searcher searcher) {
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(request.shardId());

    SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.index(), request.shardId());
    String searchSource = "search";
    if (request.hasHeader("search_source")) {
        searchSource = request.getHeader("search_source");
    }
    Engine.Searcher engineSearcher = searcher == null ? indexShard.acquireSearcher(searchSource) : searcher;

    DefaultSearchContext context = new DefaultSearchContext(idGenerator.incrementAndGet(), request, shardTarget, engineSearcher, indexService, indexShard, scriptService, pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher, defaultSearchTimeout);
    SearchContext.setCurrent(context);
    try {
        if (request.scroll() != null) {
            context.scrollContext(new ScrollContext());
            context.scrollContext().scroll = request.scroll();
        }

        parseTemplate(request, context);
        parseSource(context, request.source());
        parseSource(context, request.extraSource());

        // if the from and size are still not set, default them
        if (context.from() == -1) {
            context.from(0);
        }
        if (context.searchType() == SearchType.COUNT) {
            // so that the optimizations we apply to size=0 also apply to search_type=COUNT
            // and that we close contexts when done with the query phase
            context.searchType(SearchType.QUERY_THEN_FETCH);
            context.size(0);
        } else if (context.size() == -1) {
            context.size(10);
        }

        if (context.request().isProfile()) {
            context.setProfilers(new Profilers(context.searcher()));
        }

        // pre process
        dfsPhase.preProcess(context);
        queryPhase.preProcess(context);
        fetchPhase.preProcess(context);

        // compute the context keep alive
        long keepAlive = defaultKeepAlive;
        if (request.scroll() != null && request.scroll().keepAlive() != null) {
            keepAlive = request.scroll().keepAlive().millis();
        }
        context.keepAlive(keepAlive);
    } catch (Throwable e) {
        context.close();
        throw ExceptionsHelper.convertToRuntime(e);
    }

    return context;
}