Lucene Filter CachingWrapperFilter

IndexSearcher is = new IndexSearcher(Index_Store_Path);			
TermQuery tq = new TermQuery(new Term("bk", "bc"));			
QueryFilter qf = new QueryFilter(tq);
QueryParser qp = new QueryParser("bk", new StandardAnalyzer());
Query q = qp.parse("gh hi");
Hits hits = is.search(q, qf, s);


TermQuery tq = new TermQuery(new Term("bk", "bc"));
QueryFilter qf = new QueryFilter(tq);
			
//此时只对Index 进行了一次访问 , 因为CachingWrapperFilter 能够缓存上一个Filter的结果 从而直接在结果中查找 而普通的则需要访问两次Index
CachingWrapperFilter cwf = new CachingWrapperFilter(qf);			
QueryParser qp = new QueryParser("bk", new StandardAnalyzer());
Query q = qp.parse("gh hi");
Hits hits = is.search(q, cwf);

CachingWrapperFilter 使用WeakHashMap作为Cache,其中key:IndexReader value:BitSet

CachingWrapperFilter的bits方法被调用的时候,会首先检查Cache中是否已经存在上一个Filter的结果,若存在,直接取出后返回,否则执行传入的Filter
 

你可能感兴趣的:(Lucene)