Search in sources :

Example 1 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class SearchProjectsAction method searchData.

private SearchResults searchData(DbSession dbSession, SearchProjectsRequest request, @Nullable OrganizationDto organization) {
    Set<String> favoriteProjectUuids = loadFavoriteProjectUuids(dbSession);
    List<Criterion> criteria = FilterParser.parse(firstNonNull(request.getFilter(), ""));
    ProjectMeasuresQuery query = newProjectMeasuresQuery(criteria, hasFavoriteFilter(criteria) ? favoriteProjectUuids : null).setSort(request.getSort()).setAsc(request.getAsc());
    Optional.ofNullable(organization).map(OrganizationDto::getUuid).ifPresent(query::setOrganizationUuid);
    queryValidator.validate(dbSession, query);
    SearchIdResult<String> esResults = index.search(query, new SearchOptions().addFacets(request.getFacets()).setPage(request.getPage(), request.getPageSize()));
    List<String> projectUuids = esResults.getIds();
    Ordering<ComponentDto> ordering = Ordering.explicit(projectUuids).onResultOf(ComponentDto::uuid);
    List<ComponentDto> projects = ordering.immutableSortedCopy(dbClient.componentDao().selectByUuids(dbSession, projectUuids));
    Map<String, SnapshotDto> analysisByProjectUuid = getSnapshots(dbSession, request, projectUuids);
    return new SearchResults(projects, favoriteProjectUuids, esResults, analysisByProjectUuid, query);
}
Also used : ProjectMeasuresQueryFactory.newProjectMeasuresQuery(org.sonar.server.component.ws.ProjectMeasuresQueryFactory.newProjectMeasuresQuery) ProjectMeasuresQuery(org.sonar.server.measure.index.ProjectMeasuresQuery) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) SearchOptions(org.sonar.server.es.SearchOptions) Criterion(org.sonar.server.component.ws.FilterParser.Criterion)

Example 2 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class SearchAction method doHandle.

private SearchWsResponse doHandle(SearchWsRequest request, Request wsRequest) {
    // prepare the Elasticsearch request
    SearchOptions options = createSearchOptionsFromRequest(request);
    EnumSet<SearchAdditionalField> additionalFields = SearchAdditionalField.getFromRequest(request);
    IssueQuery query = issueQueryService.createFromRequest(request);
    // execute request
    SearchResult<IssueDoc> result = issueIndex.search(query, options);
    List<String> issueKeys = from(result.getDocs()).transform(IssueDocToKey.INSTANCE).toList();
    // load the additional information to be returned in response
    SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(additionalFields, issueKeys);
    collectLoggedInUser(collector);
    collectRequestParams(collector, request);
    Facets facets = null;
    if (!options.getFacets().isEmpty()) {
        facets = result.getFacets();
        // add missing values to facets. For example if assignee "john" and facet on "assignees" are requested, then
        // "john" should always be listed in the facet. If it is not present, then it is added with value zero.
        // This is a constraint from webapp UX.
        completeFacets(facets, request, wsRequest);
        collectFacets(collector, facets);
    }
    SearchResponseData data = searchResponseLoader.load(collector, facets);
    // format response
    // Filter and reorder facets according to the requested ordered names.
    // Must be done after loading of data as the "hidden" facet "debt"
    // can be used to get total debt.
    facets = reorderFacets(facets, options.getFacets());
    // FIXME allow long in Paging
    Paging paging = forPageIndex(options.getPage()).withPageSize(options.getLimit()).andTotal((int) result.getTotal());
    return searchResponseFormat.formatSearch(additionalFields, data, paging, facets);
}
Also used : IssueDoc(org.sonar.server.issue.index.IssueDoc) Facets(org.sonar.server.es.Facets) Paging(org.sonar.api.utils.Paging) SearchOptions(org.sonar.server.es.SearchOptions) IssueQuery(org.sonar.server.issue.IssueQuery)

