Search in sources :

Example 1 with SearchResult

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);
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Pageable(io.gravitee.rest.api.model.common.Pageable) CustomWhitespaceAnalyzer(io.gravitee.rest.api.service.impl.search.lucene.analyzer.CustomWhitespaceAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) DirectoryReader(org.apache.lucene.index.DirectoryReader) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.apache.lucene.document.Document) IndexWriter(org.apache.lucene.index.IndexWriter) DocumentSearcher(io.gravitee.rest.api.service.impl.search.lucene.DocumentSearcher) org.apache.lucene.search(org.apache.lucene.search) SearchResult(io.gravitee.rest.api.service.impl.search.SearchResult) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ArrayList(java.util.ArrayList) SearchResult(io.gravitee.rest.api.service.impl.search.SearchResult) IOException(java.io.IOException)

Example 2 with SearchResult

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);
    }
}
Also used : ApiCriteria(io.gravitee.repository.management.api.search.ApiCriteria) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) QueryBuilder(io.gravitee.rest.api.service.search.query.QueryBuilder) SearchResult(io.gravitee.rest.api.service.impl.search.SearchResult) Page(io.gravitee.common.data.domain.Page) Api(io.gravitee.repository.management.model.Api)

Example 3 with SearchResult

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());
}
Also used : SearchResult(io.gravitee.rest.api.service.impl.search.SearchResult)

Example 4 with SearchResult

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);
}
Also used : UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) SearchResult(io.gravitee.rest.api.service.impl.search.SearchResult) Page(io.gravitee.common.data.domain.Page) MetadataPage(io.gravitee.common.data.domain.MetadataPage) UuidString(io.gravitee.rest.api.service.common.UuidString)

Aggregations

SearchResult (io.gravitee.rest.api.service.impl.search.SearchResult)4 Page (io.gravitee.common.data.domain.Page)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 MetadataPage (io.gravitee.common.data.domain.MetadataPage)1 ApiCriteria (io.gravitee.repository.management.api.search.ApiCriteria)1 UserCriteria (io.gravitee.repository.management.api.search.UserCriteria)1 Api (io.gravitee.repository.management.model.Api)1 Pageable (io.gravitee.rest.api.model.common.Pageable)1 EmailNotificationBuilder (io.gravitee.rest.api.service.builder.EmailNotificationBuilder)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 DocumentSearcher (io.gravitee.rest.api.service.impl.search.lucene.DocumentSearcher)1 CustomWhitespaceAnalyzer (io.gravitee.rest.api.service.impl.search.lucene.analyzer.CustomWhitespaceAnalyzer)1 NotificationParamsBuilder (io.gravitee.rest.api.service.notification.NotificationParamsBuilder)1 QueryBuilder (io.gravitee.rest.api.service.search.query.QueryBuilder)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Analyzer (org.apache.lucene.analysis.Analyzer)1