use of org.elasticsearch.search.builder.SearchSourceBuilder 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.elasticsearch.search.builder.SearchSourceBuilder in project graylog2-server by Graylog2.
the class CountsAdapterES7 method totalCount.
@Override
public long totalCount(List<String> indices) {
final SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).size(0).trackTotalHits(true);
final SearchRequest searchRequest = new SearchRequest(indices.toArray(new String[0])).source(query);
final SearchResponse result = client.search(searchRequest, "Fetching message count failed for indices ");
return result.getHits().getTotalHits().value;
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project graylog2-server by Graylog2.
the class QuerySuggestionsES7 method suggest.
@Override
public SuggestionResponse suggest(SuggestionRequest req) {
final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(req.streams(), req.timerange());
final TermSuggestionBuilder suggestionBuilder = SuggestBuilders.termSuggestion(req.field()).text(req.input()).size(req.size());
final SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.prefixQuery(req.field(), req.input())).size(0).aggregation(AggregationBuilders.terms("fieldvalues").field(req.field()).size(req.size())).suggest(new SuggestBuilder().addSuggestion("corrections", suggestionBuilder));
try {
final SearchResponse result = client.singleSearch(new SearchRequest(affectedIndices.toArray(new String[] {})).source(search), "Failed to execute aggregation");
final ParsedStringTerms fieldValues = result.getAggregations().get("fieldvalues");
final List<SuggestionEntry> entries = fieldValues.getBuckets().stream().map(b -> new SuggestionEntry(b.getKeyAsString(), b.getDocCount())).collect(Collectors.toList());
if (!entries.isEmpty()) {
return SuggestionResponse.forSuggestions(req.field(), req.input(), entries, fieldValues.getSumOfOtherDocCounts());
} else {
TermSuggestion suggestion = result.getSuggest().getSuggestion("corrections");
final List<SuggestionEntry> corrections = suggestion.getEntries().stream().flatMap(e -> e.getOptions().stream()).map(o -> new SuggestionEntry(o.getText().string(), o.getFreq())).collect(Collectors.toList());
return SuggestionResponse.forSuggestions(req.field(), req.input(), corrections, null);
}
} catch (org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException exception) {
final SuggestionError err = tryResponseException(exception).orElseGet(() -> parseException(exception));
return SuggestionResponse.forError(req.field(), req.input(), err);
}
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project graylog2-server by Graylog2.
the class Scroll method scroll.
public ScrollResult scroll(ScrollCommand scrollCommand) {
final SearchSourceBuilder searchQuery = searchRequestFactory.create(scrollCommand);
searchQuery.fetchSource(scrollCommand.fields().toArray(new String[0]), new String[0]);
scrollCommand.batchSize().ifPresent(batchSize -> searchQuery.size(Math.toIntExact(batchSize)));
final SearchRequest request = scrollBuilder(searchQuery, scrollCommand.indices());
final SearchResponse result = client.singleSearch(request, "Unable to perform scroll search");
return scrollResultFactory.create(result, searchQuery.toString(), DEFAULT_SCROLLTIME, scrollCommand.fields(), scrollCommand.limit().orElse(-1));
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project graylog2-server by Graylog2.
the class SearchesAdapterES7 method count.
@Override
public CountResult count(Set<String> affectedIndices, String query, TimeRange range, String filter) {
final SearchesConfig config = SearchesConfig.builder().query(query).range(range).filter(filter).limit(0).offset(0).build();
final SearchSourceBuilder searchSourceBuilder = searchRequestFactory.create(config);
final SearchRequest searchRequest = new SearchRequest(affectedIndices.toArray(new String[0])).source(searchSourceBuilder);
final SearchResponse result = client.search(searchRequest, "Fetching message count failed for indices ");
return CountResult.create(result.getHits().getTotalHits().value, result.getTook().getMillis());
}
Aggregations