use of io.searchbox.core.MultiSearchResult in project graylog2-server by Graylog2.
the class MultiSearch method wrap.
public SearchResult wrap(Search search, Supplier<String> errorMessage) {
final io.searchbox.core.MultiSearch multiSearch = new io.searchbox.core.MultiSearch.Builder(search).build();
final MultiSearchResult multiSearchResult = JestUtils.execute(jestClient, multiSearch, errorMessage);
final List<MultiSearchResult.MultiSearchResponse> responses = multiSearchResult.getResponses();
if (responses.size() != 1) {
throw new ElasticsearchException("Expected exactly 1 search result, but got " + responses.size());
}
final MultiSearchResult.MultiSearchResponse response = responses.get(0);
if (response.isError) {
throw JestUtils.specificException(errorMessage, response.error);
}
final Optional<ElasticsearchException> elasticsearchException = checkForFailedShards(response.searchResult);
elasticsearchException.ifPresent(e -> {
throw e;
});
return response.searchResult;
}
use of io.searchbox.core.MultiSearchResult 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();
}
use of io.searchbox.core.MultiSearchResult in project graylog2-server by Graylog2.
the class CountsAdapterES6 method totalCount.
@Override
public long totalCount(List<String> indices) {
final String query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).size(0).toString();
final Search request = new Search.Builder(query).addIndex(indices).build();
final MultiSearch multiSearch = new MultiSearch.Builder(request).build();
final MultiSearchResult searchResult = JestUtils.execute(jestClient, multiSearch, () -> "Fetching message count failed for indices " + indices);
final List<MultiSearchResult.MultiSearchResponse> responses = searchResult.getResponses();
long total = 0L;
for (MultiSearchResult.MultiSearchResponse response : responses) {
if (response.isError) {
throw JestUtils.specificException(() -> "Fetching message count failed for indices " + indices, response.error);
}
total += response.searchResult.getTotal();
}
return total;
}
use of io.searchbox.core.MultiSearchResult in project graylog2-server by Graylog2.
the class ElasticsearchBackendErrorHandlingTest method deduplicateNumericShardErrorsOnSearchTypeLevel.
@Test
public void deduplicateNumericShardErrorsOnSearchTypeLevel() throws IOException {
final MultiSearchResult multiSearchResult = searchResultFromFixture("errorhandling/numericFailureOnSearchTypeLevel.json");
when(jestClient.execute(any())).thenReturn(multiSearchResult);
final QueryResult queryResult = this.backend.doRun(searchJob, query, queryContext);
final Set<SearchError> errors = queryResult.errors();
assertThat(errors).isNotNull();
assertThat(errors).hasSize(1);
assertThat(errors.stream().map(SearchError::description).collect(Collectors.toList())).containsExactly("Unable to perform search query: \n\nExpected numeric type on field [facility], but got [keyword].");
}
use of io.searchbox.core.MultiSearchResult in project graylog2-server by Graylog2.
the class ElasticsearchBackendErrorHandlingTest method searchResultFromFixture.
private MultiSearchResult searchResultFromFixture(String filename) throws IOException {
final JsonNode resultObject = objectMapper.readTree(resourceFile(filename));
final MultiSearchResult multiSearchResult = new MultiSearchResult(objectMapper);
multiSearchResult.setJsonObject(resultObject);
multiSearchResult.setSucceeded(true);
return multiSearchResult;
}
Aggregations