Search in sources :

Example 21 with SearchRequestBuilder

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

the class ProjectMeasuresIndex method search.

public SearchIdResult<String> search(ProjectMeasuresQuery query, SearchOptions searchOptions) {
    SearchRequestBuilder requestBuilder = getClient().prepareSearch(INDEX_TYPE_PROJECT_MEASURES).setFetchSource(false).setFrom(searchOptions.getOffset()).setSize(searchOptions.getLimit());
    BoolQueryBuilder esFilter = boolQuery();
    Map<String, QueryBuilder> filters = createFilters(query);
    filters.values().forEach(esFilter::must);
    requestBuilder.setQuery(esFilter);
    addFacets(requestBuilder, searchOptions, filters, query);
    addSort(query, requestBuilder);
    return new SearchIdResult<>(requestBuilder.get(), id -> id);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchIdResult(org.sonar.server.es.SearchIdResult)

Example 22 with SearchRequestBuilder

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

the class RuleIndex method search.

public SearchIdResult<RuleKey> search(RuleQuery query, SearchOptions options) {
    SearchRequestBuilder esSearch = getClient().prepareSearch(INDEX_TYPE_RULE);
    QueryBuilder qb = buildQuery(query);
    Map<String, QueryBuilder> filters = buildFilters(query);
    if (!options.getFacets().isEmpty()) {
        for (AbstractAggregationBuilder aggregation : getFacets(query, options, qb, filters).values()) {
            esSearch.addAggregation(aggregation);
        }
    }
    setSorting(query, esSearch);
    setPagination(options, esSearch);
    BoolQueryBuilder fb = boolQuery();
    for (QueryBuilder filterBuilder : filters.values()) {
        fb.must(filterBuilder);
    }
    esSearch.setQuery(boolQuery().must(qb).filter(fb));
    return new SearchIdResult<>(esSearch.get(), ToRuleKey.INSTANCE);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchIdResult(org.sonar.server.es.SearchIdResult)

Example 23 with SearchRequestBuilder

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

the class ActiveRuleIndex method getStatsByProfileKeys.

public Map<String, Multimap<String, FacetValue>> getStatsByProfileKeys(List<String> keys) {
    SearchRequestBuilder request = getClient().prepareSearch(INDEX_TYPE_RULE.getIndex()).setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(FIELD_ACTIVE_RULE_PROFILE_KEY, keys)).filter(QueryBuilders.boolQuery().mustNot(QueryBuilders.hasParentQuery(INDEX_TYPE_RULE.getType(), QueryBuilders.termQuery(FIELD_RULE_STATUS, RuleStatus.REMOVED.name()))))).addAggregation(AggregationBuilders.terms(FIELD_ACTIVE_RULE_PROFILE_KEY).field(RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY).size(0).subAggregation(AggregationBuilders.terms(FIELD_ACTIVE_RULE_INHERITANCE).field(RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE)).subAggregation(AggregationBuilders.terms(FIELD_ACTIVE_RULE_SEVERITY).field(RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY)).subAggregation(AggregationBuilders.count(COUNT_ACTIVE_RULES))).setSize(0);
    SearchResponse response = request.get();
    Map<String, Multimap<String, FacetValue>> stats = new HashMap<>();
    Aggregation aggregation = response.getAggregations().get(FIELD_ACTIVE_RULE_PROFILE_KEY);
    for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
        stats.put(value.getKeyAsString(), processAggregations(value.getAggregations()));
    }
    return stats;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Multimap(com.google.common.collect.Multimap) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 24 with SearchRequestBuilder

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

the class ActiveRuleIndexer method deleteKeys.

private void deleteKeys(List<ActiveRuleKey> keys) {
    BulkIndexer bulk = new BulkIndexer(esClient, INDEX_TYPE_ACTIVE_RULE.getIndex());
    bulk.start();
    SearchRequestBuilder search = esClient.prepareSearch(INDEX_TYPE_ACTIVE_RULE).setQuery(QueryBuilders.boolQuery().must(termsQuery(FIELD_ACTIVE_RULE_KEY, keys)));
    bulk.addDeletion(search);
    bulk.stop();
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BulkIndexer(org.sonar.server.es.BulkIndexer)

Example 25 with SearchRequestBuilder

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

the class ActiveRuleIndexer method deleteProfile.

public void deleteProfile(String qualityProfileKey) {
    BulkIndexer bulk = new BulkIndexer(esClient, INDEX_TYPE_ACTIVE_RULE.getIndex());
    bulk.start();
    SearchRequestBuilder search = esClient.prepareSearch(INDEX_TYPE_ACTIVE_RULE).setQuery(QueryBuilders.boolQuery().must(termsQuery(FIELD_ACTIVE_RULE_PROFILE_KEY, qualityProfileKey)));
    bulk.addDeletion(search);
    bulk.stop();
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BulkIndexer(org.sonar.server.es.BulkIndexer)

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