Search in sources :

Example 21 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project incubator-skywalking by apache.

the class ServiceReferenceEsMetricUIDAO method getFrontServices.

@Override
public List<ServiceReferenceMetric> getFrontServices(Step step, long startTimeBucket, long endTimeBucket, MetricSource metricSource, int behindServiceId) {
    String tableName = TimePyramidTableNameBuilder.build(step, ServiceReferenceMetricTable.TABLE);
    SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(tableName);
    searchRequestBuilder.setTypes(ServiceReferenceMetricTable.TABLE_TYPE);
    searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.must().add(QueryBuilders.rangeQuery(ServiceReferenceMetricTable.COLUMN_TIME_BUCKET).gte(startTimeBucket).lte(endTimeBucket));
    boolQuery.must().add(QueryBuilders.termQuery(ServiceReferenceMetricTable.COLUMN_BEHIND_SERVICE_ID, behindServiceId));
    boolQuery.must().add(QueryBuilders.termQuery(ServiceReferenceMetricTable.COLUMN_SOURCE_VALUE, metricSource.getValue()));
    searchRequestBuilder.setQuery(boolQuery);
    searchRequestBuilder.setSize(0);
    TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms(ServiceReferenceMetricTable.COLUMN_FRONT_SERVICE_ID).field(ServiceReferenceMetricTable.COLUMN_FRONT_SERVICE_ID).size(100);
    aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceReferenceMetricTable.COLUMN_TRANSACTION_CALLS).field(ServiceReferenceMetricTable.COLUMN_TRANSACTION_CALLS));
    aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceReferenceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS).field(ServiceReferenceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS));
    aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceReferenceMetricTable.COLUMN_TRANSACTION_DURATION_SUM).field(ServiceReferenceMetricTable.COLUMN_TRANSACTION_DURATION_SUM));
    aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceReferenceMetricTable.COLUMN_TRANSACTION_ERROR_DURATION_SUM).field(ServiceReferenceMetricTable.COLUMN_TRANSACTION_ERROR_DURATION_SUM));
    searchRequestBuilder.addAggregation(aggregationBuilder);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    List<ServiceReferenceMetric> referenceMetrics = new LinkedList<>();
    Terms frontServiceIdTerms = searchResponse.getAggregations().get(ServiceReferenceMetricTable.COLUMN_FRONT_SERVICE_ID);
    buildNodeByBehindServiceId(referenceMetrics, frontServiceIdTerms, behindServiceId);
    return referenceMetrics;
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) LinkedList(java.util.LinkedList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder 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));
}
Also used : JavaTokenizer(org.sonar.server.es.textsearch.JavaTokenizer) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) SortBuilders(org.elasticsearch.search.sort.SortBuilders) SCROLL_TIME_IN_MINUTES(org.sonar.server.es.EsUtils.SCROLL_TIME_IN_MINUTES) FIELD_RULE_INTERNAL_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_INTERNAL_KEY) FIELD_RULE_CREATED_AT(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_CREATED_AT) TYPE_RULE(org.sonar.server.rule.index.RuleIndexDefinition.TYPE_RULE) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) FIELD_RULE_TAGS(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_TAGS) StickyFacetBuilder(org.sonar.server.es.StickyFacetBuilder) EsClient(org.sonar.server.es.EsClient) FACET_DEFAULT_SIZE(org.sonar.server.es.StickyFacetBuilder.FACET_DEFAULT_SIZE) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SEARCH_WORDS_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_WORDS_ANALYZER) Map(java.util.Map) FIELD_RULE_STATUS(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_STATUS) SearchResponse(org.elasticsearch.action.search.SearchResponse) BucketOrder(org.elasticsearch.search.aggregations.BucketOrder) FIELD_RULE_UPDATED_AT(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_UPDATED_AT) FIELD_ACTIVE_RULE_INHERITANCE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE) JoinAggregationBuilders(org.elasticsearch.join.aggregations.JoinAggregationBuilders) FIELD_RULE_OWASP_TOP_10(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_OWASP_TOP_10) TimeValue(org.elasticsearch.core.TimeValue) FIELD_RULE_NAME(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_NAME) QueryBuilders.matchPhraseQuery(org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) System2(org.sonar.api.utils.System2) SearchIdResult(org.sonar.server.es.SearchIdResult) Collections.emptyList(java.util.Collections.emptyList) VULNERABILITY(org.sonar.api.rules.RuleType.VULNERABILITY) Collection(java.util.Collection) FIELD_RULE_IS_EXTERNAL(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_IS_EXTERNAL) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SORTABLE_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER) ScoreMode(org.apache.lucene.search.join.ScoreMode) FIELD_RULE_REPOSITORY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_REPOSITORY) List(java.util.List) SearchOptions(org.sonar.server.es.SearchOptions) QueryBuilders.matchQuery(org.elasticsearch.index.query.QueryBuilders.matchQuery) HasParentQueryBuilder(org.elasticsearch.join.query.HasParentQueryBuilder) FIELD_RULE_IS_TEMPLATE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE) SortOrder(org.elasticsearch.search.sort.SortOrder) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) FIELD_RULE_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_KEY) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TRUE(java.lang.Boolean.TRUE) Joiner(com.google.common.base.Joiner) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) FIELD_RULE_SANS_TOP_25(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_SANS_TOP_25) JoinQueryBuilders(org.elasticsearch.join.query.JoinQueryBuilders) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) RuleStatus(org.sonar.api.rule.RuleStatus) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) Function(java.util.function.Function) FIELD_RULE_LANGUAGE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_LANGUAGE) EsUtils(org.sonar.server.es.EsUtils) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) EsUtils.scrollIds(org.sonar.server.es.EsUtils.scrollIds) ENGLISH_HTML_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.ENGLISH_HTML_ANALYZER) SEARCH_GRAMS_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER) Operator(org.elasticsearch.index.query.Operator) Severity(org.sonar.api.rule.Severity) FIELD_ACTIVE_RULE_SEVERITY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY) FIELD_RULE_CWE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_CWE) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) FIELD_ACTIVE_RULE_PROFILE_UUID(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_UUID) FIELD_INDEX_TYPE(org.sonar.server.es.IndexType.FIELD_INDEX_TYPE) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) QueryBuilders.termsQuery(org.elasticsearch.index.query.QueryBuilders.termsQuery) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FALSE(java.lang.Boolean.FALSE) FIELD_RULE_SEVERITY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_SEVERITY) FIELD_RULE_SONARSOURCE_SECURITY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_SONARSOURCE_SECURITY) QueryBuilders.matchAllQuery(org.elasticsearch.index.query.QueryBuilders.matchAllQuery) Iterator(java.util.Iterator) Optional.ofNullable(java.util.Optional.ofNullable) FIELD_RULE_HTML_DESCRIPTION(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_HTML_DESCRIPTION) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) FIELD_RULE_RULE_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_RULE_KEY) FIELD_RULE_TEMPLATE_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_TEMPLATE_KEY) FIELD_RULE_TYPE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_TYPE) EsUtils.optimizeScrollRequest(org.sonar.server.es.EsUtils.optimizeScrollRequest) TYPE_ACTIVE_RULE(org.sonar.server.rule.index.RuleIndexDefinition.TYPE_ACTIVE_RULE) DefaultIndexSettings(org.sonar.server.es.newindex.DefaultIndexSettings) SearchRequest(org.elasticsearch.action.search.SearchRequest) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 23 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTermTopAggregation_has_empty_filter_when_FiltersComputer_returns_empty_for_TopAggregation.

