use of org.graylog.plugins.views.search.QueryResult in project graylog2-server by Graylog2.
the class QueryEngine method prepareAndRun.
private QueryResult prepareAndRun(SearchJob searchJob, Query query) {
final QueryBackend<? extends GeneratedQueryContext> backend = getQueryBackend(query);
LOG.debug("[{}] Using {} to generate query", query.id(), backend);
// with all the results done, we can execute the current query and eventually complete our own result
// if any of this throws an exception, the handle in #execute will convert it to an error and return a "failed" result instead
// if the backend already returns a "failed result" then nothing special happens here
final GeneratedQueryContext generatedQueryContext = backend.generate(searchJob, query, searchConfig.get());
LOG.trace("[{}] Generated query {}, running it on backend {}", query.id(), generatedQueryContext, backend);
final QueryResult result = backend.run(searchJob, query, generatedQueryContext);
LOG.debug("[{}] Query returned {}", query.id(), result);
if (!generatedQueryContext.errors().isEmpty()) {
generatedQueryContext.errors().forEach(searchJob::addError);
}
return result;
}
use of org.graylog.plugins.views.search.QueryResult in project graylog2-server by Graylog2.
the class SearchResource method extractSearchResponse.
protected SearchResponse extractSearchResponse(SearchJob searchJob, String query, boolean decorate, List<String> fieldList, TimeRange timeRange, Optional<String> streamId) {
final QueryResult queryResult = searchJob.results().values().stream().findFirst().orElseThrow(() -> new IllegalStateException("Missing query result"));
final MessageList.Result result = queryResult.searchTypes().values().stream().findFirst().map(searchTypeResult -> (MessageList.Result) searchTypeResult).orElseThrow(() -> new IllegalStateException("Missing search type result!"));
final long tookMs = queryResult.executionStats().duration();
return buildSearchResponse(query, result, fieldList, tookMs, timeRange, decorate, streamId);
}
use of org.graylog.plugins.views.search.QueryResult in project graylog2-server by Graylog2.
the class QueryBackend method run.
// TODO we can probably push job and query into the GeneratedQueryContext to simplify the signature
default QueryResult run(SearchJob job, Query query, GeneratedQueryContext generatedQueryContext) {
try {
final Stopwatch stopwatch = Stopwatch.createStarted();
final QueryExecutionStats.Builder statsBuilder = QueryExecutionStats.builderWithCurrentTime();
// https://www.ibm.com/developerworks/java/library/j-jtp04298/index.html#3.0
// noinspection unchecked
final QueryResult result = doRun(job, query, (T) generatedQueryContext);
stopwatch.stop();
return result.toBuilder().executionStats(statsBuilder.duration(stopwatch.elapsed(TimeUnit.MILLISECONDS)).effectiveTimeRange(effectiveTimeRangeForResult(query, result)).build()).build();
} catch (Exception e) {
// the backend has very likely created a more specific error and added it to the context, but we fall
// back to a generic error so we never throw exceptions into the engine.
final QueryError queryError = new QueryError(query, e);
generatedQueryContext.addError(queryError);
return QueryResult.failedQueryWithError(query, queryError);
}
}
use of org.graylog.plugins.views.search.QueryResult in project graylog2-server by Graylog2.
the class QueryEngine method execute.
public SearchJob execute(SearchJob searchJob) {
searchJob.getSearch().queries().forEach(query -> searchJob.addQueryResultFuture(query.id(), // if need be we default to an empty result with a failed state and the wrapped exception
CompletableFuture.supplyAsync(() -> prepareAndRun(searchJob, query), queryPool).handle((queryResult, throwable) -> {
if (throwable != null) {
final Throwable cause = throwable.getCause();
final SearchError error;
if (cause instanceof SearchException) {
error = ((SearchException) cause).error();
} else {
error = new QueryError(query, cause);
}
LOG.debug("Running query {} failed: {}", query.id(), cause);
searchJob.addError(error);
return QueryResult.failedQueryWithError(query, error);
}
return queryResult;
})));
searchJob.getSearch().queries().forEach(query -> {
final CompletableFuture<QueryResult> queryResultFuture = searchJob.getQueryResultFuture(query.id());
if (!queryResultFuture.isDone()) {
// this is not going to throw an exception, because we will always replace it with a placeholder "FAILED" result above
final QueryResult result = queryResultFuture.join();
} else {
LOG.debug("[{}] Not generating query for query {}", defaultIfEmpty(query.id(), "root"), query);
}
});
LOG.debug("Search job {} executing", searchJob.getId());
return searchJob.seal();
}
use of org.graylog.plugins.views.search.QueryResult in project graylog2-server by Graylog2.
the class ElasticsearchBackend method doRun.
@Override
public QueryResult doRun(SearchJob job, Query query, ESGeneratedQueryContext queryContext) {
if (query.searchTypes().isEmpty()) {
return QueryResult.builder().query(query).searchTypes(Collections.emptyMap()).errors(new HashSet<>(queryContext.errors())).build();
}
LOG.debug("Running query {} for job {}", query.id(), job.getId());
final HashMap<String, SearchType.Result> resultsMap = Maps.newHashMap();
final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(query.usedStreamIds(), query.timerange());
final Map<String, SearchSourceBuilder> searchTypeQueries = queryContext.searchTypeQueries();
final List<String> searchTypeIds = new ArrayList<>(searchTypeQueries.keySet());
final List<Search> searches = searchTypeIds.stream().map(searchTypeId -> {
final Set<String> affectedIndicesForSearchType = query.searchTypes().stream().filter(s -> s.id().equalsIgnoreCase(searchTypeId)).findFirst().flatMap(searchType -> {
if (searchType.effectiveStreams().isEmpty() && !query.globalOverride().flatMap(GlobalOverride::timerange).isPresent() && !searchType.timerange().isPresent()) {
return Optional.empty();
}
final Set<String> usedStreamIds = searchType.effectiveStreams().isEmpty() ? query.usedStreamIds() : searchType.effectiveStreams();
return Optional.of(indexLookup.indexNamesForStreamsInTimeRange(usedStreamIds, query.effectiveTimeRange(searchType)));
}).orElse(affectedIndices);
return new Search.Builder(searchTypeQueries.get(searchTypeId).toString()).addType(IndexMapping.TYPE_MESSAGE).addIndex(affectedIndicesForSearchType.isEmpty() ? Collections.singleton("") : affectedIndicesForSearchType).allowNoIndices(false).ignoreUnavailable(false).build();
}).collect(Collectors.toList());
final MultiSearch.Builder multiSearchBuilder = new MultiSearch.Builder(searches);
final MultiSearchResult result = JestUtils.execute(jestClient, multiSearchBuilder.build(), () -> "Unable to perform search query: ");
for (SearchType searchType : query.searchTypes()) {
final String searchTypeId = searchType.id();
final Provider<ESSearchTypeHandler<? extends SearchType>> handlerProvider = elasticsearchSearchTypeHandlers.get(searchType.type());
if (handlerProvider == null) {
LOG.error("Unknown search type '{}', cannot convert query result.", searchType.type());
// no need to add another error here, as the query generation code will have added the error about the missing handler already
continue;
}
if (isSearchTypeWithError(queryContext, searchTypeId)) {
LOG.error("Failed search type '{}', cannot convert query result, skipping.", searchType.type());
// no need to add another error here, as the query generation code will have added the error about the missing handler already
continue;
}
// we create a new instance because some search type handlers might need to track information between generating the query and
// processing its result, such as aggregations, which depend on the name and type
final ESSearchTypeHandler<? extends SearchType> handler = handlerProvider.get();
final int searchTypeIndex = searchTypeIds.indexOf(searchTypeId);
final MultiSearchResult.MultiSearchResponse multiSearchResponse = result.getResponses().get(searchTypeIndex);
if (multiSearchResponse.isError) {
ElasticsearchException e = JestUtils.specificException(() -> "Search type returned error: ", multiSearchResponse.error);
queryContext.addError(SearchTypeErrorParser.parse(query, searchTypeId, e));
} else if (checkForFailedShards(multiSearchResponse.searchResult).isPresent()) {
ElasticsearchException e = checkForFailedShards(multiSearchResponse.searchResult).get();
queryContext.addError(SearchTypeErrorParser.parse(query, searchTypeId, e));
} else {
final SearchType.Result searchTypeResult = handler.extractResult(job, query, searchType, multiSearchResponse.searchResult, queryContext);
if (searchTypeResult != null) {
resultsMap.put(searchTypeId, searchTypeResult);
}
}
}
LOG.debug("Query {} ran for job {}", query.id(), job.getId());
return QueryResult.builder().query(query).searchTypes(resultsMap).errors(new HashSet<>(queryContext.errors())).build();
}
Aggregations