Search in sources :

Example 6 with SearchResultLimits

use of org.apache.archiva.indexer.search.SearchResultLimits in project archiva by apache.

the class MavenRepositorySearchTest method testQuickSearchWithPagination.

@Test
public void testQuickSearchWithPagination() throws Exception {
    createSimpleIndex(true);
    List<String> selectedRepos = new ArrayList<>();
    selectedRepos.add(TEST_REPO_1);
    // page 1
    SearchResultLimits limits = new SearchResultLimits(0);
    limits.setPageSize(1);
    when(archivaConfig.getDefaultLocale()).thenReturn(Locale.getDefault());
    when(archivaConfig.getConfiguration()).thenReturn(config);
    SearchResults results = search.search("user", selectedRepos, "org", limits, Collections.emptyList());
    assertNotNull(results);
    assertEquals(1, results.getHits().size());
    assertEquals("total hits not 9 for page1 " + results, 9, results.getTotalHits());
    assertEquals("returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount());
    assertEquals(limits, results.getLimits());
    reset(archivaConfig);
    // page 2
    limits = new SearchResultLimits(1);
    limits.setPageSize(1);
    when(archivaConfig.getDefaultLocale()).thenReturn(Locale.getDefault());
    when(archivaConfig.getConfiguration()).thenReturn(config);
    results = search.search("user", selectedRepos, "org", limits, null);
    assertNotNull(results);
    assertEquals("hits not 1", 1, results.getHits().size());
    assertEquals("total hits not 9 for page 2 " + results, 9, results.getTotalHits());
    assertEquals("returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount());
    assertEquals(limits, results.getLimits());
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) ArrayList(java.util.ArrayList) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Example 7 with SearchResultLimits

use of org.apache.archiva.indexer.search.SearchResultLimits in project archiva by apache.

the class MavenRepositorySearchTest method testAdvancedSearchWithPagination.

@Test
public void testAdvancedSearchWithPagination() throws Exception {
    createIndexContainingMoreArtifacts(false);
    List<String> selectedRepos = new ArrayList<>();
    selectedRepos.add(TEST_REPO_1);
    SearchFields searchFields = new SearchFields();
    searchFields.setGroupId("org.apache.archiva");
    searchFields.setRepositories(selectedRepos);
    // page 1
    SearchResultLimits limits = new SearchResultLimits(0);
    limits.setPageSize(1);
    when(archivaConfig.getDefaultLocale()).thenReturn(Locale.getDefault());
    when(archivaConfig.getConfiguration()).thenReturn(config);
    SearchResults results = search.search("user", searchFields, limits);
    assertNotNull(results);
    assertEquals(4, results.getTotalHits());
    assertEquals(1, results.getHits().size());
    // page 2
    reset(archivaConfig);
    limits = new SearchResultLimits(1);
    limits.setPageSize(1);
    when(archivaConfig.getDefaultLocale()).thenReturn(Locale.getDefault());
    when(archivaConfig.getConfiguration()).thenReturn(config);
    results = search.search("user", searchFields, limits);
    assertNotNull(results);
    assertEquals(4, results.getTotalHits());
    assertEquals(1, results.getHits().size());
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) SearchFields(org.apache.archiva.indexer.search.SearchFields) ArrayList(java.util.ArrayList) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Example 8 with SearchResultLimits

use of org.apache.archiva.indexer.search.SearchResultLimits in project archiva by apache.

the class MavenRepositorySearchTest method nolimitedResult.

@Test
public void nolimitedResult() throws Exception {
    Path repo = Paths.get("target/repo-release-index-test/repo-release");
    try {
        Path indexDirectory = repo.resolve(".indexer");
        Path zipFile = Paths.get(Thread.currentThread().getContextClassLoader().getResource("repo-release.zip").toURI());
        FileUtils.unzip(zipFile, repo.getParent());
        // IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString(), "-delete-prior-commits"});
        createIndex(REPO_RELEASE, Collections.emptyList(), false, indexDirectory, false);
        // indexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo.toFile(), indexDirectory.toFile(),
        // repo.toUri().toURL().toExternalForm(),
        // indexDirectory.toUri().toURL().toString(), indexCreators );
        SearchResultLimits limits = new SearchResultLimits(SearchResultLimits.ALL_PAGES);
        limits.setPageSize(300);
        // when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
        when(archivaConfig.getConfiguration()).thenReturn(config);
        SearchResults searchResults = // 
        search.search(// 
        null, // 
        Arrays.asList(REPO_RELEASE), // 
        "org.example", // 
        limits, Collections.emptyList());
        log.info("results: {}", searchResults.getHits().size());
        assertEquals(255, searchResults.getHits().size());
        SearchFields searchFields = new SearchFields();
        searchFields.setGroupId("org.example");
        searchFields.setRepositories(Arrays.asList(REPO_RELEASE));
        searchResults = search.search(null, searchFields, limits);
        log.info("results: {}", searchResults.getHits().size());
        assertEquals(255, searchResults.getHits().size());
    } finally {
        FileUtils.deleteQuietly(repo);
    }
}
Also used : Path(java.nio.file.Path) SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) SearchFields(org.apache.archiva.indexer.search.SearchFields) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Example 9 with SearchResultLimits

use of org.apache.archiva.indexer.search.SearchResultLimits in project archiva by apache.

the class DefaultSearchService method searchArtifacts.

@Override
public List<Artifact> searchArtifacts(SearchRequest searchRequest) throws ArchivaRestServiceException {
    if (searchRequest == null) {
        return Collections.emptyList();
    }
    SearchFields searchField = getModelMapper().map(searchRequest, SearchFields.class);
    SearchResultLimits limits = new SearchResultLimits(0);
    limits.setPageSize(searchRequest.getPageSize());
    // if no repos set we use ones available for the user
    if (searchField.getRepositories() == null || searchField.getRepositories().isEmpty()) {
        searchField.setRepositories(getObservableRepos());
    }
    try {
        SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, limits);
        return getArtifacts(searchResults);
    } catch (RepositorySearchException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) SearchFields(org.apache.archiva.indexer.search.SearchFields) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) SearchResults(org.apache.archiva.indexer.search.SearchResults)

