use of org.sonar.server.es.SearchIdResult 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);
}
use of org.sonar.server.es.SearchIdResult 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);
}
use of org.sonar.server.es.SearchIdResult in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_is_template.
@Test
public void search_by_is_template() {
indexRules(newDoc(RuleKey.of("java", "S001")).setIsTemplate(false), newDoc(RuleKey.of("java", "S002")).setIsTemplate(true));
// find all
RuleQuery query = new RuleQuery();
SearchIdResult results = index.search(query, new SearchOptions());
assertThat(results.getIds()).hasSize(2);
// Only template
query = new RuleQuery().setIsTemplate(true);
results = index.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(RuleKey.of("java", "S002"));
// Only not template
query = new RuleQuery().setIsTemplate(false);
results = index.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(RuleKey.of("java", "S001"));
// null => no filter
query = new RuleQuery().setIsTemplate(null);
assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
}
use of org.sonar.server.es.SearchIdResult in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_any_of_repositories.
@Test
public void search_by_any_of_repositories() {
indexRules(newDoc(RuleKey.of("findbugs", "S001")), newDoc(RuleKey.of("pmd", "S002")));
List<SearchHit> docs = tester.getDocuments(INDEX_TYPE_RULE);
for (SearchHit doc : docs) {
System.out.println(doc.getSourceAsString());
}
RuleQuery query = new RuleQuery().setRepositories(asList("checkstyle", "pmd"));
SearchIdResult results = index.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(RuleKey.of("pmd", "S002"));
// no results
query = new RuleQuery().setRepositories(singletonList("checkstyle"));
assertThat(index.search(query, new SearchOptions()).getIds()).isEmpty();
// empty list => no filter
query = new RuleQuery().setRepositories(Collections.emptyList());
assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
}
use of org.sonar.server.es.SearchIdResult in project sonarqube by SonarSource.
the class RuleIndexTest method paging.
@Test
public void paging() {
indexRules(newDoc(RuleKey.of("java", "S001")), newDoc(RuleKey.of("java", "S002")), newDoc(RuleKey.of("java", "S003")));
// from 0 to 1 included
SearchOptions options = new SearchOptions();
options.setOffset(0).setLimit(2);
SearchIdResult results = index.search(new RuleQuery(), options);
assertThat(results.getTotal()).isEqualTo(3);
assertThat(results.getIds()).hasSize(2);
// from 0 to 9 included
options.setOffset(0).setLimit(10);
results = index.search(new RuleQuery(), options);
assertThat(results.getTotal()).isEqualTo(3);
assertThat(results.getIds()).hasSize(3);
// from 2 to 11 included
options.setOffset(2).setLimit(10);
results = index.search(new RuleQuery(), options);
assertThat(results.getTotal()).isEqualTo(3);
assertThat(results.getIds()).hasSize(1);
// from 2 to 11 included
options.setOffset(2).setLimit(0);
results = index.search(new RuleQuery(), options);
assertThat(results.getTotal()).isEqualTo(3);
assertThat(results.getIds()).hasSize(1);
}
Aggregations