browse()函数中有一行代码:
filter = handler.buildFilter(sel);
用于构建filter,下面是FacetHandler的函数buildFilter()的实现,其中buildRandomAccessFilter ()是主要实现函数:
public final RandomAccessFilter buildFilter(BrowseSelection sel) throws IOException { String[] selections = sel.getValues();//相当于BooleanQuery的MUST,就是必须要有的值 String[] notSelections = sel.getNotValues();//相当于NOT,不能有的属性值 Properties prop=sel.getSelectionProperties(); RandomAccessFilter filter = null; if (selections!=null && selections.length > 0) { if (sel.getSelectionOperation() == ValueOperation.ValueOperationAnd) { filter = buildRandomAccessAndFilter(selections,prop); if (filter == null) { filter = EmptyFilter.getInstance(); } } else { filter = buildRandomAccessOrFilter(selections, prop,false); if (filter == null) { return EmptyFilter.getInstance(); } } } if (notSelections!=null && notSelections.length>0) { RandomAccessFilter notFilter = buildRandomAccessOrFilter(notSelections, prop, true); if (filter==null) { filter = notFilter; } else { RandomAccessFilter andFilter = new RandomAccessAndFilter(Arrays.asList(new RandomAccessFilter[]{filter,notFilter})); filter = andFilter; } } return filter; }
在基类FacetHandler中有函数:
abstract public RandomAccessFilter buildRandomAccessFilter(String value,Properties selectionProperty) throws IOException;
那么在所有的具体实现类中都有自己的实现方法,以SimpleFacetHandler为例:
@Override public RandomAccessFilter buildRandomAccessFilter(String value, Properties prop) throws IOException { //各个FacetHanlder实例基本都是在这个地方不相同而已,比如MultiFacetHandler在这一行是 // new MultiFacetFilter(this, value); FacetFilter f = new FacetFilter(this, value); AdaptiveFacetFilter af = new AdaptiveFacetFilter(new FacetDataCacheBuilder(){ @Override public FacetDataCache build(BoboIndexReader reader) { return getFacetData(reader); } @Override public String getName() { return _name; } }, f, new String[]{value}); return af; }
每个FacetHandler基本都有自己专有的Filter,都是继承于RandomAccessFilter,下面是所有的继承关系图:
public abstract class RandomAccessFilter extends Filter { private static final long serialVersionUID = 1L; @Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { if (reader instanceof BoboIndexReader){ return getRandomAccessDocIdSet((BoboIndexReader)reader); } else{ throw new IllegalStateException("reader not instance of "+BoboIndexReader.class); } } public abstract RandomAccessDocIdSet getRandomAccessDocIdSet(BoboIndexReader reader) throws IOException; //返回该属性值在所有属性中的百分比 public double getFacetSelectivity(BoboIndexReader reader) { return 0.50; } }
可以看到RandomAccessFilter的主要的功能函数是返回一个RandomAccessDocIdSet ,而RandomAccessDocIdSet接口的主要功能函数是get (int docid)和iterator ()。get()判断某个doc是否满足条件:
public abstract class RandomAccessDocIdSet extends DocIdSet { public abstract boolean get(int docId); }
以及从Filter继承来的iterator()函数,用于返回一个满足条件的doc的迭代器:
public abstract DocIdSetIterator iterator() throws IOException;