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;
}
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();
}
Aggregations