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);
}
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;
}
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;
}
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);
}
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);
}
Aggregations