Search in sources :

Example 21 with SearcherManager

use of org.apache.lucene.search.SearcherManager in project OpenGrok by OpenGrok.

the class RuntimeEnvironment method getIndexSearcher.

/**
 * Get IndexSearcher for given project.
 * Each IndexSearcher is born from a SearcherManager object. There is one SearcherManager for every project.
 * This schema makes it possible to reuse IndexSearcher/IndexReader objects so the heavy lifting
 * (esp. system calls) performed in FSDirectory and DirectoryReader happens only once for a project.
 * The caller has to make sure that the IndexSearcher is returned back
 * to the SearcherManager. This is done with returnIndexSearcher().
 * The return of the IndexSearcher should happen only after the search result data are read fully.
 *
 * @param projectName project
 * @return SearcherManager for given project
 * @throws IOException I/O exception
 */
@SuppressWarnings("java:S2095")
public SuperIndexSearcher getIndexSearcher(String projectName) throws IOException {
    SearcherManager mgr = searcherManagerMap.get(projectName);
    SuperIndexSearcher searcher;
    if (mgr == null) {
        File indexDir = new File(getDataRootPath(), IndexDatabase.INDEX_DIR);
        Directory dir = FSDirectory.open(new File(indexDir, projectName).toPath());
        mgr = new SearcherManager(dir, new ThreadpoolSearcherFactory());
        searcherManagerMap.put(projectName, mgr);
    }
    searcher = (SuperIndexSearcher) mgr.acquire();
    searcher.setSearcherManager(mgr);
    return searcher;
}
Also used : SearcherManager(org.apache.lucene.search.SearcherManager) File(java.io.File) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Aggregations

SearcherManager (org.apache.lucene.search.SearcherManager)21 IndexSearcher (org.apache.lucene.search.IndexSearcher)11 IOException (java.io.IOException)8 Directory (org.apache.lucene.store.Directory)8 SearcherFactory (org.apache.lucene.search.SearcherFactory)7 IndexWriter (org.apache.lucene.index.IndexWriter)6 TermQuery (org.apache.lucene.search.TermQuery)4 RAMDirectory (org.apache.lucene.store.RAMDirectory)4 File (java.io.File)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3 Query (org.apache.lucene.search.Query)3 TopDocs (org.apache.lucene.search.TopDocs)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 FilterLeafReader (org.apache.lucene.index.FilterLeafReader)2 LeafReader (org.apache.lucene.index.LeafReader)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 SegmentReader (org.apache.lucene.index.SegmentReader)2 Term (org.apache.lucene.index.Term)2 FSDirectory (org.apache.lucene.store.FSDirectory)2