@Test
public void buildTermTopAggregation_has_empty_filter_when_FiltersComputer_returns_empty_for_TopAggregation() {
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    SimpleFieldTopAggregationDefinition otherTopAggregation = new SimpleFieldTopAggregationDefinition("acme", false);
    BoolQueryBuilder otherFilter = boolQuery();
    when(filtersComputer.getTopAggregationFilter(topAggregation)).thenReturn(Optional.empty());
    when(filtersComputer.getTopAggregationFilter(otherTopAggregation)).thenReturn(Optional.of(otherFilter));
    String topAggregationName = randomAlphabetic(10);
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, NO_EXTRA_FILTER, NO_OTHER_SUBAGGREGATION);
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isEqualTo(boolQuery()).isNotSameAs(otherFilter);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.Test)

Example 24 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTermTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation_and_extra_one.

@Test
public void buildTermTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation_and_extra_one() {
    String topAggregationName = randomAlphabetic(10);
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    SimpleFieldTopAggregationDefinition otherTopAggregation = new SimpleFieldTopAggregationDefinition("acme", false);
    BoolQueryBuilder computerFilter = boolQuery();
    BoolQueryBuilder otherFilter = boolQuery();
    BoolQueryBuilder extraFilter = boolQuery();
    when(filtersComputer.getTopAggregationFilter(topAggregation)).thenReturn(Optional.of(computerFilter));
    when(filtersComputer.getTopAggregationFilter(otherTopAggregation)).thenReturn(Optional.of(otherFilter));
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, t -> t.must(extraFilter), NO_OTHER_SUBAGGREGATION);
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isEqualTo(computerFilter);
    assertThat(((BoolQueryBuilder) aggregationBuilder.getFilter()).must()).containsExactly(extraFilter);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.Test)

Example 25 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTermTopAggregation_adds_term_subaggregation_from_subAggregationHelper.

@Test
public void buildTermTopAggregation_adds_term_subaggregation_from_subAggregationHelper() {
    String topAggregationName = randomAlphabetic(10);
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, NO_EXTRA_FILTER, NO_OTHER_SUBAGGREGATION);
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getSubAggregations()).hasSize(1);
    assertThat(aggregationBuilder.getSubAggregations().iterator().next()).isSameAs(termSubAgg);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Test(org.junit.Test)

Aggregations

TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)64 SignificantTermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregationBuilder)20 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)13 Test (org.junit.Test)11 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)8 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)8 LinkedList (java.util.LinkedList)7 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)6 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude)6 Arrays (java.util.Arrays)4 Map (java.util.Map)4 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)4 AggregationBuilders (org.elasticsearch.search.aggregations.AggregationBuilders)4 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)4 Nullable (javax.annotation.Nullable)3 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)3