Search in sources :

Example 1 with FilterDirectoryReader

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();
}
Also used : FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 2 with FilterDirectoryReader

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);
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) StringField(org.apache.lucene.document.StringField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 3 with FilterDirectoryReader

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;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) IndexReader(org.apache.lucene.index.IndexReader) FilterLeafReader(org.apache.lucene.index.FilterLeafReader)

Aggregations

FilterDirectoryReader (org.apache.lucene.index.FilterDirectoryReader)3 Document (org.apache.lucene.document.Document)2 DirectoryReader (org.apache.lucene.index.DirectoryReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 Directory (org.apache.lucene.store.Directory)2 StringField (org.apache.lucene.document.StringField)1 FilterLeafReader (org.apache.lucene.index.FilterLeafReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Term (org.apache.lucene.index.Term)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1