private static SearchOptions buildSearchOptions(SearchRequest request) { SearchOptions context = loadCommonContext(request); SearchOptions searchOptions = new SearchOptions() .setLimit(context.getLimit()) .setOffset(context.getOffset()); if (context.getFacets().contains(RuleIndex.FACET_OLD_DEFAULT)) { searchOptions.addFacets(DEFAULT_FACETS); } else { searchOptions.addFacets(context.getFacets()); } return searchOptions; }
@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); }
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; }
/** * Set offset and limit according to page approach. If pageSize is negative, then * {@link #MAX_LIMIT} is used. */ public SearchOptions setPage(int page, int pageSize) { checkArgument(page >= 1, "Page must be greater or equal to 1 (got " + page + ")"); int lastResultIndex = page * pageSize; checkArgument(lastResultIndex <= MAX_RETURNABLE_RESULTS, "Can return only the first %s results. %sth result asked.", MAX_RETURNABLE_RESULTS, lastResultIndex); setLimit(pageSize); setOffset((page * this.limit) - this.limit); return this; }
private Facets search(String additionalFacet) { return new Facets(underTest.search(newQueryBuilder().build(), new SearchOptions().addFacets(singletonList(additionalFacet))), system2.getDefaultTimeZone()); }
private static SearchOptions buildSearchOptions(Request request) { int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); return new SearchOptions().setPage(request.mandatoryParamAsInt(Param.PAGE), pageSize); }
@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); }
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; }
@CheckForNull public CompareToSonarWay getSonarWay(Request request, DbSession dbSession, OrganizationDto organization, QProfileDto profile) { if (!request.mandatoryParamAsBoolean(PARAM_COMPARE_TO_SONAR_WAY) || profile.isBuiltIn()) { return null; } QProfileDto sonarWay = Stream.of(SONAR_WAY, SONARQUBE_WAY) .map(name -> dbClient.qualityProfileDao().selectByNameAndLanguage(dbSession, organization, name, profile.getLanguage())) .filter(Objects::nonNull) .filter(QProfileDto::isBuiltIn) .findFirst() .orElse(null); if (sonarWay == null) { return null; } long missingRuleCount = ruleIndex.search( new RuleQuery().setQProfile(profile).setActivation(false).setCompareToQProfile(sonarWay), new SearchOptions().setLimit(1)) .getTotal(); return CompareToSonarWay.newBuilder() .setProfile(sonarWay.getKee()) .setProfileName(sonarWay.getName()) .setMissingRuleCount(missingRuleCount) .build(); }
@Test public void max_limit() { SearchOptions options = new SearchOptions().setLimit(42); assertThat(options.getLimit()).isEqualTo(42); options.setLimit(SearchOptions.MAX_LIMIT + 10); assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); }
IssueQuery query = issueQueryFactory.create(request); Set<String> facetsRequiringProjectOrOrganizationParameter = options.getFacets().stream() .filter(FACETS_REQUIRING_PROJECT_OR_ORGANIZATION::contains) .collect(toSet()); collectRequestParams(collector, request); Facets facets = new Facets(result, system2.getDefaultTimeZone()); if (!options.getFacets().isEmpty()) { Paging paging = forPageIndex(options.getPage()).withPageSize(options.getLimit()).andTotal((int) result.getHits().getTotalHits()); return searchResponseFormat.formatSearch(additionalFields, data, paging, facets);
@Test public void use_max_limit_if_negative() { SearchOptions options = new SearchOptions().setPage(2, -1); assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); }
/** * Optimization - do not send ES request to all shards when scope is restricted * to a set of projects. Because project UUID is used for routing, the request * can be sent to only the shards containing the specified projects. * Note that sticky facets may involve all projects, so this optimization must be * disabled when facets are enabled. */ private static void configureRouting(IssueQuery query, SearchOptions options, SearchRequestBuilder requestBuilder) { Collection<String> uuids = query.projectUuids(); if (!uuids.isEmpty() && options.getFacets().isEmpty()) { requestBuilder.setRouting(uuids.toArray(new String[uuids.size()])); } }
@Test public void paging() { createRule(); createRule(); createRule(); index(); // from 0 to 1 included SearchOptions options = new SearchOptions(); options.setOffset(0).setLimit(2); SearchIdResult results = underTest.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 = underTest.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 = underTest.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 = underTest.search(new RuleQuery(), options); assertThat(results.getTotal()).isEqualTo(3); assertThat(results.getIds()).hasSize(1); }
@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); } }
@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}"); }
/** * Execute the search request and return the document ids of results. */ private List<String> searchAndReturnKeys(IssueQuery.Builder query) { return Arrays.stream(underTest.search(query.build(), new SearchOptions()).getHits().getHits()) .map(SearchHit::getId) .collect(Collectors.toList()); }