Java Code Examples for org.apache.maven.index.context.IndexingContext#acquireIndexSearcher()

The following examples show how to use org.apache.maven.index.context.IndexingContext#acquireIndexSearcher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassDependencyIndexCreator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static void search(String className, Indexer indexer, Collection<IndexingContext> contexts, List<? super ClassUsage> results) throws IOException {
    String searchString = crc32base64(className.replace('.', '/'));
    Query refClassQuery = indexer.constructQuery(ClassDependencyIndexCreator.FLD_NB_DEPENDENCY_CLASS.getOntology(), new StringSearchExpression(searchString));
    TopScoreDocCollector collector = TopScoreDocCollector.create(NexusRepositoryIndexerImpl.MAX_RESULT_COUNT, null);
    for (IndexingContext context : contexts) {
        IndexSearcher searcher = context.acquireIndexSearcher();
        try {
    searcher.search(refClassQuery, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    LOG.log(Level.FINER, "for {0} ~ {1} found {2} hits", new Object[] {className, searchString, hits.length});
    for (ScoreDoc hit : hits) {
        int docId = hit.doc;
        Document d = searcher.doc(docId);
        String fldValue = d.get(ClassDependencyIndexCreator.NB_DEPENDENCY_CLASSES);
        LOG.log(Level.FINER, "{0} uses: {1}", new Object[] {className, fldValue});
        Set<String> refClasses = parseField(searchString, fldValue, d.get(ArtifactInfo.NAMES));
        if (!refClasses.isEmpty()) {
            ArtifactInfo ai = IndexUtils.constructArtifactInfo(d, context);
            if (ai != null) {
                ai.setRepository(context.getRepositoryId());
                List<NBVersionInfo> version = NexusRepositoryIndexerImpl.convertToNBVersionInfo(Collections.singleton(ai));
                if (!version.isEmpty()) {
                    results.add(new ClassUsage(version.get(0), refClasses));
                }
            }
        }
    }
    } finally {
        context.releaseIndexSearcher(searcher);
    }
    }
}
 
Example 2
Source File: ArchivaIndexingTaskExecutorTest.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateArtifactInIndex()
    throws Exception
{
    Path basePath = repo.getRoot().getFilePath();
    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 );
    indexingExecutor.executeTask( task );

    BooleanQuery.Builder qb = new BooleanQuery.Builder();
    qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
           BooleanClause.Occur.SHOULD );
    qb.add(
        indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
        BooleanClause.Occur.SHOULD );

    IndexingContext ctx = getIndexingContext();

    IndexSearcher searcher = ctx.acquireIndexSearcher();
    TopDocs topDocs = searcher.search( qb.build(), 10 );

    //searcher.close();
    ctx.releaseIndexSearcher( searcher );

    assertTrue( Files.exists(basePath.resolve(".indexer" )) );
    assertTrue( Files.exists(basePath.resolve(".index" )) );

    // should only return 1 hit!
    assertEquals( 1, topDocs.totalHits );
}