From 7c88eab8c9d984204338ade236535d54b8b6348a Mon Sep 17 00:00:00 2001 From: Shibi Balamurugan Date: Mon, 4 Nov 2024 11:14:23 -0500 Subject: [PATCH 1/2] Multireader Support in Searcher Manager --- .../apache/lucene/search/SearcherManager.java | 7 ++-- .../lucene/search/TestSearcherManager.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java b/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java index 072c13d92746..1ec9d70a46ba 100644 --- a/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java +++ b/lucene/core/src/java/org/apache/lucene/search/SearcherManager.java @@ -114,16 +114,15 @@ public SearcherManager(Directory dir, SearcherFactory searcherFactory) throws IO } /** - * Creates and returns a new SearcherManager from an existing {@link DirectoryReader}. Note that + * Creates and returns a new SearcherManager from an existing {@link IndexReader}. Note that * this steals the incoming reference. * - * @param reader the DirectoryReader. + * @param reader the IndexReader. * @param searcherFactory An optional {@link SearcherFactory}. Pass null if you don't * require the searcher to be warmed before going live or other custom behavior. * @throws IOException if there is a low-level I/O error */ - public SearcherManager(DirectoryReader reader, SearcherFactory searcherFactory) - throws IOException { + public SearcherManager(IndexReader reader, SearcherFactory searcherFactory) throws IOException { if (searcherFactory == null) { searcherFactory = new SearcherFactory(); } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java b/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java index e44855afe870..7137248883d5 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java @@ -545,6 +545,43 @@ public void testCustomDirectoryReader() throws Exception { w.close(); dir.close(); } + + // LUCENE-13975 + public void testMultiReaderReader() throws Exception { + Directory dir = newDirectory(); + RandomIndexWriter w = new RandomIndexWriter(random(), dir); + DirectoryReader nrtReader = w.getReader(); + + Directory dir2 = newDirectory(); + RandomIndexWriter w2 = new RandomIndexWriter(random(), dir2); + DirectoryReader nrtReader2 = w2.getReader(); + + MultiReader reader = new MultiReader(nrtReader, nrtReader2); + + SearcherManager mgr = new SearcherManager(reader, null); + for(int i=0;i<10;i++) { + Document d = new Document(); + d.add(new TextField("contents", English.intToEnglish(i), Field.Store.NO)); + w.addDocument(new Document()); + + Document d2 = new Document(); + d2.add(new TextField("contents", English.intToEnglish(i+10), Field.Store.NO)); + w2.addDocument(new Document()); + } + + mgr.maybeRefresh(); + IndexSearcher s = mgr.acquire(); + try { + assertTrue(s.getIndexReader() instanceof MultiReader); + assertEquals(s.count(new MatchAllDocsQuery()), 20); + } finally { + mgr.release(s); + } + + mgr.close(); + w.close(); + dir.close(); + } public void testPreviousReaderIsPassed() throws IOException { final Directory dir = newDirectory(); From fafe3ffc5f12cd713146584f60dc9af2a016907d Mon Sep 17 00:00:00 2001 From: Shibi Balamurugan Date: Mon, 4 Nov 2024 12:58:27 -0500 Subject: [PATCH 2/2] add imports --- .../apache/lucene/search/TestSearcherManager.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java b/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java index 7137248883d5..ca858635e54f 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSearcherManager.java @@ -28,20 +28,13 @@ import java.util.concurrent.atomic.AtomicReference; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; -import org.apache.lucene.index.ConcurrentMergeScheduler; -import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.FilterDirectoryReader; -import org.apache.lucene.index.FilterLeafReader; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.IndexWriterConfig; -import org.apache.lucene.index.LeafReader; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.Term; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.*; import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.Directory; import org.apache.lucene.tests.analysis.MockAnalyzer; import org.apache.lucene.tests.index.RandomIndexWriter; +import org.apache.lucene.util.English; import org.apache.lucene.tests.index.ThreadedIndexingAndSearchingTestCase; import org.apache.lucene.tests.util.LuceneTestCase; import org.apache.lucene.tests.util.LuceneTestCase.SuppressCodecs; @@ -545,7 +538,7 @@ public void testCustomDirectoryReader() throws Exception { w.close(); dir.close(); } - + // LUCENE-13975 public void testMultiReaderReader() throws Exception { Directory dir = newDirectory();