Example 10 with SearchResultLimits

use of org.apache.archiva.indexer.search.SearchResultLimits in project archiva by apache.

the class MavenRepositorySearch method paginate.

protected SearchResults paginate(SearchResults results) {
    SearchResultLimits limits = results.getLimits();
    SearchResults paginated = new SearchResults();
    // ( limits.getPageSize() * ( Math.max( 1, limits.getSelectedPage() ) ) );
    int fetchCount = limits.getPageSize();
    int offset = (limits.getSelectedPage() * limits.getPageSize());
    if (fetchCount > results.getTotalHits()) {
        fetchCount = results.getTotalHits();
    }
    // Goto offset.
    if (offset < results.getTotalHits()) {
        // only process if the offset is within the hit count.
        for (int i = 0; i < fetchCount; i++) {
            // Stop fetching if we are past the total # of available hits.
            if (offset + i >= results.getHits().size()) {
                break;
            }
            SearchResultHit hit = results.getHits().get((offset + i));
            if (hit != null) {
                String id = SearchUtil.getHitId(hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(), hit.getPackaging());
                paginated.addHit(id, hit);
            } else {
                break;
            }
        }
    }
    paginated.setTotalHits(results.getTotalHits());
    paginated.setReturnedHitsCount(paginated.getHits().size());
    paginated.setTotalHitsMapSize(results.getTotalHitsMapSize());
    paginated.setLimits(limits);
    return paginated;
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) SearchResultHit(org.apache.archiva.indexer.search.SearchResultHit) SearchResults(org.apache.archiva.indexer.search.SearchResults)

Aggregations

SearchResultLimits (org.apache.archiva.indexer.search.SearchResultLimits)16 SearchResults (org.apache.archiva.indexer.search.SearchResults)16 Test (org.junit.Test)12 SearchFields (org.apache.archiva.indexer.search.SearchFields)5 ArrayList (java.util.ArrayList)4 MavenRepositorySearch (org.apache.archiva.maven.indexer.search.MavenRepositorySearch)3 Path (java.nio.file.Path)2 RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)2 SearchResultHit (org.apache.archiva.indexer.search.SearchResultHit)2 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)2