Search in sources :

Example 1 with SearchResults

use of org.apache.archiva.indexer.search.SearchResults 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 SearchResults

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

the class MavenRepositorySearch method search.

/**
 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
 */
@SuppressWarnings("deprecation")
@Override
public SearchResults search(String principal, SearchFields searchFields, SearchResultLimits limits) throws RepositorySearchException {
    if (searchFields.getRepositories() == null) {
        throw new RepositorySearchException("Repositories cannot be null.");
    }
    List<String> indexingContextIds = addIndexingContexts(searchFields.getRepositories());
    // olamy: IMHO doesn't make sense
    if (!searchFields.getRepositories().isEmpty() && (indexingContextIds == null || indexingContextIds.isEmpty())) {
        return new SearchResults();
    }
    BooleanQuery.Builder qb = new BooleanQuery.Builder();
    if (StringUtils.isNotBlank(searchFields.getGroupId())) {
        qb.add(indexer.constructQuery(MAVEN.GROUP_ID, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getGroupId()) : new UserInputSearchExpression(searchFields.getGroupId())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getArtifactId())) {
        qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getArtifactId()) : new UserInputSearchExpression(searchFields.getArtifactId())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getVersion())) {
        qb.add(indexer.constructQuery(MAVEN.VERSION, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getVersion()) : new SourcedSearchExpression(searchFields.getVersion())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getPackaging())) {
        qb.add(indexer.constructQuery(MAVEN.PACKAGING, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getPackaging()) : new UserInputSearchExpression(searchFields.getPackaging())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getClassName())) {
        qb.add(indexer.constructQuery(MAVEN.CLASSNAMES, new UserInputSearchExpression(searchFields.getClassName())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleSymbolicName())) {
        qb.add(indexer.constructQuery(OSGI.SYMBOLIC_NAME, new UserInputSearchExpression(searchFields.getBundleSymbolicName())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleVersion())) {
        qb.add(indexer.constructQuery(OSGI.VERSION, new UserInputSearchExpression(searchFields.getBundleVersion())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleExportPackage())) {
        qb.add(indexer.constructQuery(OSGI.EXPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleExportPackage())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleExportService())) {
        qb.add(indexer.constructQuery(OSGI.EXPORT_SERVICE, new UserInputSearchExpression(searchFields.getBundleExportService())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleImportPackage())) {
        qb.add(indexer.constructQuery(OSGI.IMPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleImportPackage())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleName())) {
        qb.add(indexer.constructQuery(OSGI.NAME, new UserInputSearchExpression(searchFields.getBundleName())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleImportPackage())) {
        qb.add(indexer.constructQuery(OSGI.IMPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleImportPackage())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleRequireBundle())) {
        qb.add(indexer.constructQuery(OSGI.REQUIRE_BUNDLE, new UserInputSearchExpression(searchFields.getBundleRequireBundle())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getClassifier())) {
        qb.add(indexer.constructQuery(MAVEN.CLASSIFIER, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getClassifier()) : new UserInputSearchExpression(searchFields.getClassifier())), Occur.MUST);
    } else if (searchFields.isExactSearch()) {
    // TODO improvement in case of exact search and no classifier we must query for classifier with null value
    // currently it's done in DefaultSearchService with some filtering
    }
    BooleanQuery qu = qb.build();
    if (qu.clauses() == null || qu.clauses().size() <= 0) {
        throw new RepositorySearchException("No search fields set.");
    }
    if (qu.clauses() != null) {
        log.debug("CLAUSES ", qu.clauses());
        for (BooleanClause cl : qu.clauses()) {
            log.debug("Clause ", cl);
        }
    }
    return search(limits, qu, indexingContextIds, Collections.<ArtifactInfoFilter>emptyList(), searchFields.getRepositories(), searchFields.isIncludePomArtifacts());
}
Also used : BooleanClause(org.apache.maven.index_shaded.lucene.search.BooleanClause) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) UserInputSearchExpression(org.apache.maven.index.expr.UserInputSearchExpression) SourcedSearchExpression(org.apache.maven.index.expr.SourcedSearchExpression) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) SearchResults(org.apache.archiva.indexer.search.SearchResults)

Example 3 with SearchResults

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

the class MavenRepositorySearchOSGITest method searchFelixWithSymbolicName.

@Test
public void searchFelixWithSymbolicName() throws Exception {
    createIndex(TEST_REPO_1, Collections.<Path>emptyList(), true);
    List<String> selectedRepos = Arrays.asList(TEST_REPO_1);
    // search artifactId
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    SearchFields searchFields = new SearchFields();
    searchFields.setBundleSymbolicName("org.apache.felix.bundlerepository");
    searchFields.setBundleVersion("1.6.6");
    searchFields.setRepositories(selectedRepos);
    SearchResults results = search.search("user", searchFields, null);
    archivaConfigControl.verify();
    assertNotNull(results);
    assertEquals(1, results.getTotalHits());
    SearchResultHit hit = results.getHits().get(0);
    assertEquals("org.apache.felix", hit.getGroupId());
    assertEquals("org.apache.felix.bundlerepository", hit.getArtifactId());
    assertEquals("1.6.6", hit.getVersions().get(0));
    assertEquals("org.apache.felix.bundlerepository;uses:=\"org.osgi.framework\";version=\"2.0\"", hit.getBundleExportPackage());
    assertEquals("org.apache.felix.bundlerepository.RepositoryAdmin,org.osgi.service.obr.RepositoryAdmin", hit.getBundleExportService());
    assertEquals("org.apache.felix.bundlerepository", hit.getBundleSymbolicName());
    assertEquals("1.6.6", hit.getBundleVersion());
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) SearchResultHit(org.apache.archiva.indexer.search.SearchResultHit) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Example 4 with SearchResults

use of org.apache.archiva.indexer.search.SearchResults 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 5 with SearchResults

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

the class MavenRepositorySearchTest method testAdvancedSearchJarArtifacts.

@Test
public void testAdvancedSearchJarArtifacts() throws Exception {
    createIndexContainingMoreArtifacts(true);
    List<String> selectedRepos = new ArrayList<>();
    selectedRepos.add(TEST_REPO_1);
    SearchFields searchFields = new SearchFields();
    searchFields.setPackaging("jar");
    searchFields.setRepositories(selectedRepos);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    SearchResults results = search.search("user", searchFields, null);
    archivaConfigControl.verify();
    assertNotNull(results);
    assertEquals("not 8 but " + results.getTotalHits() + ":" + niceDisplay(results), 8, results.getTotalHits());
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) ArrayList(java.util.ArrayList) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Aggregations

SearchResults (org.apache.archiva.indexer.search.SearchResults)35 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)17 SearchFields (org.apache.archiva.indexer.search.SearchFields)16 SearchResultHit (org.apache.archiva.indexer.search.SearchResultHit)13 SearchResultLimits (org.apache.archiva.indexer.search.SearchResultLimits)9 RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)6 Path (java.nio.file.Path)4 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)4 IOException (java.io.IOException)1 URI (java.net.URI)1 TreeMap (java.util.TreeMap)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 Artifact (org.apache.archiva.maven2.model.Artifact)1 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)1 ArtifactInfo (org.apache.maven.index.ArtifactInfo)1 FlatSearchRequest (org.apache.maven.index.FlatSearchRequest)1 FlatSearchResponse (org.apache.maven.index.FlatSearchResponse)1 SourcedSearchExpression (org.apache.maven.index.expr.SourcedSearchExpression)1 UserInputSearchExpression (org.apache.maven.index.expr.UserInputSearchExpression)1