use of io.gravitee.rest.api.model.common.Pageable in project gravitee-management-rest-api by gravitee-io.
the class AbstractDocumentSearcher method search.
protected SearchResult search(Query query, Pageable pageable) throws TechnicalException {
logger.debug("Searching for: {}", query.toString());
try {
IndexSearcher searcher = getIndexSearcher();
TopDocs topDocs;
if (pageable != null) {
// TODO: How to find the accurate numhits ?
TopScoreDocCollector collector = TopScoreDocCollector.create(1000);
searcher.search(query, collector);
topDocs = collector.topDocs((pageable.getPageNumber() - 1) * pageable.getPageSize(), pageable.getPageSize());
} else {
topDocs = searcher.search(query, Integer.MAX_VALUE);
}
final ScoreDoc[] hits = topDocs.scoreDocs;
final List<String> results = new ArrayList<>();
final List<Integer> orderdedDocsId = Arrays.stream(topDocs.scoreDocs).sorted((doc1, doc2) -> Float.compare(doc2.score, doc1.score)).map(doc -> doc.doc).collect(Collectors.toList());
logger.debug("Found {} total matching documents", topDocs.totalHits);
for (Integer docId : orderdedDocsId) {
results.add(getReference(searcher.doc(docId)));
}
return new SearchResult(results.stream().distinct().collect(Collectors.toList()), topDocs.totalHits);
} catch (IOException ioe) {
logger.error("An error occurs while getting documents from search result", ioe);
throw new TechnicalException("An error occurs while getting documents from search result", ioe);
}
}
use of io.gravitee.rest.api.model.common.Pageable in project gravitee-management-rest-api by gravitee-io.
the class DefaultPageRevisionUpgrader method upgrade.
@Override
public boolean upgrade() {
if (hasNoRevisions()) {
logger.info("No page revisions found. Create a default revision based on pages.");
final int pageSize = 100;
int pageNumber = 0;
Page<PageEntity> pagesSubSet = null;
do {
Pageable pageable = new PageableImpl(pageNumber, pageSize);
pagesSubSet = pageService.findAll(pageable);
if (!pagesSubSet.getContent().isEmpty()) {
pagesSubSet.getContent().stream().filter(entity -> pageService.shouldHaveRevision(entity.getType())).forEach(entity -> pageRevisionService.create(convert(entity)));
++pageNumber;
}
} while (!pagesSubSet.getContent().isEmpty());
}
return true;
}
use of io.gravitee.rest.api.model.common.Pageable in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method search.
@Override
public Page<UserEntity> search(UserCriteria criteria, Pageable pageable) {
try {
LOGGER.debug("search users");
UserCriteria.Builder builder = new UserCriteria.Builder().organizationId(GraviteeContext.getCurrentOrganization()).statuses(criteria.getStatuses());
if (criteria.hasNoStatus()) {
builder.noStatus();
}
UserCriteria newCriteria = builder.build();
Page<User> users = userRepository.search(newCriteria, new PageableBuilder().pageNumber(pageable.getPageNumber() - 1).pageSize(pageable.getPageSize()).build());
List<UserEntity> entities = users.getContent().stream().map(u -> convert(u, false)).collect(toList());
populateUserFlags(entities);
return new Page<>(entities, users.getPageNumber() + 1, (int) users.getPageElements(), users.getTotalElements());
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to search users", ex);
throw new TechnicalManagementException("An error occurs while trying to search users", ex);
}
}
Aggregations