Search in sources :

Example 6 with SearcherManager

use of org.apache.lucene.search.SearcherManager in project lucene-solr by apache.

the class AnalyzingInfixSuggester method getCount.

@Override
public long getCount() throws IOException {
    if (searcherMgr == null) {
        return 0;
    }
    SearcherManager mgr;
    IndexSearcher searcher;
    synchronized (searcherMgrLock) {
        // acquire & release on same SearcherManager, via local reference
        mgr = searcherMgr;
        searcher = mgr.acquire();
    }
    try {
        return searcher.getIndexReader().numDocs();
    } finally {
        mgr.release(searcher);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SearcherManager(org.apache.lucene.search.SearcherManager)

Example 7 with SearcherManager

use of org.apache.lucene.search.SearcherManager in project lucene-solr by apache.

the class AnalyzingInfixSuggester method ensureOpen.

private synchronized void ensureOpen() throws IOException {
    if (writer == null) {
        if (DirectoryReader.indexExists(dir)) {
            // Already built; open it:
            writer = new IndexWriter(dir, getIndexWriterConfig(getGramAnalyzer(), IndexWriterConfig.OpenMode.APPEND));
        } else {
            writer = new IndexWriter(dir, getIndexWriterConfig(getGramAnalyzer(), IndexWriterConfig.OpenMode.CREATE));
        }
        synchronized (searcherMgrLock) {
            SearcherManager oldSearcherMgr = searcherMgr;
            searcherMgr = new SearcherManager(writer, null);
            if (oldSearcherMgr != null) {
                oldSearcherMgr.close();
            }
        }
    }
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) SearcherManager(org.apache.lucene.search.SearcherManager)

Example 8 with SearcherManager

use of org.apache.lucene.search.SearcherManager in project lucene-solr by apache.

the class TestTryDelete method testTryDeleteDocument.

public void testTryDeleteDocument() throws IOException {
    Directory directory = createIndex();
    IndexWriter writer = getWriter(directory);
    ReferenceManager<IndexSearcher> mgr = new SearcherManager(writer, new SearcherFactory());
    IndexSearcher searcher = mgr.acquire();
    TopDocs topDocs = searcher.search(new TermQuery(new Term("foo", "0")), 100);
    assertEquals(1, topDocs.totalHits);
    long result;
    if (random().nextBoolean()) {
        IndexReader r = DirectoryReader.open(writer);
        result = writer.tryDeleteDocument(r, 0);
        r.close();
    } else {
        result = writer.tryDeleteDocument(searcher.getIndexReader(), 0);
    }
    // The tryDeleteDocument should have succeeded:
    assertTrue(result != -1);
    assertTrue(writer.hasDeletions());
    if (random().nextBoolean()) {
        writer.commit();
    }
    assertTrue(writer.hasDeletions());
    mgr.maybeRefresh();
    searcher = mgr.acquire();
    topDocs = searcher.search(new TermQuery(new Term("foo", "0")), 100);
    assertEquals(0, topDocs.totalHits);
}
Also used : SearcherFactory(org.apache.lucene.search.SearcherFactory) IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) SearcherManager(org.apache.lucene.search.SearcherManager) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 9 with SearcherManager

use of org.apache.lucene.search.SearcherManager in project lucene-solr by apache.

the class TestTryDelete method testTryDeleteDocumentCloseAndReopen.

public void testTryDeleteDocumentCloseAndReopen() throws IOException {
    Directory directory = createIndex();
    IndexWriter writer = getWriter(directory);
    ReferenceManager<IndexSearcher> mgr = new SearcherManager(writer, new SearcherFactory());
    IndexSearcher searcher = mgr.acquire();
    TopDocs topDocs = searcher.search(new TermQuery(new Term("foo", "0")), 100);
    assertEquals(1, topDocs.totalHits);
    long result = writer.tryDeleteDocument(DirectoryReader.open(writer), 0);
    assertTrue(result != -1);
    writer.commit();
    assertTrue(writer.hasDeletions());
    mgr.maybeRefresh();
    searcher = mgr.acquire();
    topDocs = searcher.search(new TermQuery(new Term("foo", "0")), 100);
    assertEquals(0, topDocs.totalHits);
    writer.close();
    searcher = new IndexSearcher(DirectoryReader.open(directory));
    topDocs = searcher.search(new TermQuery(new Term("foo", "0")), 100);
    assertEquals(0, topDocs.totalHits);
}
Also used : SearcherFactory(org.apache.lucene.search.SearcherFactory) IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) SearcherManager(org.apache.lucene.search.SearcherManager) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 10 with SearcherManager

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

the class AllNodesCollector method getAllNodes.

public static List<Long> getAllNodes(Directory directory, Value propertyValue) throws IOException {
    try (SearcherManager manager = new SearcherManager(directory, new Neo4jSearcherFactory())) {
        IndexSearcher searcher = manager.acquire();
        Query query = LuceneDocumentStructure.newSeekQuery(propertyValue);
        AllNodesCollector collector = new AllNodesCollector();
        searcher.search(query, collector);
        return collector.nodeIds;
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Neo4jSearcherFactory(org.neo4j.kernel.api.impl.index.partition.Neo4jSearcherFactory) Query(org.apache.lucene.search.Query) SearcherManager(org.apache.lucene.search.SearcherManager)

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