Boolean Query
当parse函数中指定多个项时,QueryParser能很方便地构建BooleanQuery对象。使用圆括号分组,通过指定的-、+、AND、OR以及NOT这些操作符。
允许进行逻辑的AND、OR或NOT组合,通过BooleanQuery的add方法将一个查询子句增加到某个BooleanQuery对象中:
public void add(Query query,boolean required,boolean prohibited)
|
Required |
||
False |
true |
||
Prohibited |
False |
该子句是可选(逻辑或) |
该子句是必须匹配(逻辑与) |
True |
该子句是不允许匹配的(逻辑非) |
|
TermQuery searchingBooks=new TermQuery(new Term("subject","search"));
RangeQuery currentBooks=new RangeQuery(new Term("pubmonth","200401"),new Term("pubmonth","200412"),true);
BooleanQuery currentsearchingBooks=new BooleanQuery();
currentSearchingBooks.add(searchingBooks,true,false);
currentSearchingBooks.add(currentBooks,true,false);
IndexSearcher searcher=new IndexSearcher(directory);
Hits hits=searcher.search(currentSearchingBooks);