Search in sources :

Example 1 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)

Example 2 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");
    FileUtils.deleteDirectory(repo.toFile());
    Path indexDirectory = repo.resolve(".index");
    FileUtils.copyDirectoryStructure(Paths.get("src/test/repo-release").toFile(), repo.toFile());
    IndexUpgrader.main(new String[] { indexDirectory.toAbsolutePath().toString() });
    createIndex(REPO_RELEASE, Collections.emptyList(), false, indexDirectory);
    // 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);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    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());
    archivaConfigControl.verify();
}
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 3 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);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    SearchResults results = search.search("user", selectedRepos, "org", limits, Collections.emptyList());
    archivaConfigControl.verify();
    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());
    archivaConfigControl.reset();
    // page 2
    limits = new SearchResultLimits(1);
    limits.setPageSize(1);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    results = search.search("user", selectedRepos, "org", limits, null);
    archivaConfigControl.verify();
    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 4 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);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    SearchResults results = search.search("user", searchFields, limits);
    archivaConfigControl.verify();
    assertNotNull(results);
    assertEquals(4, results.getTotalHits());
    assertEquals(1, results.getHits().size());
    // page 2
    archivaConfigControl.reset();
    limits = new SearchResultLimits(1);
    limits.setPageSize(1);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    results = search.search("user", searchFields, limits);
    archivaConfigControl.verify();
    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 5 with SearchResultLimits

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

the class MavenRepositorySearchPaginateTest method nonPaginatedResult.

@Test
public void nonPaginatedResult() throws Exception {
    MavenRepositorySearch search = new MavenRepositorySearch();
    SearchResults searchResults = build(10, new SearchResultLimits(0));
    searchResults = search.paginate(searchResults);
    assertEquals(10, searchResults.getReturnedHitsCount());
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Aggregations

SearchResultLimits (org.apache.archiva.indexer.search.SearchResultLimits)9 SearchResults (org.apache.archiva.indexer.search.SearchResults)9 Test (org.junit.Test)6 SearchFields (org.apache.archiva.indexer.search.SearchFields)3 ArrayList (java.util.ArrayList)2 RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)2 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)2 Path (java.nio.file.Path)1 SearchResultHit (org.apache.archiva.indexer.search.SearchResultHit)1