Example usage for org.apache.lucene.search IndexSearcher getIndexReader

List of usage examples for org.apache.lucene.search IndexSearcher getIndexReader

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher getIndexReader.

Prototype

public IndexReader getIndexReader() 

Source Link

Document

Return the IndexReader this searches.

Usage

From source file:org.metaservice.core.maven.MavenIndexCrawler.java

License:Apache License

public void perform() throws IOException, ComponentLookupException, InvalidVersionSpecificationException {
    // Files where local cache is (if any) and Lucene Index should be located
    File centralLocalCache = new File("target/central-cache");
    File centralIndexDir = new File("target/central-index");

    // Creators we want to use (search for fields it defines)
    List<IndexCreator> indexers = new ArrayList<>();
    indexers.add(plexusContainer.lookup(IndexCreator.class, "min"));
    indexers.add(plexusContainer.lookup(IndexCreator.class, "jarContent"));
    indexers.add(plexusContainer.lookup(IndexCreator.class, "maven-plugin"));

    // Create context for central repository index
    centralContext = indexer.createIndexingContext("central-context", "central", centralLocalCache,
            centralIndexDir, "http://repo1.maven.org/maven2", null, true, true, indexers);

    // Update the index (incremental update will happen if this is not 1st run and files are not deleted)
    // This whole block below should not be executed on every app start, but rather controlled by some configuration
    // since this block will always emit at least one HTTP GET. Central indexes are updated once a week, but
    // other index sources might have different index publishing frequency.
    // Preferred frequency is once a week.
    if (true) {/*ww  w  .  ja  v a  2 s  .c  o m*/
        System.out.println("Updating Index...");
        System.out.println("This might take a while on first run, so please be patient!");
        // Create ResourceFetcher implementation to be used with IndexUpdateRequest
        // Here, we use Wagon based one as shorthand, but all we need is a ResourceFetcher implementation
        TransferListener listener = new AbstractTransferListener() {
            public void transferStarted(TransferEvent transferEvent) {
                System.out.print(" Downloading " + transferEvent.getResource().getName());
            }

            public void transferProgress(TransferEvent transferEvent, byte[] buffer, int length) {
            }

            public void transferCompleted(TransferEvent transferEvent) {
                System.out.println(" - Done");
            }
        };
        ResourceFetcher resourceFetcher = new WagonHelper.WagonFetcher(httpWagon, listener, null, null);

        Date centralContextCurrentTimestamp = centralContext.getTimestamp();
        IndexUpdateRequest updateRequest = new IndexUpdateRequest(centralContext, resourceFetcher);
        IndexUpdateResult updateResult = indexUpdater.fetchAndUpdateIndex(updateRequest);
        if (updateResult.isFullUpdate()) {
            System.out.println("Full update happened!");
        } else if (updateResult.getTimestamp().equals(centralContextCurrentTimestamp)) {
            System.out.println("No update needed, index is up to date!");
        } else {
            System.out.println("Incremental update happened, change covered " + centralContextCurrentTimestamp
                    + " - " + updateResult.getTimestamp() + " period.");
        }

        System.out.println();
    }

    System.out.println();
    System.out.println("Using index");
    System.out.println("===========");
    System.out.println();

    // ====
    // Case:
    // dump all the GAVs
    // NOTE: will not actually execute do this below, is too long to do (Central is HUGE), but is here as code
    // example

    int j = 0;
    if (true) {

        final IndexSearcher searcher = centralContext.acquireIndexSearcher();
        try {
            final IndexReader ir = searcher.getIndexReader();
            for (int i = 0; i < ir.maxDoc(); i++) {
                if (!ir.isDeleted(i)) {
                    j++;
                    final Document doc = ir.document(i);
                    final ArtifactInfo ai = IndexUtils.constructArtifactInfo(doc, centralContext);
                    if (ai != null && "pom".equals(ai.fextension))
                        System.out.println(ai.groupId + ":" + ai.artifactId + ":" + ai.version + ":"
                                + ai.classifier + " (sha1=" + ai.sha1 + ")");

                }
            }
        } finally {
            centralContext.releaseIndexSearcher(searcher);
        }
    }
    System.err.println(j);
    if (j > 0)
        return;

    // ====
    // Case:
    // Search for all GAVs with known G and A and having version greater than V

    final GenericVersionScheme versionScheme = new GenericVersionScheme();
    final String versionString = "1.5.0";
    final Version version = versionScheme.parseVersion(versionString);

    // construct the query for known GA
    final Query groupIdQ = indexer.constructQuery(MAVEN.GROUP_ID,
            new SourcedSearchExpression("org.sonatype.nexus"));
    final Query artifactIdQ = indexer.constructQuery(MAVEN.ARTIFACT_ID,
            new SourcedSearchExpression("nexus-api"));
    final BooleanQuery query = new BooleanQuery();
    query.add(groupIdQ, BooleanClause.Occur.MUST);
    query.add(artifactIdQ, BooleanClause.Occur.MUST);

    // we want "jar" artifacts only
    query.add(indexer.constructQuery(MAVEN.PACKAGING, new SourcedSearchExpression("jar")),
            BooleanClause.Occur.MUST);
    // we want main artifacts only (no classifier)
    // Note: this below is unfinished API, needs fixing
    query.add(indexer.constructQuery(MAVEN.CLASSIFIER, new SourcedSearchExpression(Field.NOT_PRESENT)),
            BooleanClause.Occur.MUST_NOT);

    // construct the filter to express "V greater than"
    final ArtifactInfoFilter versionFilter = new ArtifactInfoFilter() {
        public boolean accepts(final IndexingContext ctx, final ArtifactInfo ai) {
            try {
                final Version aiV = versionScheme.parseVersion(ai.version);
                // Use ">=" if you are INCLUSIVE
                return aiV.compareTo(version) > 0;
            } catch (InvalidVersionSpecificationException e) {
                // do something here? be safe and include?
                return true;
            }
        }
    };

    System.out.println(
            "Searching for all GAVs with G=org.sonatype.nexus and nexus-api and having V greater than 1.5.0");
    final IteratorSearchRequest request = new IteratorSearchRequest(query,
            Collections.singletonList(centralContext), versionFilter);
    final IteratorSearchResponse response = indexer.searchIterator(request);
    for (ArtifactInfo ai : response) {
        System.out.println(ai.toString());
    }

    // Case:
    // Use index
    // Searching for some artifact
    Query gidQ = indexer.constructQuery(MAVEN.GROUP_ID,
            new SourcedSearchExpression("org.apache.maven.indexer"));
    Query aidQ = indexer.constructQuery(MAVEN.ARTIFACT_ID, new SourcedSearchExpression("indexer-artifact"));

    BooleanQuery bq = new BooleanQuery();
    bq.add(gidQ, BooleanClause.Occur.MUST);
    bq.add(aidQ, BooleanClause.Occur.MUST);

    searchAndDump(indexer, "all artifacts under GA org.apache.maven.indexer:indexer-artifact", bq);

    // Searching for some main artifact
    bq = new BooleanQuery();
    bq.add(gidQ, BooleanClause.Occur.MUST);
    bq.add(aidQ, BooleanClause.Occur.MUST);
    // bq.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression( "*" ) ), Occur.MUST_NOT
    // );

    searchAndDump(indexer, "main artifacts under GA org.apache.maven.indexer:indexer-artifact", bq);

    // doing sha1 search
    searchAndDump(indexer, "SHA1 7ab67e6b20e5332a7fb4fdf2f019aec4275846c2", indexer.constructQuery(MAVEN.SHA1,
            new SourcedSearchExpression("7ab67e6b20e5332a7fb4fdf2f019aec4275846c2")));

    searchAndDump(indexer, "SHA1 7ab67e6b20 (partial hash)",
            indexer.constructQuery(MAVEN.SHA1, new UserInputSearchExpression("7ab67e6b20")));

    // doing classname search (incomplete classname)
    searchAndDump(indexer,
            "classname DefaultNexusIndexer (note: Central does not publish classes in the index)",
            indexer.constructQuery(MAVEN.CLASSNAMES, new UserInputSearchExpression("DefaultNexusIndexer")));

    // doing search for all "canonical" maven plugins latest versions
    bq = new BooleanQuery();
    bq.add(indexer.constructQuery(MAVEN.PACKAGING, new SourcedSearchExpression("maven-plugin")),
            BooleanClause.Occur.MUST);
    bq.add(indexer.constructQuery(MAVEN.GROUP_ID, new SourcedSearchExpression("org.apache.maven.plugins")),
            BooleanClause.Occur.MUST);
    searchGroupedAndDump(indexer, "all \"canonical\" maven plugins", bq, new GAGrouping());

    // close cleanly
    indexer.closeIndexingContext(centralContext, false);
}

