Search in sources :

Example 1 with SearchException

use of org.graylog.plugins.views.search.errors.SearchException in project graylog2-server by Graylog2.

the class QueryValidationServiceImpl method toExplanation.

private List<ValidationMessage> toExplanation(String query, SearchException searchException) {
    if (searchException.error() instanceof UnboundParameterError) {
        final UnboundParameterError error = (UnboundParameterError) searchException.error();
        final List<SubstringMultilinePosition> positions = SubstringMultilinePosition.compute(query, "$" + error.parameterName() + "$");
        if (!positions.isEmpty()) {
            return positions.stream().map(p -> ValidationMessage.builder().errorType("Parameter error").errorMessage(error.description()).beginLine(p.getLine()).endLine(p.getLine()).beginColumn(p.getBeginColumn()).endColumn(p.getEndColumn()).build()).collect(Collectors.toList());
        }
    }
    return Collections.singletonList(ValidationMessage.fromException(searchException));
}
Also used : ParseException(org.apache.lucene.queryparser.classic.ParseException) Query(org.graylog.plugins.views.search.Query) Set(java.util.Set) ParameterProvider(org.graylog.plugins.views.search.ParameterProvider) MappedFieldTypesService(org.graylog2.indexer.fieldtypes.MappedFieldTypesService) Streams(com.google.common.collect.Streams) Singleton(javax.inject.Singleton) StringUtils(org.apache.commons.lang3.StringUtils) SearchException(org.graylog.plugins.views.search.errors.SearchException) Collectors(java.util.stream.Collectors) UnboundParameterError(org.graylog.plugins.views.search.errors.UnboundParameterError) Inject(javax.inject.Inject) Objects(java.util.Objects) List(java.util.List) QueryStringDecorators(org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators) Stream(java.util.stream.Stream) MappedFieldTypeDTO(org.graylog.plugins.views.search.rest.MappedFieldTypeDTO) Locale(java.util.Locale) Collections(java.util.Collections) UnboundParameterError(org.graylog.plugins.views.search.errors.UnboundParameterError)

Example 2 with SearchException

use of org.graylog.plugins.views.search.errors.SearchException 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();
}
Also used : QueryResult(org.graylog.plugins.views.search.QueryResult) SearchError(org.graylog.plugins.views.search.errors.SearchError) SearchException(org.graylog.plugins.views.search.errors.SearchException) QueryError(org.graylog.plugins.views.search.errors.QueryError)

Example 3 with SearchException

use of org.graylog.plugins.views.search.errors.SearchException in project graylog2-server by Graylog2.

the class AggregationEventProcessor method createEvents.

@Override
public void createEvents(EventFactory eventFactory, EventProcessorParameters processorParameters, EventConsumer<List<EventWithContext>> eventsConsumer) throws EventProcessorException {
    final AggregationEventProcessorParameters parameters = (AggregationEventProcessorParameters) processorParameters;
    // TODO: We have to take the Elasticsearch index.refresh_interval into account here!
    if (!dependencyCheck.hasMessagesIndexedUpTo(parameters.timerange().getTo())) {
        final String msg = String.format(Locale.ROOT, "Couldn't run aggregation <%s/%s> for timerange <%s to %s> because required messages haven't been indexed, yet.", eventDefinition.title(), eventDefinition.id(), parameters.timerange().getFrom(), parameters.timerange().getTo());
        throw new EventProcessorPreconditionException(msg, eventDefinition);
    }
    LOG.debug("Creating events for config={} parameters={}", config, parameters);
    // a simple search query. (one message -> one event)
    try {
        if (config.series().isEmpty()) {
            filterSearch(eventFactory, parameters, eventsConsumer);
        } else {
            aggregatedSearch(eventFactory, parameters, eventsConsumer);
        }
    } catch (SearchException e) {
        if (e.error() instanceof ParameterExpansionError) {
            final String msg = String.format(Locale.ROOT, "Couldn't run aggregation <%s/%s>  because parameters failed to expand: %s", eventDefinition.title(), eventDefinition.id(), e.error().description());
            LOG.error(msg);
            throw new EventProcessorPreconditionException(msg, eventDefinition, e);
        }
    }
    // Update the state for this processor! This state will be used for dependency checks between event processors.
    stateService.setState(eventDefinition.id(), parameters.timerange().getFrom(), parameters.timerange().getTo());
}
Also used : ParameterExpansionError(org.graylog.plugins.views.search.errors.ParameterExpansionError) SearchException(org.graylog.plugins.views.search.errors.SearchException) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) EventProcessorPreconditionException(org.graylog.events.processor.EventProcessorPreconditionException)

Example 4 with SearchException

use of org.graylog.plugins.views.search.errors.SearchException in project graylog2-server by Graylog2.

the class MoreSearch method scrollQuery.

/**
 * This scrolls results for the given query, streams and time range from Elasticsearch. The result is passed to
 * the given callback in batches. (using the given batch size)
 * <p>
 * The search will continue until it is done, an error occurs or the search is stopped by setting the
 * {@code continueScrolling} boolean to {@code false} from the {@link ScrollCallback}.
 * <p></p>
 * TODO: Elasticsearch has a default limit of 500 concurrent scrolls. Every caller of this method should check
 * if there is capacity to create a new scroll request. This can be done by using the ES nodes stats API.
 * See: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html#scroll-search-context
 *
 * @param queryString    the search query string
 * @param streams        the set of streams to search in
 * @param timeRange      the time range for the search
 * @param batchSize      the number of documents to retrieve at once
 * @param resultCallback the callback that gets executed for each batch
 */
public void scrollQuery(String queryString, Set<String> streams, Set<Parameter> queryParameters, TimeRange timeRange, int batchSize, ScrollCallback resultCallback) throws EventProcessorException {
    // TODO: Does scroll time need to be configurable?
    final String scrollTime = "1m";
    final Set<String> affectedIndices = getAffectedIndices(streams, timeRange);
    try {
        queryString = decorateQuery(queryParameters, timeRange, queryString);
    } catch (SearchException e) {
        if (e.error() instanceof EmptyParameterError) {
            LOG.debug("Empty parameter from lookup table. Assuming non-matching query. Error: {}", e.getMessage());
            return;
        }
        throw e;
    }
    moreSearchAdapter.scrollEvents(queryString, timeRange, affectedIndices, streams, scrollTime, batchSize, resultCallback::call);
}
Also used : EmptyParameterError(org.graylog.plugins.views.search.errors.EmptyParameterError) SearchException(org.graylog.plugins.views.search.errors.SearchException) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString)

Aggregations

SearchException (org.graylog.plugins.views.search.errors.SearchException)4 ElasticsearchQueryString (org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString)2 Streams (com.google.common.collect.Streams)1 Collections (java.util.Collections)1 List (java.util.List)1 Locale (java.util.Locale)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 EventProcessorPreconditionException (org.graylog.events.processor.EventProcessorPreconditionException)1 ParameterProvider (org.graylog.plugins.views.search.ParameterProvider)1 Query (org.graylog.plugins.views.search.Query)1 QueryResult (org.graylog.plugins.views.search.QueryResult)1 QueryStringDecorators (org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators)1 EmptyParameterError (org.graylog.plugins.views.search.errors.EmptyParameterError)1