use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.
the class IssueIndex method listTags.
public List<String> listTags(IssueQuery query, @Nullable String textQuery, int maxNumberOfTags) {
SearchRequestBuilder requestBuilder = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE, RuleIndexDefinition.INDEX_TYPE_RULE);
requestBuilder.setQuery(boolQuery().must(matchAllQuery()).filter(createBoolFilter(query)));
GlobalBuilder topAggreg = AggregationBuilders.global("tags");
String tagsOnIssuesSubAggregation = "tags__issues";
String tagsOnRulesSubAggregation = "tags__rules";
TermsBuilder issueTags = AggregationBuilders.terms(tagsOnIssuesSubAggregation).field(IssueIndexDefinition.FIELD_ISSUE_TAGS).size(maxNumberOfTags).order(Terms.Order.term(true)).minDocCount(1L);
TermsBuilder ruleTags = AggregationBuilders.terms(tagsOnRulesSubAggregation).field(RuleIndexDefinition.FIELD_RULE_ALL_TAGS).size(maxNumberOfTags).order(Terms.Order.term(true)).minDocCount(1L);
if (textQuery != null) {
String escapedTextQuery = escapeSpecialRegexChars(textQuery);
issueTags.include(format(SUBSTRING_MATCH_REGEXP, escapedTextQuery));
ruleTags.include(format(SUBSTRING_MATCH_REGEXP, escapedTextQuery));
}
SearchResponse searchResponse = requestBuilder.addAggregation(topAggreg.subAggregation(issueTags).subAggregation(ruleTags)).get();
Global allTags = searchResponse.getAggregations().get("tags");
SortedSet<String> result = Sets.newTreeSet();
Terms issuesResult = allTags.getAggregations().get(tagsOnIssuesSubAggregation);
Terms rulesResult = allTags.getAggregations().get(tagsOnRulesSubAggregation);
result.addAll(EsUtils.termsKeys(issuesResult));
result.addAll(EsUtils.termsKeys(rulesResult));
List<String> resultAsList = Lists.newArrayList(result);
return resultAsList.size() > maxNumberOfTags && maxNumberOfTags > 0 ? resultAsList.subList(0, maxNumberOfTags) : resultAsList;
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.
the class IssueIndex method getMinCreatedAt.
private Optional<Long> getMinCreatedAt(Map<String, QueryBuilder> filters, QueryBuilder esQuery) {
String facetNameAndField = IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT;
SearchRequestBuilder esRequest = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE).setSize(0);
BoolQueryBuilder esFilter = boolQuery();
filters.values().stream().filter(Objects::nonNull).forEach(esFilter::must);
if (esFilter.hasClauses()) {
esRequest.setQuery(QueryBuilders.filteredQuery(esQuery, esFilter));
} else {
esRequest.setQuery(esQuery);
}
esRequest.addAggregation(AggregationBuilders.min(facetNameAndField).field(facetNameAndField));
Min minValue = esRequest.get().getAggregations().get(facetNameAndField);
Double actualValue = minValue.getValue();
if (actualValue.isInfinite()) {
return Optional.empty();
}
return Optional.of(actualValue.longValue());
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.
the class EsClient method getMaxFieldValue.
public long getMaxFieldValue(IndexType indexType, String fieldName) {
SearchRequestBuilder request = prepareSearch(indexType).setQuery(QueryBuilders.matchAllQuery()).setSize(0).addAggregation(AggregationBuilders.max("latest").field(fieldName));
Max max = request.get().getAggregations().get("latest");
return (long) max.getValue();
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.
the class IssueIndexer method deleteProject.
@Override
public void deleteProject(String uuid) {
BulkIndexer bulk = new BulkIndexer(esClient, INDEX_TYPE_ISSUE.getIndex());
bulk.start();
SearchRequestBuilder search = esClient.prepareSearch(INDEX_TYPE_ISSUE).setRouting(uuid).setQuery(boolQuery().must(termQuery(FIELD_ISSUE_PROJECT_UUID, uuid)));
bulk.addDeletion(search);
bulk.stop();
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method searchTags.
public List<String> searchTags(@Nullable String textQuery, int pageSize) {
checkArgument(pageSize <= 100, "Page size must be lower than or equals to " + 100);
if (pageSize == 0) {
return emptyList();
}
TermsBuilder tagFacet = AggregationBuilders.terms(FIELD_TAGS).field(FIELD_TAGS).size(pageSize).minDocCount(1).order(Terms.Order.term(true));
if (textQuery != null) {
tagFacet.include(".*" + escapeSpecialRegexChars(textQuery) + ".*");
}
SearchRequestBuilder searchQuery = getClient().prepareSearch(INDEX_TYPE_PROJECT_MEASURES).setQuery(authorizationTypeSupport.createQueryFilter()).setFetchSource(false).setSize(0).addAggregation(tagFacet);
Terms aggregation = searchQuery.get().getAggregations().get(FIELD_TAGS);
return aggregation.getBuckets().stream().map(Bucket::getKeyAsString).collect(Collectors.toList());
}
Aggregations