lucene中search内置的Filter

  categories:资料  author:

 

来源:互联网

先来看一下RangeFilter:

public static void printFilter(String path)throws IOException
{
Term normal=new Term(“securitylevel”,SECURITY_NORMAL+””);
Term middle=new Term(“securitylevel”,SECURITY_MIDDLE+””);
Term advanced=new Term(“securitylevel”,SECURITY_ADVANCED+””);

BooleanQuery bq=new BooleanQuery();
bq.add(new TermQuery(normal),BooleanClause.Occur.SHOULD);
bq.add(new TermQuery(middle),BooleanClause.Occur.SHOULD);
bq.add(new TermQuery(advanced),BooleanClause.Occur.SHOULD);

IndexSearcher searcher=new IndexSearcher(path);

RangeFilter filter=new RangeFilter(“publishdate”,”1999-01-01″,”1999-10-10″,true,true);

Hits hits=searcher.search(bq,filter);

for(int i=0;i<hits.length();i++)
{
Document doc=hits.doc(i);

System.out.println(“书号:”);
System.out.println(doc.get(“bookNumber”));
System.out.println(“书名:”);
System.out.println(doc.get(“bookname”));
System.out.println(“出版日期:”);
System.out.println(doc.get(“publishdate”));
System.out.println(“安全级别”);
int level=Integer.parseInt(doc.get(“securitylevel”));
switch(level)
{
case SECURITY_ADVANCED:
System.out.println(“高级”);
break;
case SECURITY_MIDDLE:
System.out.println(“中级”);
break;
case SECURITY_NORMAL:
System.out.println(“一般”);
break;
}
System.out.println(“====================”);
}
}

其实这段代码没什么,就增加了下面这段代码

RangeFilter filter=new RangeFilter(“publishdate”,”1999-01-01″,”1999-10-10″,true,true);

观察到参数里里面有两个true,表示边界包含在范围内.

其次,QueryFilter(在结果中查找)

 

Term begin=new Term(“publishdate”,”1999-01-01″);
Term end=new Term (“publishdate”,”1999-10-15″);

RangeQuery q=new RangeQuery(begin,end,true);

QueryFilter filter=new QueryFilter(q);

Term normal=new Term(“securitylevel”,SECURITY_NORMAL+””);
TermQuery query=new TermQuery(normal);

IndexSearcher searcher=new IndexSearcher(path);
Hits hits=searcher.search(query,filter);

这里只使用了词条搜索,让我不知所以啊,因为搜索的方法太多了;呵呵……



快乐成长 每天进步一点点