Search in sources :

Example 1 with IncludeExclude

use of org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude 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 2 with IncludeExclude

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

the class SubAggregationHelper method buildSelectedItemsAggregation.

public <T> Optional<TermsAggregationBuilder> buildSelectedItemsAggregation(String name, TopAggregationDefinition<?> topAggregation, T[] selected) {
    if (selected.length <= 0) {
        return Optional.empty();
    }
    String includes = Arrays.stream(selected).filter(Objects::nonNull).map(s -> EsUtils.escapeSpecialRegexChars(s.toString())).collect(PIPE_JOINER);
    TermsAggregationBuilder selectedTerms = AggregationBuilders.terms(name + Facets.SELECTED_SUB_AGG_NAME_SUFFIX).size(max(MAXIMUM_NUMBER_OF_SELECTED_ITEMS_WHOSE_DOC_COUNT_WILL_BE_CALCULATED, includes.length())).field(topAggregation.getFilterScope().getFieldName()).includeExclude(new IncludeExclude(includes, null));
    if (subAggregation != null) {
        selectedTerms = selectedTerms.subAggregation(subAggregation);
    }
    return of(selectedTerms);
}
Also used : Arrays(java.util.Arrays) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) Facets(org.sonar.server.es.Facets) Optional.of(java.util.Optional.of) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) Collectors(java.util.stream.Collectors) EsUtils(org.sonar.server.es.EsUtils) Objects(java.util.Objects) Optional(java.util.Optional) Math.max(java.lang.Math.max) Collector(java.util.stream.Collector) BucketOrder(org.elasticsearch.search.aggregations.BucketOrder) CheckForNull(javax.annotation.CheckForNull) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Nullable(javax.annotation.Nullable) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) Objects(java.util.Objects)

Example 3 with IncludeExclude

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

the class ProjectMeasuresIndex method searchTags.

public List<String> searchTags(@Nullable String textQuery, int page, int size) {
    int maxPageSize = 100;
    int maxPage = 20;
    checkArgument(size <= maxPageSize, "Page size must be lower than or equals to " + maxPageSize);
    checkArgument(page > 0 && page <= maxPage, "Page must be between 0 and " + maxPage);
    if (size <= 0) {
        return emptyList();
    }
    TermsAggregationBuilder tagFacet = AggregationBuilders.terms(FIELD_TAGS).field(FIELD_TAGS).size(size * page).minDocCount(1).order(BucketOrder.key(true));
    if (textQuery != null) {
        tagFacet.includeExclude(new IncludeExclude(".*" + escapeSpecialRegexChars(textQuery) + ".*", null));
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(authorizationTypeSupport.createQueryFilter()).fetchSource(false).aggregation(tagFacet);
    SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder));
    Terms aggregation = response.getAggregations().get(FIELD_TAGS);
    return aggregation.getBuckets().stream().skip((page - 1) * size).map(Bucket::getKeyAsString).collect(MoreCollectors.toList());
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with IncludeExclude

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

the class StickyFacetBuilder method addSelectedItemsToFacet.

public FilterAggregationBuilder addSelectedItemsToFacet(String fieldName, String facetName, FilterAggregationBuilder facetTopAggregation, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, 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);
    TermsAggregationBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected").size(max(MAXIMUM_NUMBER_OF_SELECTED_ITEMS_WHOSE_DOC_COUNT_WILL_BE_CALCULATED, includes.length())).field(fieldName).includeExclude(new IncludeExclude(includes, null));
    if (subAggregation != null) {
        selectedTerms = selectedTerms.subAggregation(subAggregation);
    }
    AggregationBuilder improvedAggregation = additionalAggregationFilter.apply(selectedTerms);
    facetTopAggregation.subAggregation(improvedAggregation);
    return facetTopAggregation;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Arrays(java.util.Arrays) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Map(java.util.Map) Math.max(java.lang.Math.max) Collector(java.util.stream.Collector) BucketOrder(org.elasticsearch.search.aggregations.BucketOrder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Nullable(javax.annotation.Nullable) ArrayUtils(org.apache.commons.lang.ArrayUtils) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) Objects(java.util.Objects)

Example 5 with IncludeExclude

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

the class IssueIndex method listTermsMatching.

private Terms listTermsMatching(String fieldName, IssueQuery query, @Nullable String textQuery, BucketOrder termsOrder, int size) {
    SearchRequest requestBuilder = EsClient.prepareSearch(TYPE_ISSUE.getMainType());
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(0);
    requestBuilder.source(sourceBuilder);
    sourceBuilder.query(boolQuery().must(QueryBuilders.matchAllQuery()).filter(createBoolFilter(query)));
    TermsAggregationBuilder aggreg = AggregationBuilders.terms("_ref").field(fieldName).size(size).order(termsOrder).minDocCount(1L);
    if (textQuery != null) {
        aggreg.includeExclude(new IncludeExclude(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery)), null));
    }
    sourceBuilder.aggregation(aggreg);
    SearchResponse searchResponse = client.search(requestBuilder);
    return searchResponse.getAggregations().get("_ref");
}
Also used : 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)

Aggregations

IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude)8 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)8 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)4 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)4 Arrays (java.util.Arrays)3 Nullable (javax.annotation.Nullable)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 AggregationBuilders (org.elasticsearch.search.aggregations.AggregationBuilders)3 BucketOrder (org.elasticsearch.search.aggregations.BucketOrder)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 Math.max (java.lang.Math.max)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Function (java.util.function.Function)2 Collector (java.util.stream.Collector)2 Collectors (java.util.stream.Collectors)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 QueryBuilders.boolQuery (org.elasticsearch.index.query.QueryBuilders.boolQuery)2 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)2