use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class IssueIndex method addAssigneesFacetIfNeeded.
private static void addAssigneesFacetIfNeeded(SearchOptions options, IssueQuery query, TopAggregationHelper aggregationHelper, SearchSourceBuilder esRequest) {
if (!options.getFacets().contains(PARAM_ASSIGNEES)) {
return;
}
Consumer<FilterAggregationBuilder> assigneeAggregations = t -> {
// optional second aggregation to return the issue count for selected assignees (if any)
Object[] assignees = query.assignees().toArray();
aggregationHelper.getSubAggregationHelper().buildSelectedItemsAggregation(ASSIGNEES.getName(), ASSIGNEES.getTopAggregationDef(), assignees).ifPresent(t::subAggregation);
// third aggregation to always return the count of unassigned in the assignee facet
t.subAggregation(addEffortAggregationIfNeeded(query, AggregationBuilders.missing(ASSIGNEES.getName() + FACET_SUFFIX_MISSING).field(ASSIGNEES.getFieldName())));
};
AggregationBuilder aggregation = aggregationHelper.buildTermTopAggregation(ASSIGNEES.getName(), ASSIGNEES.getTopAggregationDef(), ASSIGNEES.getNumberOfTerms(), NO_EXTRA_FILTER, assigneeAggregations);
esRequest.aggregation(aggregation);
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method searchTelemetryStatistics.
public ProjectMeasuresStatistics searchTelemetryStatistics() {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().fetchSource(false).size(0);
BoolQueryBuilder esFilter = boolQuery().filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())).filter(termQuery(FIELD_QUALIFIER, Qualifiers.PROJECT));
searchSourceBuilder.query(esFilter);
searchSourceBuilder.aggregation(AggregationBuilders.terms(FIELD_LANGUAGES).field(FIELD_LANGUAGES).size(MAX_PAGE_SIZE).minDocCount(1).order(BucketOrder.count(false)));
searchSourceBuilder.aggregation(AggregationBuilders.nested(FIELD_NCLOC_DISTRIBUTION, FIELD_NCLOC_DISTRIBUTION).subAggregation(AggregationBuilders.terms(FIELD_NCLOC_DISTRIBUTION + "_terms").field(FIELD_NCLOC_DISTRIBUTION_LANGUAGE).size(MAX_PAGE_SIZE).minDocCount(1).order(BucketOrder.count(false)).subAggregation(sum(FIELD_NCLOC_DISTRIBUTION_NCLOC).field(FIELD_NCLOC_DISTRIBUTION_NCLOC))));
searchSourceBuilder.aggregation(AggregationBuilders.nested(NCLOC_KEY, FIELD_MEASURES).subAggregation(AggregationBuilders.filter(NCLOC_KEY + "_filter", termQuery(FIELD_MEASURES_MEASURE_KEY, NCLOC_KEY)).subAggregation(sum(NCLOC_KEY + "_filter_sum").field(FIELD_MEASURES_MEASURE_VALUE))));
ProjectMeasuresStatistics.Builder statistics = ProjectMeasuresStatistics.builder();
SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder));
statistics.setProjectCount(getTotalHits(response.getHits().getTotalHits()).value);
statistics.setProjectCountByLanguage(termsToMap(response.getAggregations().get(FIELD_LANGUAGES)));
Function<Terms.Bucket, Long> bucketToNcloc = bucket -> Math.round(((Sum) bucket.getAggregations().get(FIELD_NCLOC_DISTRIBUTION_NCLOC)).getValue());
Map<String, Long> nclocByLanguage = Stream.of((Nested) response.getAggregations().get(FIELD_NCLOC_DISTRIBUTION)).map(nested -> (Terms) nested.getAggregations().get(nested.getName() + "_terms")).flatMap(terms -> terms.getBuckets().stream()).collect(MoreCollectors.uniqueIndex(Bucket::getKeyAsString, bucketToNcloc));
statistics.setNclocByLanguage(nclocByLanguage);
return statistics.build();
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method search.
public SearchIdResult<String> search(ProjectMeasuresQuery query, SearchOptions searchOptions) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().fetchSource(false).trackTotalHits(true).from(searchOptions.getOffset()).size(searchOptions.getLimit());
AllFilters allFilters = createFilters(query);
RequestFiltersComputer filtersComputer = createFiltersComputer(searchOptions, allFilters);
addFacets(searchSourceBuilder, searchOptions, filtersComputer, query);
addSort(query, searchSourceBuilder);
filtersComputer.getQueryFilters().ifPresent(searchSourceBuilder::query);
filtersComputer.getPostFilters().ifPresent(searchSourceBuilder::postFilter);
SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder));
return new SearchIdResult<>(response, id -> id, system2.getDefaultTimeZone().toZoneId());
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method addFacets.
private static void addFacets(SearchSourceBuilder esRequest, SearchOptions options, RequestFiltersComputer filtersComputer, ProjectMeasuresQuery query) {
TopAggregationHelper topAggregationHelper = new TopAggregationHelper(filtersComputer, new SubAggregationHelper());
options.getFacets().stream().map(FACETS_BY_NAME::get).filter(Objects::nonNull).map(facet -> facet.getFacetBuilder().buildFacet(facet, query, topAggregationHelper)).forEach(esRequest::aggregation);
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method searchTags.
public List<String> searchTags(@Nullable String textQuery, int page, int size) {
int maxPageSize = 100;
int maxPage = 20;
checkArgument(size <= maxPageSize, "Page size must be lower than or equals to " + maxPageSize);
checkArgument(page > 0 && page <= maxPage, "Page must be between 0 and " + maxPage);
if (size <= 0) {
return emptyList();
}
TermsAggregationBuilder tagFacet = AggregationBuilders.terms(FIELD_TAGS).field(FIELD_TAGS).size(size * page).minDocCount(1).order(BucketOrder.key(true));
if (textQuery != null) {
tagFacet.includeExclude(new IncludeExclude(".*" + escapeSpecialRegexChars(textQuery) + ".*", null));
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(authorizationTypeSupport.createQueryFilter()).fetchSource(false).aggregation(tagFacet);
SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder));
Terms aggregation = response.getAggregations().get(FIELD_TAGS);
return aggregation.getBuckets().stream().skip((page - 1) * size).map(Bucket::getKeyAsString).collect(MoreCollectors.toList());
}
Aggregations