Search in sources :

Example 6 with TermsBuilder

use of org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder in project sonarqube by SonarSource.

the class IssueIndex method listTermsMatching.

private Terms listTermsMatching(String fieldName, IssueQuery query, @Nullable String textQuery, Terms.Order termsOrder, int maxNumberOfTags) {
    SearchRequestBuilder requestBuilder = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE).setSize(0);
    requestBuilder.setQuery(boolQuery().must(QueryBuilders.matchAllQuery()).filter(createBoolFilter(query)));
    TermsBuilder aggreg = AggregationBuilders.terms("_ref").field(fieldName).size(maxNumberOfTags).order(termsOrder).minDocCount(1L);
    if (textQuery != null) {
        aggreg.include(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery)));
    }
    SearchResponse searchResponse = requestBuilder.addAggregation(aggreg).get();
    return searchResponse.getAggregations().get("_ref");
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with TermsBuilder

use of org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder in project sonarqube by SonarSource.

the class StickyFacetBuilder method addSelectedItemsToFacet.

public FilterAggregationBuilder addSelectedItemsToFacet(String fieldName, String facetName, FilterAggregationBuilder facetTopAggregation, 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);
    TermsBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected").field(fieldName).include(includes);
    if (subAggregation != null) {
        selectedTerms = selectedTerms.subAggregation(subAggregation);
    }
    facetTopAggregation.subAggregation(selectedTerms);
    return facetTopAggregation;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Arrays(java.util.Arrays) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Collectors(java.util.stream.Collectors) Order(org.elasticsearch.search.aggregations.bucket.terms.Terms.Order) Objects(java.util.Objects) Map(java.util.Map) Collector(java.util.stream.Collector) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Nullable(javax.annotation.Nullable) ArrayUtils(org.apache.commons.lang.ArrayUtils) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) Objects(java.util.Objects)

Example 8 with TermsBuilder

use of org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder in project tutorials by eugenp.

the class ElasticSearchQueryIntegrationTest method givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately.

@Test
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
    final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
    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).sorted().collect(toList());
    assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "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) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

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