private static SearchOptions loadCommonContext(SearchRequest request) { int pageSize = Integer.parseInt(request.getPs()); SearchOptions context = new SearchOptions().addFields(request.getF()); if (request.getFacets() != null) { context.addFacets(request.getFacets()); } if (pageSize < 1) { context.setPage(Integer.parseInt(request.getP()), 0).setLimit(MAX_LIMIT); } else { context.setPage(Integer.parseInt(request.getP()), pageSize); } return context; }
private static SearchOptions buildSearchOptions(Request request) { int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); return new SearchOptions().setPage(request.mandatoryParamAsInt(Param.PAGE), pageSize); }
private SearchOptions createSearchOptionsFromRequest(DbSession dbSession, SearchRequest request) { SearchOptions options = new SearchOptions(); options.setPage(request.getPage(), request.getPageSize()); List<String> facets = request.getFacets(); if (facets == null || facets.isEmpty()) { return options; } List<String> requestedFacets = new ArrayList<>(facets); if (isOnSonarCloud) { Optional<OrganizationDto> organizationDto = Optional.empty(); String organizationKey = request.getOrganization(); if (organizationKey != null) { organizationDto = dbClient.organizationDao().selectByKey(dbSession, organizationKey); } if (!organizationDto.isPresent() || !userSession.hasMembership(organizationDto.get())) { // In order to display the authors facet, the organization parameter must be set and the user // must be member of this organization requestedFacets.remove(PARAM_AUTHORS); } } options.addFacets(requestedFacets); return options; }
@Test public void with_zero_page_size() { SearchOptions options = new SearchOptions().setPage(1, 0); assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); assertThat(options.getOffset()).isEqualTo(0); assertThat(options.getPage()).isEqualTo(1); }
@Test public void max_page_size() { SearchOptions options = new SearchOptions().setPage(3, SearchOptions.MAX_LIMIT + 10); assertThat(options.getOffset()).isEqualTo(SearchOptions.MAX_LIMIT * 2); assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); }
@Test public void page_starts_at_one() { SearchOptions options = new SearchOptions().setPage(1, 10); assertThat(options.getLimit()).isEqualTo(10); assertThat(options.getOffset()).isEqualTo(0); assertThat(options.getPage()).isEqualTo(1); }
@Test public void page_shortcut_for_limit_and_offset() { SearchOptions options = new SearchOptions().setPage(3, 10); assertThat(options.getLimit()).isEqualTo(10); assertThat(options.getOffset()).isEqualTo(20); }
@Test public void page_must_be_strictly_positive() { try { new SearchOptions().setPage(0, 10); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Page must be greater or equal to 1 (got 0)"); } }
@Test public void use_max_limit_if_negative() { SearchOptions options = new SearchOptions().setPage(2, -1); assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); }
@Test public void fail_if_result_after_first_10_000() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Can return only the first 10000 results. 10500th result asked."); underTest.setPage(21, 500); } }
@Test public void paginate_results() { IntStream.rangeClosed(1, 9) .forEach(i -> index(newDoc(newPrivateProjectDto(ORG, "P" + i)))); SearchIdResult<String> result = underTest.search(new ProjectMeasuresQuery(), new SearchOptions().setPage(2, 3)); assertThat(result.getIds()).containsExactly("P4", "P5", "P6"); assertThat(result.getTotal()).isEqualTo(9); }
@Test public void paginate_results() { List<ComponentDto> projects = IntStream.range(0, 9) .mapToObj(i -> db.components().insertPrivateProject(p -> p.setName("project " + i))) .collect(Collectors.toList()); index(projects.toArray(new ComponentDto[0])); SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3)); assertThat(result.getIds()).containsExactlyInAnyOrder(projects.get(3).uuid(), projects.get(4).uuid(), projects.get(5).uuid()); }
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); ProjectMeasuresQueryValidator.validate(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); }
@Test public void writeJson() { SearchOptions options = new SearchOptions().setPage(3, 10); StringWriter json = new StringWriter(); JsonWriter jsonWriter = JsonWriter.of(json).beginObject(); options.writeJson(jsonWriter, 42L); jsonWriter.endObject().close(); JsonAssert.assertJson(json.toString()).isSimilarTo("{\"total\": 42, \"p\": 3, \"ps\": 10}"); }
private Users.SearchWsResponse doHandle(SearchRequest request) { SearchOptions options = new SearchOptions().setPage(request.getPage(), request.getPageSize()); List<String> fields = request.getPossibleFields(); SearchResult<UserDoc> result = userIndex.search(UserQuery.builder().setTextQuery(request.getQuery()).build(), options); try (DbSession dbSession = dbClient.openSession(false)) { List<String> logins = result.getDocs().stream().map(UserDoc::login).collect(toList()); Multimap<String, String> groupsByLogin = dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, logins); List<UserDto> users = dbClient.userDao().selectByOrderedLogins(dbSession, logins); Map<String, Integer> tokenCountsByLogin = dbClient.userTokenDao().countTokensByUsers(dbSession, users); Paging paging = forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal((int) result.getTotal()); return buildResponse(users, groupsByLogin, tokenCountsByLogin, fields, paging); } }
@Test public void paging() { ComponentDto project = newPrivateProjectDto(newOrganizationDto()); ComponentDto file = newFileDto(project, null); for (int i = 0; i < 12; i++) { indexIssues(newDoc("I" + i, file)); } IssueQuery.Builder query = IssueQuery.builder(); // There are 12 issues in total, with 10 issues per page, the page 2 should only contain 2 elements SearchResponse result = underTest.search(query.build(), new SearchOptions().setPage(2, 10)); assertThat(result.getHits().hits()).hasSize(2); assertThat(result.getHits().getTotalHits()).isEqualTo(12); result = underTest.search(IssueQuery.builder().build(), new SearchOptions().setOffset(0).setLimit(5)); assertThat(result.getHits().hits()).hasSize(5); assertThat(result.getHits().getTotalHits()).isEqualTo(12); result = underTest.search(IssueQuery.builder().build(), new SearchOptions().setOffset(2).setLimit(0)); assertThat(result.getHits().hits()).hasSize(10); assertThat(result.getHits().getTotalHits()).isEqualTo(12); }
@Override public void handle(Request request, Response response) throws Exception { int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); SearchOptions options = new SearchOptions() .setPage(page, pageSize); String query = defaultIfBlank(request.param(Param.TEXT_QUERY), ""); Set<String> fields = neededFields(request); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = groupWsSupport.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY)); userSession.checkLoggedIn().checkPermission(ADMINISTER, organization); GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession, organization.getUuid()); int limit = dbClient.groupDao().countByQuery(dbSession, organization.getUuid(), query); Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit); List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, organization.getUuid(), query, options.getOffset(), pageSize); List<Integer> groupIds = groups.stream().map(GroupDto::getId).collect(MoreCollectors.toList(groups.size())); Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupIds); writeProtobuf(buildResponse(groups, userCountByGroup, fields, paging, defaultGroup), request, response); } }
private SearchWsResponse doHandle(SearchRequest request) { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = getOrganization(dbSession, request); ComponentQuery esQuery = buildEsQuery(organization, request); SearchIdResult<String> results = componentIndex.search(esQuery, new SearchOptions().setPage(request.getPage(), request.getPageSize())); List<ComponentDto> components = dbClient.componentDao().selectByUuids(dbSession, results.getIds()); Map<String, String> projectKeysByUuids = searchProjectsKeysByUuids(dbSession, components); return buildResponse(components, organization, projectKeysByUuids, Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal((int) results.getTotal())); } }
@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> usersByUuid = usersByUuid(dbSession, customMeasures); Map<Integer, MetricDto> metricsById = metricsById(dbSession, customMeasures); writeResponse(response, customMeasures, nbCustomMeasures, component, metricsById, usersByUuid, lastAnalysisDateMs, searchOptions, fieldsToReturn); } }
@Override public void handle(Request request, Response response) throws Exception { SearchOptions searchOptions = new SearchOptions() .setPage(request.mandatoryParamAsInt(Param.PAGE), request.mandatoryParamAsInt(Param.PAGE_SIZE)); Boolean isCustom = request.paramAsBoolean(PARAM_IS_CUSTOM); try (DbSession dbSession = dbClient.openSession(false)) { List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions.getOffset(), searchOptions.getLimit()); int nbMetrics = dbClient.metricDao().countEnabled(dbSession, isCustom); JsonWriter json = response.newJsonWriter(); json.beginObject(); Set<String> desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS)); writeMetrics(json, metrics, desiredFields); searchOptions.writeJson(json, nbMetrics); json.endObject(); json.close(); } }