Browse repositoryg Assets and components

Hello,

I want to check if a repository contains data. Meaning I want to browse the repository to find if we have 1 result. Retrieval of real asset/component is not needed.
When I execute below groovy script it complaints that property “browseService” is unknown.
Error “groovy.lang.MissingPropertyException: No such property: browseService”

The question is how can I instantiate the org.sonatype.nexus.repository.browse.BrowseService using groovy script. Can you please give an example.

Kind Regards,

Guy

import org.sonatype.nexus.security.user.UserManager;
import org.sonatype.nexus.internal.capability.DefaultCapabilityRegistry;
import org.sonatype.nexus.capability.CapabilityReferenceFilterBuilder;
import org.sonatype.nexus.internal.capability.DefaultCapabilityReference;
import org.sonatype.nexus.repository.browse.BrowseResult;
import org.sonatype.nexus.repository.browse.BrowseService;
import org.sonatype.nexus.repository.browse.QueryOptions;
import org.sonatype.nexus.repository.Repository;
import org.sonatype.nexus.repository.storage.Asset;

	 Repository repository = repository.getRepositoryManager().get("${repo.name}");
	 BrowseResult<Asset> assetBrowseResult = browseService.browseAssets(repository, new QueryOptions(null, "id", "asc", 0, 1));
	 if (assetBrowseResult != null && assetBrowseResult.getTotal() > 0) {
                ....
            }

Found another solution:
def repo = repository.getRepositoryManager().get(repoName);

StorageFacet storageFacet = repo.facet(StorageFacet);

def tx = storageFacet.txSupplier().get();

try {

    tx.begin()

    nbrAssets = tx.countAssets(Query.builder().build(), [repo]);

} catch (Exception e) {

    log.error("Could not retrieve assets from repo: {}", repoName, e)

    tx.rollback()

} finally {

    tx.close()

}

Rather than using groovy scripting, why don’t you use the component or asset search REST APIs? Just go to /#admin/system/api to find the search apis (ie /v1/search) and see how to use it.