use of org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude in project sonarqube by SonarSource.
the class RuleIndex method listTags.
public List<String> listTags(@Nullable String query, int size) {
int maxPageSize = 500;
checkArgument(size <= maxPageSize, "Page size must be lower than or equals to " + maxPageSize);
if (size <= 0) {
return emptyList();
}
TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS).field(FIELD_RULE_TAGS).size(size).order(BucketOrder.key(true)).minDocCount(1);
ofNullable(query).map(EsUtils::escapeSpecialRegexChars).map(queryString -> ".*" + queryString + ".*").map(s -> new IncludeExclude(s, null)).ifPresent(termsAggregation::includeExclude);
SearchRequest request = EsClient.prepareSearch(TYPE_RULE.getMainType()).source(new SearchSourceBuilder().query(matchAllQuery()).size(0).aggregation(termsAggregation));
SearchResponse esResponse = client.search(request);
return EsUtils.termsKeys(esResponse.getAggregations().get(AGGREGATION_NAME_FOR_TAGS));
}
use of org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude in project sonarqube by SonarSource.
the class SubAggregationHelper method buildSelectedItemsAggregation.
public <T> Optional<TermsAggregationBuilder> buildSelectedItemsAggregation(String name, TopAggregationDefinition<?> topAggregation, T[] selected) {
if (selected.length <= 0) {
return Optional.empty();
}
String includes = Arrays.stream(selected).filter(Objects::nonNull).map(s -> EsUtils.escapeSpecialRegexChars(s.toString())).collect(PIPE_JOINER);
TermsAggregationBuilder selectedTerms = AggregationBuilders.terms(name + Facets.SELECTED_SUB_AGG_NAME_SUFFIX).size(max(MAXIMUM_NUMBER_OF_SELECTED_ITEMS_WHOSE_DOC_COUNT_WILL_BE_CALCULATED, includes.length())).field(topAggregation.getFilterScope().getFieldName()).includeExclude(new IncludeExclude(includes, null));
if (subAggregation != null) {
selectedTerms = selectedTerms.subAggregation(subAggregation);
}
return of(selectedTerms);
}
use of org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude 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());
}
use of org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude in project sonarqube by SonarSource.
the class StickyFacetBuilder method addSelectedItemsToFacet.
public FilterAggregationBuilder addSelectedItemsToFacet(String fieldName, String facetName, FilterAggregationBuilder facetTopAggregation, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, Object... selected) {
if (selected.length <= 0) {
return facetTopAggregation;
}
String includes = Arrays.stream(selected).filter(Objects::nonNull).map(s -> EsUtils.escapeSpecialRegexChars(s.toString())).collect(PIPE_JOINER);
TermsAggregationBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected").size(max(MAXIMUM_NUMBER_OF_SELECTED_ITEMS_WHOSE_DOC_COUNT_WILL_BE_CALCULATED, includes.length())).field(fieldName).includeExclude(new IncludeExclude(includes, null));
if (subAggregation != null) {
selectedTerms = selectedTerms.subAggregation(subAggregation);
}
AggregationBuilder improvedAggregation = additionalAggregationFilter.apply(selectedTerms);
facetTopAggregation.subAggregation(improvedAggregation);
return facetTopAggregation;
}
use of org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude in project sonarqube by SonarSource.
the class IssueIndex method listTermsMatching.
private Terms listTermsMatching(String fieldName, IssueQuery query, @Nullable String textQuery, BucketOrder termsOrder, int size) {
SearchRequest requestBuilder = EsClient.prepareSearch(TYPE_ISSUE.getMainType());
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(0);
requestBuilder.source(sourceBuilder);
sourceBuilder.query(boolQuery().must(QueryBuilders.matchAllQuery()).filter(createBoolFilter(query)));
TermsAggregationBuilder aggreg = AggregationBuilders.terms("_ref").field(fieldName).size(size).order(termsOrder).minDocCount(1L);
if (textQuery != null) {
aggreg.includeExclude(new IncludeExclude(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery)), null));
}
sourceBuilder.aggregation(aggreg);
SearchResponse searchResponse = client.search(requestBuilder);
return searchResponse.getAggregations().get("_ref");
}
Aggregations