use of org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder 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.search.aggregations.bucket.terms.TermsBuilder 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());
}
use of org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder in project tutorials by eugenp.
the class ElasticSearchQueryIntegrationTest method givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually.
@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
final Map<String, Aggregation> results = response.getAggregations().asMap();
final StringTerms topTags = (StringTerms) results.get("top_tags");
final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).collect(toList());
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
}
use of org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder in project opencast by opencast.
the class AbstractSearchIndex method getTermsForField.
/**
* Returns all the known terms for a field (aka facets).
*
* @param field
* the field name
* @param types
* an optional array of document types, if none is set all types are searched
* @return the list of terms
*/
public List<String> getTermsForField(String field, Option<String[]> types) {
final String facetName = "terms";
TermsBuilder aggBuilder = AggregationBuilders.terms(facetName).field(field);
SearchRequestBuilder search = getSearchClient().prepareSearch(getIndexName()).addAggregation(aggBuilder);
if (types.isSome())
search = search.setTypes(types.get());
SearchResponse response = search.execute().actionGet();
List<String> terms = new ArrayList<>();
Terms aggs = response.getAggregations().get(facetName);
for (Bucket bucket : aggs.getBuckets()) {
terms.add(bucket.getKey());
}
return terms;
}
use of org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder in project sonarqube by SonarSource.
the class RuleIndex method terms.
public Set<String> terms(String fields, @Nullable String query, int size) {
String aggregationKey = "_ref";
TermsBuilder termsAggregation = AggregationBuilders.terms(aggregationKey).field(fields).size(size).minDocCount(1);
if (query != null) {
termsAggregation.include(".*" + escapeSpecialRegexChars(query) + ".*");
}
SearchRequestBuilder request = getClient().prepareSearch(INDEX_TYPE_RULE, INDEX_TYPE_ACTIVE_RULE).setQuery(matchAllQuery()).setSize(0).addAggregation(termsAggregation);
SearchResponse esResponse = request.get();
Set<String> terms = new HashSet<>();
Terms aggregation = esResponse.getAggregations().get(aggregationKey);
if (aggregation != null) {
aggregation.getBuckets().forEach(value -> terms.add(value.getKeyAsString()));
}
return terms;
}
Aggregations