From source file:org.moxie.proxy.LuceneExecutor.java

License:Apache License

/**
 * Close the writer/searcher objects for a repository.
 * //from   ww  w . ja  v a 2s . c  om
 * @param repositoryName
 */
public synchronized void close(String repositoryName) {
    try {
        IndexSearcher searcher = searchers.remove(repositoryName);
        if (searcher != null) {
            searcher.getIndexReader().close();
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed to close index searcher for " + repositoryName, e);
    }

    try {
        IndexWriter writer = writers.remove(repositoryName);
        if (writer != null) {
            writer.close();
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed to close index writer for " + repositoryName, e);
    }
}

From source file:org.moxie.proxy.LuceneExecutor.java

License:Apache License

/**
 * Searches the specified repositories for the given text or query
 * /*from   ww w  . j  a  va  2 s . com*/
 * @param text
 *            if the text is null or empty, null is returned
 * @param page
 *            the page number to retrieve. page is 1-indexed.
 * @param pageSize
 *            the number of elements to return for this page
 * @param repositories
 *            a list of repositories to search. if no repositories are
 *            specified null is returned.
 * @return a list of SearchResults in order from highest to the lowest score
 * 
 */
public List<SearchResult> search(String text, int page, int pageSize, String... repositories) {
    if (StringUtils.isEmpty(text)) {
        return null;
    }
    if (repositories == null || repositories.length == 0) {
        return null;
    }
    Set<SearchResult> results = new LinkedHashSet<SearchResult>();
    StandardAnalyzer analyzer = new StandardAnalyzer(LUCENE_VERSION);
    try {
        // default search checks groupId and artifactId
        BooleanQuery query = new BooleanQuery();
        QueryParser qp;
        qp = new QueryParser(LUCENE_VERSION, FIELD_GROUPID, analyzer);
        qp.setAllowLeadingWildcard(true);
        query.add(qp.parse(text), Occur.SHOULD);

        qp = new QueryParser(LUCENE_VERSION, FIELD_ARTIFACTID, analyzer);
        qp.setAllowLeadingWildcard(true);
        query.add(qp.parse(text), Occur.SHOULD);

        IndexSearcher searcher;
        if (repositories.length == 1) {
            // single repository search
            searcher = getIndexSearcher(repositories[0]);
        } else {
            // multiple repository search
            List<IndexReader> readers = new ArrayList<IndexReader>();
            for (String repository : repositories) {
                IndexSearcher repositoryIndex = getIndexSearcher(repository);
                readers.add(repositoryIndex.getIndexReader());
            }
            IndexReader[] rdrs = readers.toArray(new IndexReader[readers.size()]);
            MultiSourceReader reader = new MultiSourceReader(rdrs);
            searcher = new IndexSearcher(reader);
        }
        Query rewrittenQuery = searcher.rewrite(query);
        Sort sort = new Sort(new SortField(FIELD_DATE, SortField.STRING, true));
        TopFieldDocs topDocs = searcher.search(rewrittenQuery, 10000, sort);
        int offset = Math.max(0, (page - 1) * pageSize);
        ScoreDoc[] hits = topDocs.scoreDocs;
        int totalHits = topDocs.totalHits;
        if (pageSize <= 0) {
            pageSize = totalHits;
        }
        if (totalHits > offset) {
            for (int i = offset, len = Math.min(offset + pageSize, hits.length); i < len; i++) {
                int docId = hits[i].doc;
                Document doc = searcher.doc(docId);
                SearchResult result = createSearchResult(doc, i + 1, totalHits);
                if (repositories.length == 1) {
                    // single repository search
                    result.repository = repositories[0];
                } else {
                    // multi-repository search
                    MultiSourceReader reader = (MultiSourceReader) searcher.getIndexReader();
                    int index = reader.getSourceIndex(docId);
                    result.repository = repositories[index];
                }
                results.add(result);
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, MessageFormat.format("Exception while searching for {0}", text), e);
    }
    return new ArrayList<SearchResult>(results);
}

From source file:org.neo4j.index.impl.lucene.legacy.LuceneLegacyIndex.java

License:Open Source License

private IndexHits<Document> search(IndexReference searcherRef, IndexSearcher fulltextTransactionStateSearcher,
        Query query, QueryContext additionalParametersOrNull, Collection<EntityId> removed) throws IOException {
    if (fulltextTransactionStateSearcher != null && !removed.isEmpty()) {
        letThroughAdditions(fulltextTransactionStateSearcher, query, removed);
    }//from   w  w  w .j av  a  2 s. co m

    IndexSearcher searcher = fulltextTransactionStateSearcher == null ? searcherRef.getSearcher()
            : new IndexSearcher(new MultiReader(searcherRef.getSearcher().getIndexReader(),
                    fulltextTransactionStateSearcher.getIndexReader()));
    IndexHits<Document> result;
    if (additionalParametersOrNull != null && additionalParametersOrNull.getTop() > 0) {
        result = new TopDocsIterator(query, additionalParametersOrNull, searcher);
    } else {
        Sort sorting = additionalParametersOrNull != null ? additionalParametersOrNull.getSorting() : null;
        boolean forceScore = additionalParametersOrNull == null
                || !additionalParametersOrNull.getTradeCorrectnessForSpeed();
        DocValuesCollector collector = new DocValuesCollector(forceScore);
        searcher.search(query, collector);
        return collector.getIndexHits(sorting);
    }
    return result;
}

From source file:org.neo4j.index.impl.lucene.LuceneBatchInserterIndex.java

License:Open Source License

private IndexSearcher searcher() {
    IndexSearcher result = this.searcher;
    try {//from  ww w.  j a va2  s. c  om
        if (result == null || writerModified) {
            if (result != null) {
                result.getIndexReader().close();
                result.close();
            }
            IndexReader newReader = IndexReader.open(writer, true);
            result = new IndexSearcher(newReader);
            writerModified = false;
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        this.searcher = result;
    }
}

From source file:org.neo4j.index.impl.lucene.LuceneIndex.java

License:Open Source License

private IndexHits<Document> search(IndexReference searcherRef, Query query,
        QueryContext additionalParametersOrNull, IndexSearcher additionsSearcher, Collection<Long> removed) {
    try {/*from   ww  w  .j  a va2  s . c  om*/
        if (additionsSearcher != null && !removed.isEmpty()) {
            letThroughAdditions(additionsSearcher, query, removed);
        }

        IndexSearcher searcher = additionsSearcher == null ? searcherRef.getSearcher()
                : new IndexSearcher(new MultiReader(searcherRef.getSearcher().getIndexReader(),
                        additionsSearcher.getIndexReader()));
        IndexHits<Document> result = null;
        if (additionalParametersOrNull != null && additionalParametersOrNull.getTop() > 0) {
            result = new TopDocsIterator(query, additionalParametersOrNull, searcher);
        } else {
            Sort sorting = additionalParametersOrNull != null ? additionalParametersOrNull.getSorting() : null;
            boolean forceScore = additionalParametersOrNull == null
                    || !additionalParametersOrNull.getTradeCorrectnessForSpeed();
            Hits hits = new Hits(searcher, query, null, sorting, forceScore);
            result = new HitsIterator(hits);
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException("Unable to query " + this + " with " + query, e);
    }
}

From source file:org.neo4j.index.lucene.LuceneIndexBatchInserterImpl.java

License:Open Source License

private IndexSearcher getSearcher(String key) {
    try {//from w  w w. j a  v a2 s  .c  om
        IndexWriterContext writer = getWriter(key, false);
        if (writer == null) {
            return null;
        }

        IndexSearcher oldSearcher = indexSearchers.get(key);
        IndexSearcher result = oldSearcher;
        if (oldSearcher == null || writer.modifiedFlag) {
            if (oldSearcher != null) {
                oldSearcher.getIndexReader().close();
                oldSearcher.close();
            }
            IndexReader newReader = writer.writer.getReader();
            result = new IndexSearcher(newReader);
            indexSearchers.put(key, result);
            writer.modifiedFlag = false;
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.neo4j.kernel.api.impl.index.DeferredConstraintVerificationUniqueLuceneIndexPopulator.java

License:Open Source License

@Override
public void verifyDeferredConstraints(PropertyAccessor accessor)
        throws IndexEntryConflictException, IOException {
    searcherManager.maybeRefresh();//from   w  w w . ja  v  a2 s .  c  o m
    IndexSearcher searcher = searcherManager.acquire();

    try {
        DuplicateCheckingCollector collector = duplicateCheckingCollector(accessor);
        TermEnum terms = searcher.getIndexReader().terms();
        while (terms.next()) {
            Term term = terms.term();

            if (!NODE_ID_KEY.equals(term.field()) && terms.docFreq() > 1) {
                collector.reset();
                searcher.search(new TermQuery(term), collector);
            }
        }
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IndexEntryConflictException) {
            throw (IndexEntryConflictException) cause;
        }
        throw e;
    } finally {
        searcherManager.release(searcher);
    }
}

From source file:org.neo4j.kernel.api.impl.index.UniqueLuceneIndexPopulator.java

License:Open Source License

@Override
public void add(long nodeId, Object propertyValue) throws IndexEntryConflictException, IOException {
    Long previousEntry = currentBatch.get(propertyValue);
    if (previousEntry == null) {
        IndexSearcher searcher = searcherManager.acquire();
        try {/*from w  w  w  . j  a  v a 2s  .c  o m*/
            TopDocs docs = searcher.search(documentStructure.newQuery(propertyValue), 1);
            if (docs.totalHits > 0) {
                Document doc = searcher.getIndexReader().document(docs.scoreDocs[0].doc);
                previousEntry = documentStructure.getNodeId(doc);
            }
        } finally {
            searcherManager.release(searcher);
        }
    }
    if (previousEntry != null) {
        if (previousEntry != nodeId) {
            throw new PreexistingIndexEntryConflictException(propertyValue, previousEntry, nodeId);
        }
    } else {
        currentBatch.put(propertyValue, nodeId);
        writer.addDocument(documentStructure.newDocumentRepresentingProperty(nodeId, propertyValue));
        if (currentBatch.size() >= batchSize) {
            startNewBatch();
        }
    }
}

From source file:org.neo4j.kernel.api.impl.schema.verification.SimpleUniquenessVerifier.java

License:Open Source License

@Override
public void verify(PropertyAccessor accessor, int propKeyId) throws IndexEntryConflictException, IOException {
    try {/*w w  w.j  a  v a  2  s .c  om*/
        DuplicateCheckingCollector collector = new DuplicateCheckingCollector(accessor, propKeyId);
        IndexSearcher searcher = indexSearcher();
        for (LeafReaderContext leafReaderContext : searcher.getIndexReader().leaves()) {
            Fields fields = leafReaderContext.reader().fields();
            for (String field : fields) {
                if (LuceneDocumentStructure.NODE_ID_KEY.equals(field)) {
                    continue;
                }

                TermsEnum terms = LuceneDocumentStructure.originalTerms(fields.terms(field), field);
                BytesRef termsRef;
                while ((termsRef = terms.next()) != null) {
                    if (terms.docFreq() > 1) {
                        collector.reset();
                        searcher.search(new TermQuery(new Term(field, termsRef)), collector);
                    }
                }
            }
        }
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IndexEntryConflictException) {
            throw (IndexEntryConflictException) cause;
        }
        throw e;
    }
}