Example usage for org.apache.lucene.search SearcherFactory newSearcher

List of usage examples for org.apache.lucene.search SearcherFactory newSearcher

Introduction

In this page you can find the example usage for org.apache.lucene.search SearcherFactory newSearcher.

Prototype

public IndexSearcher newSearcher(IndexReader reader, IndexReader previousReader) throws IOException 

Source Link

Document

Returns a new IndexSearcher over the given reader.

Usage

From source file:com.google.gerrit.lucene.WrappableSearcherManager.java

License:Apache License

/** Expert: creates a searcher from the provided {@link
 *  IndexReader} using the provided {@link
 *  SearcherFactory}.  NOTE: this decRefs incoming reader
 * on throwing an exception. *///from  w w  w. j  a v a  2  s . c om
@SuppressWarnings("resource")
public static IndexSearcher getSearcher(SearcherFactory searcherFactory, IndexReader reader)
        throws IOException {
    boolean success = false;
    final IndexSearcher searcher;
    try {
        searcher = searcherFactory.newSearcher(reader, null);
        // Modification for Gerrit: Allow searcherFactory to transitively wrap the
        // provided reader.
        IndexReader unwrapped = searcher.getIndexReader();
        while (true) {
            if (unwrapped == reader) {
                break;
            } else if (unwrapped instanceof FilterDirectoryReader) {
                unwrapped = ((FilterDirectoryReader) unwrapped).getDelegate();
            } else if (unwrapped instanceof FilterLeafReader) {
                unwrapped = ((FilterLeafReader) unwrapped).getDelegate();
            } else {
                break;
            }
        }

        if (unwrapped != reader) {
            throw new IllegalStateException("SearcherFactory must wrap the provided reader (got "
                    + searcher.getIndexReader() + " but expected " + reader + ")");
        }
        success = true;
    } finally {
        if (!success) {
            reader.decRef();
        }
    }
    return searcher;
}