欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lucene-链式过滤器ChainedFliter

程序员文章站 2022-05-16 09:09:55
...

1、使用ChainedFilter将其它过滤器链接起来,在过滤器中执行AND、OR、XOR和ANDNOT的位操作。

2、ChianedFilter没有缓存,只要把它封装进CachingWrappingFilter就可以提供对过滤器结果的缓存操作。

3、

public class ChainedFilterTest extends TestCase{

public static final intMAX=500;

private RAMDirectorydirectory;

private IndexSearchersearcher;

private Query query;

private DateFilterdateFilter;

private QueryFilterbobFilter;

privateQueryFilter sueFilter;

public void setUp() throwsException{

directory =newRAMDirectory();

IndexWriter writer=new IndexWriter(directory,newWhitespaceAnalyzer(),true);

Calendarcal=Calendar.getInstance();

cal.setTimeInMillies(1041397200000L);

 

for (int i=0;i<MAX;i++){

Document doc=new Document();

doc.add(Field.Keyword("key",""+(i+1)));

doc.add(Field.Keyword("owner",(i<MAX/2)?"bob":"sue"));

doc.add(Field.Keyword("date",cal.getTime()));

writer.addDocument(doc);

cal.add(Calendar.DATE,1);

}

writer.close();

searcher=new IndexSearcerh(directory);

BooleanQueryquery=newBooleanQuery();

bq.add(new TermQuery(newTerm("owner","bob")),false,false);

bq.add(new TermQuery(new Term("owner","sue")),false,false);

query=bq;

 

DatapastTheEnd=parseDate("2009 Jan 1");

dateFilter=DateFilter.Before("date",pastTheEnd);

bobFilter=newQueryFilter(new TermQuery(new Term("owner","bob")));

sueFilter=new QueryFilter(new TermQuery(newTerm("owner","sue")));


}

//.........

}

4、测试单个过滤器

ChaindFilter chain=new ChainedFilter(new Filter[]{dateFilter});

Hits hits=searcher.search(query,chain);

chain=new ChainedFilter(new Filter[] {bobFilter});

hits=searcher.search(query,chain);

5、测试OR过滤器

ChainedFilter chain=new chainedFilter(newFilter[]{sueFilter,bobFilter});

Hits hits=searcher.search(query,chain);

6、测试AND过滤器

//2个都要匹配

ChainedFilter chain=new chainedFilter(new Filter[]{dateFilter,bobFilter},Chainedfilter.AND);

Hits hits=searcher.search(query,chain);

7、测试XOR过滤器

//只能匹配其中的一个过滤器,不能2个都匹配

ChainedFilter chain=new chainedFilter(new Filter[]{dateFilter,sueFilter},Chainedfilter.XOR);

Hits hits=searcher.search(query,chain);
8、测试ANDNOT过滤器

ChainedFilter chain=new chainedFilter(new Filter[]{dateFilter,sueFilter},new int[]{Chainedfilter.AND,Chainedfilter.ANDNOT);//结果符合第一个过滤器,而不能匹配第二个过滤器

Hits hits=searcher.search(query,chain);

相关标签: lucene