Search in sources :

Example 1 with TermsBuilder

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;
}
Also used : GlobalBuilder(org.elasticsearch.search.aggregations.bucket.global.GlobalBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with TermsBuilder

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());
}
Also used : TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms)

Example 3 with TermsBuilder

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);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 4 with TermsBuilder

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;
}
Also used : TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) ArrayList(java.util.ArrayList) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with TermsBuilder

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;
}
Also used : TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) HashSet(java.util.HashSet)

Aggregations

TermsBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder)8 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)5 Aggregation (org.elasticsearch.search.aggregations.Aggregation)2 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)2 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Collector (java.util.stream.Collector)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 ArrayUtils (org.apache.commons.lang.ArrayUtils)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 QueryBuilders.boolQuery (org.elasticsearch.index.query.QueryBuilders.boolQuery)1