use of io.gravitee.rest.api.service.impl.search.SearchResult 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.service.impl.search.SearchResult in project gravitee-management-rest-api by gravitee-io.
the class ApiServiceImpl method search.
@Override
public Page<ApiEntity> search(String query, Map<String, Object> filters, Sortable sortable, Pageable pageable) {
try {
LOGGER.debug("Search paged APIs by {}", query);
Query<ApiEntity> apiQuery = QueryBuilder.create(ApiEntity.class).setQuery(query).setFilters(filters).build();
SearchResult matchApis = searchEngineService.search(apiQuery);
if (matchApis.getDocuments().isEmpty()) {
return new Page<>(emptyList(), 0, 0, 0);
}
final ApiCriteria apiCriteria = new ApiCriteria.Builder().ids(matchApis.getDocuments().toArray(new String[0])).build();
final Page<Api> apiPage = sortAndPaginate(apiRepository.search(apiCriteria), sortable, pageable);
// merge all apis
final List<ApiEntity> apis = convert(apiPage.getContent());
return new Page<>(apis, apiPage.getPageNumber(), (int) apiPage.getPageElements(), apiPage.getTotalElements());
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to search paged apis", ex);
throw new TechnicalManagementException("An error occurs while trying to search paged apis", ex);
}
}
use of io.gravitee.rest.api.service.impl.search.SearchResult in project gravitee-management-rest-api by gravitee-io.
the class ApiServiceImpl method search.
@Override
public Collection<ApiEntity> search(String query, Map<String, Object> filters) {
Query<ApiEntity> apiQuery = QueryBuilder.create(ApiEntity.class).setQuery(query).setFilters(filters).build();
SearchResult matchApis = searchEngineService.search(apiQuery);
return matchApis.getDocuments().stream().map(this::findById).collect(toList());
}
use of io.gravitee.rest.api.service.impl.search.SearchResult in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method search.
@Override
public Page<UserEntity> search(String query, Pageable pageable) {
LOGGER.debug("search users");
if (query == null || query.isEmpty()) {
return search(new UserCriteria.Builder().statuses(UserStatus.ACTIVE, UserStatus.PENDING, UserStatus.REJECTED).build(), pageable);
}
// UserDocumentTransformation remove domain from email address for security reasons
// remove it during search phase to provide results
String sanitizedQuery = query.indexOf('@') > 0 ? query.substring(0, query.indexOf('@')) : query;
Query<UserEntity> userQuery = QueryBuilder.create(UserEntity.class).setQuery(sanitizedQuery).setPage(pageable).build();
SearchResult results = searchEngineService.search(userQuery);
if (results.hasResults()) {
List<UserEntity> users = new ArrayList<>((findByIds(results.getDocuments())));
populateUserFlags(users);
return new Page<>(users, pageable.getPageNumber(), pageable.getPageSize(), results.getHits());
}
return new Page<>(Collections.emptyList(), 1, 0, 0);
}
Aggregations