Example 3 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class SearchAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectUuid = request.param(PARAM_PROJECT_ID);
    String projectKey = request.param(PARAM_PROJECT_KEY);
    List<String> fieldsToReturn = request.paramAsStrings(WebService.Param.FIELDS);
    SearchOptions searchOptions = new SearchOptions().setPage(request.mandatoryParamAsInt(WebService.Param.PAGE), request.mandatoryParamAsInt(WebService.Param.PAGE_SIZE));
    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto component = componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, PROJECT_ID_AND_KEY);
        checkPermissions(userSession, component);
        Long lastAnalysisDateMs = searchLastSnapshotDate(dbSession, component);
        List<CustomMeasureDto> customMeasures = searchCustomMeasures(dbSession, component, searchOptions);
        int nbCustomMeasures = countTotalOfCustomMeasures(dbSession, component);
        Map<String, UserDto> usersByLogin = usersByLogin(dbSession, customMeasures);
        Map<Integer, MetricDto> metricsById = metricsById(dbSession, customMeasures);
        writeResponse(response, customMeasures, nbCustomMeasures, component, metricsById, usersByLogin, lastAnalysisDateMs, searchOptions, fieldsToReturn);
    }
}
Also used : UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) SearchOptions(org.sonar.server.es.SearchOptions) DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) CustomMeasureDto(org.sonar.db.measure.custom.CustomMeasureDto)

Example 4 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class ProvisionedAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    SearchOptions options = new SearchOptions().setPage(request.mandatoryParamAsInt(Param.PAGE), request.mandatoryParamAsInt(Param.PAGE_SIZE));
    Set<String> desiredFields = desiredFields(request);
    String query = request.param(Param.TEXT_QUERY);
    try (DbSession dbSession = dbClient.openSession(false)) {
        OrganizationDto organization = support.getOrganization(dbSession, request.getParam(PARAM_ORGANIZATION).or(defaultOrganizationProvider.get()::getKey));
        userSession.checkPermission(PROVISION_PROJECTS, organization);
        RowBounds rowBounds = new RowBounds(options.getOffset(), options.getLimit());
        List<ComponentDto> projects = dbClient.componentDao().selectProvisioned(dbSession, organization.getUuid(), query, QUALIFIERS_FILTER, rowBounds);
        int nbOfProjects = dbClient.componentDao().countProvisioned(dbSession, organization.getUuid(), query, QUALIFIERS_FILTER);
        JsonWriter json = response.newJsonWriter().beginObject();
        writeProjects(projects, json, desiredFields);
        options.writeJson(json, nbOfProjects);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto) RowBounds(org.apache.ibatis.session.RowBounds) SearchOptions(org.sonar.server.es.SearchOptions) JsonWriter(org.sonar.api.utils.text.JsonWriter) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 5 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class SearchAction method doContextResponse.

protected void doContextResponse(DbSession dbSession, SearchWsRequest request, SearchResult result, SearchResponse.Builder response, RuleQuery query) {
    SearchOptions contextForResponse = loadCommonContext(request);
    writeRules(response, result, contextForResponse);
    if (contextForResponse.getFields().contains("actives")) {
        activeRuleCompleter.completeSearch(dbSession, query, result.rules, response);
    }
}
Also used : SearchOptions(org.sonar.server.es.SearchOptions)

Aggregations

SearchOptions (org.sonar.server.es.SearchOptions)214 Test (org.junit.Test)179 ComponentDto (org.sonar.db.component.ComponentDto)67 Facets (org.sonar.server.es.Facets)51 RuleQuery (org.sonar.server.rule.index.RuleQuery)38 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)33 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 RuleDto (org.sonar.db.rule.RuleDto)22 RuleKey (org.sonar.api.rule.RuleKey)11 DbSession (org.sonar.db.DbSession)11 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)9 NewRepository (org.sonar.api.server.rule.RulesDefinition.NewRepository)8 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)8 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)8 OrganizationDto (org.sonar.db.organization.OrganizationDto)7 IssueQuery (org.sonar.server.issue.IssueQuery)7 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 List (java.util.List)5 RuleParamDto (org.sonar.db.rule.RuleParamDto)5