Search in sources :

Example 1 with BooleanQuery

use of org.apache.maven.index_shaded.lucene.search.BooleanQuery 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 2 with BooleanQuery

use of org.apache.maven.index_shaded.lucene.search.BooleanQuery in project archiva by apache.

the class ArchivaIndexingTaskExecutorTest method testAddArtifactToIndex.

@Test
public void testAddArtifactToIndex() throws Exception {
    Path basePath = repo.getLocalPath();
    Path artifactFile = basePath.resolve("org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar");
    ArtifactIndexingTask task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.ADD, repo.getIndexingContext());
    indexingExecutor.executeTask(task);
    task = new ArtifactIndexingTask(repo, null, ArtifactIndexingTask.Action.FINISH, repo.getIndexingContext());
    indexingExecutor.executeTask(task);
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    queryBuilder.add(indexer.constructQuery(MAVEN.GROUP_ID, new StringSearchExpression("org.apache.archiva")), BooleanClause.Occur.SHOULD);
    queryBuilder.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new StringSearchExpression("archiva-index-methods-jar-test")), BooleanClause.Occur.SHOULD);
    BooleanQuery q = queryBuilder.build();
    FlatSearchRequest request = new FlatSearchRequest(q, getIndexingContext());
    FlatSearchResponse response = indexer.searchFlat(request);
    assertTrue(Files.exists(basePath.resolve(".indexer")));
    assertTrue(Files.exists(basePath.resolve(".index")));
    assertEquals(1, response.getTotalHitsCount());
    Set<ArtifactInfo> results = response.getResults();
    ArtifactInfo artifactInfo = results.iterator().next();
    assertEquals("org.apache.archiva", artifactInfo.getGroupId());
    assertEquals("archiva-index-methods-jar-test", artifactInfo.getArtifactId());
    assertEquals("test-repo", artifactInfo.getRepository());
}
Also used : Path(java.nio.file.Path) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) ArtifactInfo(org.apache.maven.index.ArtifactInfo) FlatSearchResponse(org.apache.maven.index.FlatSearchResponse) FlatSearchRequest(org.apache.maven.index.FlatSearchRequest) StringSearchExpression(org.apache.maven.index.expr.StringSearchExpression) Test(org.junit.Test)

Aggregations

BooleanQuery (org.apache.maven.index_shaded.lucene.search.BooleanQuery)2 Path (java.nio.file.Path)1 RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)1 SearchResults (org.apache.archiva.indexer.search.SearchResults)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 StringSearchExpression (org.apache.maven.index.expr.StringSearchExpression)1 UserInputSearchExpression (org.apache.maven.index.expr.UserInputSearchExpression)1 BooleanClause (org.apache.maven.index_shaded.lucene.search.BooleanClause)1 Test (org.junit.Test)1