Search in sources :

Example 1 with SearchIdResult

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);
}
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 2 with SearchIdResult

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);
}
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 3 with SearchIdResult

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);
}
Also used : SearchOptions(org.sonar.server.es.SearchOptions) SearchIdResult(org.sonar.server.es.SearchIdResult) Test(org.junit.Test)

Example 4 with SearchIdResult

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);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchOptions(org.sonar.server.es.SearchOptions) SearchIdResult(org.sonar.server.es.SearchIdResult) Test(org.junit.Test)

Example 5 with SearchIdResult

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);
}
Also used : SearchOptions(org.sonar.server.es.SearchOptions) SearchIdResult(org.sonar.server.es.SearchIdResult) Test(org.junit.Test)

Aggregations

SearchIdResult (org.sonar.server.es.SearchIdResult)11 Test (org.junit.Test)9 SearchOptions (org.sonar.server.es.SearchOptions)9 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)1 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)1 SearchHit (org.elasticsearch.search.SearchHit)1 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)1