use of org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms 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.aggregations.bucket.terms.ParsedStringTerms in project sonarqube by SonarSource.
the class IssueIndex method processSecurityReportCategorySearchResults.
private static SecurityStandardCategoryStatistics processSecurityReportCategorySearchResults(HasAggregations categoryBucket, String categoryName, @Nullable List<SecurityStandardCategoryStatistics> children) {
List<? extends Terms.Bucket> severityBuckets = ((ParsedStringTerms) ((ParsedFilter) categoryBucket.getAggregations().get(AGG_VULNERABILITIES)).getAggregations().get(AGG_SEVERITIES)).getBuckets();
long vulnerabilities = severityBuckets.stream().mapToLong(b -> ((ParsedValueCount) b.getAggregations().get(AGG_COUNT)).getValue()).sum();
// Worst severity having at least one issue
OptionalInt severityRating = severityBuckets.stream().filter(b -> ((ParsedValueCount) b.getAggregations().get(AGG_COUNT)).getValue() != 0).mapToInt(b -> Severity.ALL.indexOf(b.getKeyAsString()) + 1).max();
long toReviewSecurityHotspots = ((ParsedValueCount) ((ParsedFilter) categoryBucket.getAggregations().get(AGG_TO_REVIEW_SECURITY_HOTSPOTS)).getAggregations().get(AGG_COUNT)).getValue();
long reviewedSecurityHotspots = ((ParsedValueCount) ((ParsedFilter) categoryBucket.getAggregations().get(AGG_REVIEWED_SECURITY_HOTSPOTS)).getAggregations().get(AGG_COUNT)).getValue();
Optional<Double> percent = computePercent(toReviewSecurityHotspots, reviewedSecurityHotspots);
Integer securityReviewRating = computeRating(percent.orElse(null)).getIndex();
return new SecurityStandardCategoryStatistics(categoryName, vulnerabilities, severityRating, toReviewSecurityHotspots, reviewedSecurityHotspots, securityReviewRating, children);
}
use of org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms in project sonarqube by SonarSource.
the class IssueIndex method searchProjectStatistics.
public List<ProjectStatistics> searchProjectStatistics(List<String> projectUuids, List<Long> froms, @Nullable String assigneeUuid) {
checkState(projectUuids.size() == froms.size(), "Expected same size for projectUuids (had size %s) and froms (had size %s)", projectUuids.size(), froms.size());
if (projectUuids.isEmpty()) {
return Collections.emptyList();
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(boolQuery().mustNot(existsQuery(FIELD_ISSUE_RESOLUTION)).filter(termQuery(FIELD_ISSUE_ASSIGNEE_UUID, assigneeUuid)).mustNot(termQuery(FIELD_ISSUE_TYPE, SECURITY_HOTSPOT.name()))).size(0);
IntStream.range(0, projectUuids.size()).forEach(i -> {
String projectUuid = projectUuids.get(i);
long from = froms.get(i);
sourceBuilder.aggregation(AggregationBuilders.filter(projectUuid, boolQuery().filter(termQuery(FIELD_ISSUE_PROJECT_UUID, projectUuid)).filter(rangeQuery(FIELD_ISSUE_FUNC_CREATED_AT).gte(epochMillisToEpochSeconds(from)))).subAggregation(AggregationBuilders.terms("branchUuid").field(FIELD_ISSUE_BRANCH_UUID).subAggregation(AggregationBuilders.count(AGG_COUNT).field(FIELD_ISSUE_KEY)).subAggregation(AggregationBuilders.max("maxFuncCreatedAt").field(FIELD_ISSUE_FUNC_CREATED_AT))));
});
SearchRequest requestBuilder = EsClient.prepareSearch(TYPE_ISSUE.getMainType());
requestBuilder.source(sourceBuilder);
SearchResponse response = client.search(requestBuilder);
return response.getAggregations().asList().stream().map(x -> (ParsedFilter) x).flatMap(projectBucket -> ((ParsedStringTerms) projectBucket.getAggregations().get("branchUuid")).getBuckets().stream().flatMap(branchBucket -> {
long count = ((ParsedValueCount) branchBucket.getAggregations().get(AGG_COUNT)).getValue();
if (count < 1L) {
return Stream.empty();
}
long lastIssueDate = (long) ((ParsedMax) branchBucket.getAggregations().get("maxFuncCreatedAt")).getValue();
return Stream.of(new ProjectStatistics(branchBucket.getKeyAsString(), count, lastIssueDate));
})).collect(MoreCollectors.toList(projectUuids.size()));
}
use of org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms in project sonarqube by SonarSource.
the class IssueIndex method searchBranchStatistics.
public List<PrStatistics> searchBranchStatistics(String projectUuid, List<String> branchUuids) {
if (branchUuids.isEmpty()) {
return Collections.emptyList();
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(boolQuery().must(termsQuery(FIELD_ISSUE_BRANCH_UUID, branchUuids)).mustNot(existsQuery(FIELD_ISSUE_RESOLUTION)).must(termQuery(FIELD_ISSUE_IS_MAIN_BRANCH, Boolean.toString(false)))).size(0).aggregation(AggregationBuilders.terms("branchUuids").field(FIELD_ISSUE_BRANCH_UUID).size(branchUuids.size()).subAggregation(AggregationBuilders.terms("types").field(FIELD_ISSUE_TYPE)));
SearchRequest requestBuilder = EsClient.prepareSearch(TYPE_ISSUE.getMainType()).routing(AuthorizationDoc.idOf(projectUuid));
requestBuilder.source(sourceBuilder);
SearchResponse response = client.search(requestBuilder);
return ((ParsedStringTerms) response.getAggregations().get("branchUuids")).getBuckets().stream().map(bucket -> new PrStatistics(bucket.getKeyAsString(), ((ParsedStringTerms) bucket.getAggregations().get("types")).getBuckets().stream().collect(uniqueIndex(MultiBucketsAggregation.Bucket::getKeyAsString, MultiBucketsAggregation.Bucket::getDocCount)))).collect(MoreCollectors.toList(branchUuids.size()));
}
Aggregations