Search in sources :

Example 86 with SearchRequestBuilder

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

the class IssueIndex method search.

public SearchResult<IssueDoc> search(IssueQuery query, SearchOptions options) {
    SearchRequestBuilder requestBuilder = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE);
    configureSorting(query, requestBuilder);
    configurePagination(options, requestBuilder);
    configureRouting(query, options, requestBuilder);
    QueryBuilder esQuery = matchAllQuery();
    BoolQueryBuilder esFilter = boolQuery();
    Map<String, QueryBuilder> filters = createFilters(query);
    for (QueryBuilder filter : filters.values()) {
        if (filter != null) {
            esFilter.must(filter);
        }
    }
    if (esFilter.hasClauses()) {
        requestBuilder.setQuery(boolQuery().must(esQuery).filter(esFilter));
    } else {
        requestBuilder.setQuery(esQuery);
    }
    configureStickyFacets(query, options, filters, esQuery, requestBuilder);
    return new SearchResult<>(requestBuilder.get(), DOC_CONVERTER);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResult(org.sonar.server.es.SearchResult) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 87 with SearchRequestBuilder

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

the class ActiveRuleIndex method countByField.

private Map<String, Long> countByField(String indexField, QueryBuilder filter) {
    Map<String, Long> counts = new HashMap<>();
    SearchRequestBuilder request = getClient().prepareSearch(INDEX_TYPE_ACTIVE_RULE).setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter)).setSize(0).addAggregation(AggregationBuilders.terms(indexField).field(indexField).order(Terms.Order.count(false)).size(Integer.MAX_VALUE).minDocCount(0));
    SearchResponse response = request.get();
    Terms values = response.getAggregations().get(indexField);
    for (Terms.Bucket value : values.getBuckets()) {
        counts.put(value.getKeyAsString(), value.getDocCount());
    }
    return counts;
}
Also used : 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 88 with SearchRequestBuilder

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

the class RuleIndex method terms.

public Set<String> terms(String fields, @Nullable String query, int size) {
    String aggregationKey = "_ref";
    TermsBuilder termsAggregation = AggregationBuilders.terms(aggregationKey).field(fields).size(size).minDocCount(1);
    if (query != null) {
        termsAggregation.include(".*" + escapeSpecialRegexChars(query) + ".*");
    }
    SearchRequestBuilder request = getClient().prepareSearch(INDEX_TYPE_RULE, INDEX_TYPE_ACTIVE_RULE).setQuery(matchAllQuery()).setSize(0).addAggregation(termsAggregation);
    SearchResponse esResponse = request.get();
    Set<String> terms = new HashSet<>();
    Terms aggregation = esResponse.getAggregations().get(aggregationKey);
    if (aggregation != null) {
        aggregation.getBuckets().forEach(value -> terms.add(value.getKeyAsString()));
    }
    return terms;
}
Also used : TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) HashSet(java.util.HashSet)

Example 89 with SearchRequestBuilder

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

the class RuleIndex method searchAll.

/**
   * Return all keys matching the search query, without pagination nor facets
   */
public Iterator<RuleKey> searchAll(RuleQuery query) {
    SearchRequestBuilder esSearch = getClient().prepareSearch(INDEX_TYPE_RULE).setSearchType(SearchType.SCAN).setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
    QueryBuilder qb = buildQuery(query);
    Map<String, QueryBuilder> filters = buildFilters(query);
    setSorting(query, esSearch);
    BoolQueryBuilder fb = boolQuery();
    for (QueryBuilder filterBuilder : filters.values()) {
        fb.must(filterBuilder);
    }
    esSearch.setQuery(boolQuery().must(qb).filter(fb));
    SearchResponse response = esSearch.get();
    return scrollIds(getClient(), response.getScrollId(), ToRuleKey.INSTANCE);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) 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) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 90 with SearchRequestBuilder

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

the class TestIndexer method deleteProject.

@Override
public void deleteProject(String projectUuid) {
    SearchRequestBuilder searchRequest = esClient.prepareSearch(INDEX_TYPE_TEST).setTypes(INDEX_TYPE_TEST.getType()).setQuery(QueryBuilders.termQuery(TestIndexDefinition.FIELD_PROJECT_UUID, projectUuid));
    BulkIndexer.delete(esClient, INDEX_TYPE_TEST.getIndex(), searchRequest);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder)

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