Search in sources :

Example 1 with EmptyParameterError

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

the class PivotAggregationSearch method doSearch.

@Override
public AggregationResult doSearch() throws EventProcessorException {
    final SearchJob searchJob = getSearchJob(parameters, searchOwner, config.searchWithinMs(), config.executeEveryMs());
    final QueryResult queryResult = searchJob.results().get(QUERY_ID);
    final QueryResult streamQueryResult = searchJob.results().get(STREAMS_QUERY_ID);
    final Set<SearchError> aggregationErrors = firstNonNull(queryResult.errors(), Collections.emptySet());
    final Set<SearchError> streamErrors = firstNonNull(streamQueryResult.errors(), Collections.emptySet());
    if (!aggregationErrors.isEmpty() || !streamErrors.isEmpty()) {
        final Set<SearchError> errors = aggregationErrors.isEmpty() ? streamErrors : aggregationErrors;
        errors.forEach(error -> {
            if (error instanceof QueryError) {
                final QueryError queryError = (QueryError) error;
                final String backtrace = queryError.backtrace() != null ? queryError.backtrace() : "";
                if (error instanceof EmptyParameterError) {
                    LOG.debug("Aggregation search query <{}> with empty Parameter: {}\n{}", queryError.queryId(), queryError.description(), backtrace);
                } else {
                    LOG.error("Aggregation search query <{}> returned an error: {}\n{}", queryError.queryId(), queryError.description(), backtrace);
                }
            } else {
                LOG.error("Aggregation search returned an error: {}", error);
            }
        });
        // If we have only EmptyParameterErrors, just return an empty Result
        if (!(errors.stream().filter(e -> !(e instanceof EmptyParameterError)).count() > 1)) {
            return AggregationResult.empty();
        }
        if (errors.size() > 1) {
            throw new EventProcessorException("Pivot search failed with multiple errors.", false, eventDefinition);
        } else {
            throw new EventProcessorException(errors.iterator().next().description(), false, eventDefinition);
        }
    }
    final PivotResult pivotResult = (PivotResult) queryResult.searchTypes().get(PIVOT_ID);
    final PivotResult streamsResult = (PivotResult) streamQueryResult.searchTypes().get(STREAMS_PIVOT_ID);
    return AggregationResult.builder().keyResults(extractValues(pivotResult)).effectiveTimerange(pivotResult.effectiveTimerange()).totalAggregatedMessages(pivotResult.total()).sourceStreams(extractSourceStreams(streamsResult)).build();
}
Also used : EventProcessorException(org.graylog.events.processor.EventProcessorException) EmptyParameterError(org.graylog.plugins.views.search.errors.EmptyParameterError) QueryResult(org.graylog.plugins.views.search.QueryResult) PivotResult(org.graylog.plugins.views.search.searchtypes.pivot.PivotResult) SearchError(org.graylog.plugins.views.search.errors.SearchError) SearchJob(org.graylog.plugins.views.search.SearchJob) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) QueryError(org.graylog.plugins.views.search.errors.QueryError)

Example 2 with EmptyParameterError

use of org.graylog.plugins.views.search.errors.EmptyParameterError 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

ElasticsearchQueryString (org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString)2 EmptyParameterError (org.graylog.plugins.views.search.errors.EmptyParameterError)2 EventProcessorException (org.graylog.events.processor.EventProcessorException)1 QueryResult (org.graylog.plugins.views.search.QueryResult)1 SearchJob (org.graylog.plugins.views.search.SearchJob)1 QueryError (org.graylog.plugins.views.search.errors.QueryError)1 SearchError (org.graylog.plugins.views.search.errors.SearchError)1 SearchException (org.graylog.plugins.views.search.errors.SearchException)1 PivotResult (org.graylog.plugins.views.search.searchtypes.pivot.PivotResult)1