use of org.apache.lucene.index.FilterDirectoryReader in project lucene-solr by apache.
the class TestSearcherManager method testCustomDirectoryReader.
// LUCENE-6087
public void testCustomDirectoryReader() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
DirectoryReader nrtReader = w.getReader();
FilterDirectoryReader reader = new MyFilterDirectoryReader(nrtReader);
assertEquals(nrtReader, reader.getDelegate());
assertEquals(nrtReader, FilterDirectoryReader.unwrap(reader));
SearcherManager mgr = new SearcherManager(reader, null);
for (int i = 0; i < 10; i++) {
w.addDocument(new Document());
mgr.maybeRefresh();
IndexSearcher s = mgr.acquire();
try {
assertTrue(s.getIndexReader() instanceof MyFilterDirectoryReader);
for (LeafReaderContext ctx : s.getIndexReader().leaves()) {
assertTrue(ctx.reader() instanceof MyFilterLeafReader);
}
} finally {
mgr.release(s);
}
}
mgr.close();
w.close();
dir.close();
}
use of org.apache.lucene.index.FilterDirectoryReader in project lucene-solr by apache.
the class TestTermQuery method testCreateWeightDoesNotSeekIfScoresAreNotNeeded.
public void testCreateWeightDoesNotSeekIfScoresAreNotNeeded() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE));
// segment that contains the term
Document doc = new Document();
doc.add(new StringField("foo", "bar", Store.NO));
w.addDocument(doc);
w.getReader().close();
// segment that does not contain the term
doc = new Document();
doc.add(new StringField("foo", "baz", Store.NO));
w.addDocument(doc);
w.getReader().close();
// segment that does not contain the field
w.addDocument(new Document());
DirectoryReader reader = w.getReader();
FilterDirectoryReader noSeekReader = new NoSeekDirectoryReader(reader);
IndexSearcher noSeekSearcher = new IndexSearcher(noSeekReader);
Query query = new TermQuery(new Term("foo", "bar"));
AssertionError e = expectThrows(AssertionError.class, () -> noSeekSearcher.createNormalizedWeight(query, true));
assertEquals("no seek", e.getMessage());
// no exception
noSeekSearcher.createNormalizedWeight(query, false);
IndexSearcher searcher = new IndexSearcher(reader);
// use a collector rather than searcher.count() which would just read the
// doc freq instead of creating a scorer
TotalHitCountCollector collector = new TotalHitCountCollector();
searcher.search(query, collector);
assertEquals(1, collector.getTotalHits());
TermQuery queryWithContext = new TermQuery(new Term("foo", "bar"), TermContext.build(reader.getContext(), new Term("foo", "bar")));
collector = new TotalHitCountCollector();
searcher.search(queryWithContext, collector);
assertEquals(1, collector.getTotalHits());
IOUtils.close(reader, w, dir);
}
use of org.apache.lucene.index.FilterDirectoryReader in project gerrit by GerritCodeReview.
the class WrappableSearcherManager method getSearcher.
/**
* Expert: creates a searcher from the provided {@link IndexReader} using the provided {@link
* SearcherFactory}. NOTE: this decRefs incoming reader on throwing an exception.
*/
@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;
}
Aggregations