Search in sources :

Example 1 with FilterLeafReader

use of org.apache.lucene.index.FilterLeafReader 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)

Example 2 with FilterLeafReader

use of org.apache.lucene.index.FilterLeafReader in project lucene-solr by apache.

the class TestTermScorer method testDoesNotLoadNorms.

public void testDoesNotLoadNorms() throws IOException {
    Term allTerm = new Term(FIELD, "all");
    TermQuery termQuery = new TermQuery(allTerm);
    LeafReader forbiddenNorms = new FilterLeafReader(indexReader) {

        @Override
        public NumericDocValues getNormValues(String field) throws IOException {
            fail("Norms should not be loaded");
            // unreachable
            return null;
        }

        @Override
        public CacheHelper getCoreCacheHelper() {
            return in.getCoreCacheHelper();
        }

        @Override
        public CacheHelper getReaderCacheHelper() {
            return in.getReaderCacheHelper();
        }
    };
    // We don't use newSearcher because it sometimes runs checkIndex which loads norms
    IndexSearcher indexSearcher = new IndexSearcher(forbiddenNorms);
    Weight weight = indexSearcher.createNormalizedWeight(termQuery, true);
    expectThrows(AssertionError.class, () -> {
        weight.scorer(forbiddenNorms.getContext()).iterator().nextDoc();
    });
    Weight weight2 = indexSearcher.createNormalizedWeight(termQuery, false);
    // should not fail this time since norms are not necessary
    weight2.scorer(forbiddenNorms.getContext()).iterator().nextDoc();
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) FilterLeafReader(org.apache.lucene.index.FilterLeafReader) Term(org.apache.lucene.index.Term) FilterLeafReader(org.apache.lucene.index.FilterLeafReader)

Aggregations

FilterLeafReader (org.apache.lucene.index.FilterLeafReader)2 FilterDirectoryReader (org.apache.lucene.index.FilterDirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 LeafReader (org.apache.lucene.index.LeafReader)1 Term (org.apache.lucene.index.Term)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1