Search in sources :

Example 16 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder 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 17 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.

the class IssueIndex method getMinCreatedAt.

private Optional<Long> getMinCreatedAt(Map<String, QueryBuilder> filters, QueryBuilder esQuery) {
    String facetNameAndField = IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT;
    SearchRequestBuilder esRequest = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE).setSize(0);
    BoolQueryBuilder esFilter = boolQuery();
    filters.values().stream().filter(Objects::nonNull).forEach(esFilter::must);
    if (esFilter.hasClauses()) {
        esRequest.setQuery(QueryBuilders.filteredQuery(esQuery, esFilter));
    } else {
        esRequest.setQuery(esQuery);
    }
    esRequest.addAggregation(AggregationBuilders.min(facetNameAndField).field(facetNameAndField));
    Min minValue = esRequest.get().getAggregations().get(facetNameAndField);
    Double actualValue = minValue.getValue();
    if (actualValue.isInfinite()) {
        return Optional.empty();
    }
    return Optional.of(actualValue.longValue());
}
Also used : Min(org.elasticsearch.search.aggregations.metrics.min.Min) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 18 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.

the class EsClient method getMaxFieldValue.

public long getMaxFieldValue(IndexType indexType, String fieldName) {
    SearchRequestBuilder request = prepareSearch(indexType).setQuery(QueryBuilders.matchAllQuery()).setSize(0).addAggregation(AggregationBuilders.max("latest").field(fieldName));
    Max max = request.get().getAggregations().get("latest");
    return (long) max.getValue();
}
Also used : ProxySearchRequestBuilder(org.sonar.server.es.request.ProxySearchRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Max(org.elasticsearch.search.aggregations.metrics.max.Max)

Example 19 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.

the class IssueIndexer method deleteProject.

@Override
public void deleteProject(String uuid) {
    BulkIndexer bulk = new BulkIndexer(esClient, INDEX_TYPE_ISSUE.getIndex());
    bulk.start();
    SearchRequestBuilder search = esClient.prepareSearch(INDEX_TYPE_ISSUE).setRouting(uuid).setQuery(boolQuery().must(termQuery(FIELD_ISSUE_PROJECT_UUID, uuid)));
    bulk.addDeletion(search);
    bulk.stop();
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BulkIndexer(org.sonar.server.es.BulkIndexer)

Example 20 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder 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)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)110 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)23 SearchHits (org.elasticsearch.search.SearchHits)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 ArrayList (java.util.ArrayList)14 Client (org.elasticsearch.client.Client)13 HashMap (java.util.HashMap)12 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)9 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 Map (java.util.Map)7 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)7 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)7