use of org.graylog.plugins.views.search.SearchType in project graylog2-server by Graylog2.
the class ElasticsearchBackendErrorHandlingTest method setUp.
@Before
public void setUp() throws Exception {
final FieldTypesLookup fieldTypesLookup = mock(FieldTypesLookup.class);
this.backend = new ElasticsearchBackend(ImmutableMap.of("dummy", () -> mock(DummyHandler.class)), jestClient, indexLookup, new QueryStringDecorators(Collections.emptySet()), (elasticsearchBackend, ssb, job, query) -> new ESGeneratedQueryContext(elasticsearchBackend, ssb, job, query, fieldTypesLookup), false, objectMapper);
when(indexLookup.indexNamesForStreamsInTimeRange(any(), any())).thenReturn(Collections.emptySet());
final SearchType searchType1 = mock(SearchType.class);
when(searchType1.id()).thenReturn("deadbeef");
when(searchType1.type()).thenReturn("dummy");
final SearchType searchType2 = mock(SearchType.class);
when(searchType2.id()).thenReturn("cafeaffe");
when(searchType2.type()).thenReturn("dummy");
final Set<SearchType> searchTypes = ImmutableSet.of(searchType1, searchType2);
this.query = Query.builder().id("query1").timerange(RelativeRange.create(300)).query(ElasticsearchQueryString.of("*")).searchTypes(searchTypes).build();
final Search search = Search.builder().id("search1").queries(ImmutableSet.of(query)).build();
this.searchJob = new SearchJob("job1", search, "admin");
this.queryContext = new ESGeneratedQueryContext(this.backend, new SearchSourceBuilder(), searchJob, query, mock(FieldTypesLookup.class));
searchTypes.forEach(queryContext::searchSourceBuilder);
when(jestClient.execute(any())).thenReturn(result);
}
use of org.graylog.plugins.views.search.SearchType in project graylog2-server by Graylog2.
the class ElasticsearchBackendQueryStringDecoratorsTest method searchJobWithSearchTypeQueryString.
private SearchJob searchJobWithSearchTypeQueryString(Query query) throws InvalidRangeParametersException {
final SearchType searchType = basicSearchType();
final SearchJob searchJob = basicSearchJob(query, searchType);
when(query.query()).thenReturn(ElasticsearchQueryString.of("*"));
when(searchType.query()).thenReturn(Optional.of(ElasticsearchQueryString.of("Should never show up")));
return searchJob;
}
use of org.graylog.plugins.views.search.SearchType in project graylog2-server by Graylog2.
the class ElasticsearchBackendQueryStringDecoratorsTest method basicSearchType.
@Nonnull
private SearchType basicSearchType() {
final SearchType searchType = mock(SearchType.class);
when(searchType.id()).thenReturn("testSearchtype");
return searchType;
}
use of org.graylog.plugins.views.search.SearchType in project graylog2-server by Graylog2.
the class OffsetRangeTest method throwsExceptionIfInvalidSearchTypeIsReferenced.
@Test
public void throwsExceptionIfInvalidSearchTypeIsReferenced() throws Exception {
final OffsetRange offsetRange = constructRange("300", "search_type", "invalidSearchType");
final TimeRange sourceRange = mock(TimeRange.class);
when(sourceRange.getFrom()).thenReturn(DateTime.parse("2019-11-18T10:00:00.000Z"));
when(sourceRange.getTo()).thenReturn(DateTime.parse("2019-11-21T12:00:00.000Z"));
final Query query = mock(Query.class);
final SearchType searchType = mock(SearchType.class);
when(searchType.id()).thenReturn("searchTypeId");
when(searchType.timerange()).thenReturn(Optional.of(DerivedTimeRange.of(sourceRange)));
when(query.searchTypes()).thenReturn(ImmutableSet.of(searchType));
when(query.effectiveTimeRange(searchType)).thenReturn(sourceRange);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> offsetRange.deriveTimeRange(query, searchType)).withMessage("Search type searchTypeId has offset timerange referencing invalid search type: invalidSearchType");
}
use of org.graylog.plugins.views.search.SearchType in project graylog2-server by Graylog2.
the class PivotAggregationSearch method getAggregationQuery.
/**
* Returns the query to compute the aggregation.
*
* @param parameters processor parameters
* @param searchWithinMs processor search within period. Used to build the date range buckets
* @param executeEveryMs
* @return aggregation query
*/
private Query getAggregationQuery(AggregationEventProcessorParameters parameters, long searchWithinMs, long executeEveryMs) {
final Pivot.Builder pivotBuilder = Pivot.builder().id(PIVOT_ID).rollup(true);
final ImmutableList<SeriesSpec> series = config.series().stream().map(entry -> entry.function().toSeriesSpec(metricName(entry), entry.field().orElse(null))).collect(ImmutableList.toImmutableList());
if (!series.isEmpty()) {
pivotBuilder.series(series);
}
// Wrap every aggregation with date range buckets of the searchWithin time range.
// If the aggregation is configured to be using a sliding window (searchWithin > executeEveryMs)
// the time ranges will overlap.
// This allows us to run aggregations over larger time ranges than the searchWithin time.
// The results will be received in time buckets of the searchWithin time size.
final DateRangeBucket dateRangeBucket = buildDateRangeBuckets(parameters.timerange(), searchWithinMs, executeEveryMs);
final List<BucketSpec> groupBy = new ArrayList<>();
// The first bucket must be the date range!
groupBy.add(dateRangeBucket);
if (!config.groupBy().isEmpty()) {
// Then we add the configured groups
groupBy.addAll(config.groupBy().stream().map(field -> Values.builder().limit(Integer.MAX_VALUE).field(field).build()).collect(Collectors.toList()));
}
// We always have row groups because of the date range buckets
pivotBuilder.rowGroups(groupBy);
final Set<SearchType> searchTypes = Collections.singleton(pivotBuilder.build());
final Query.Builder queryBuilder = Query.builder().id(QUERY_ID).searchTypes(searchTypes).query(ElasticsearchQueryString.of(config.query())).timerange(parameters.timerange());
final Set<String> streams = getStreams(parameters);
if (!streams.isEmpty()) {
queryBuilder.filter(filteringForStreamIds(streams));
}
return queryBuilder.build();
}
Aggregations