最近在学习Lucene,使用的是5.3.0版本。
学习到filter的使用时,发现在Lucene5中filter的好多子类都过时了,例如
@Deprecated
public class TermFilter extends QueryWrapperFilter
并且query方法也过时了
/** Finds the top <code>n</code>
* hits for <code>query</code>, applying <code>filter</code> if non-null.
*
* @throws BooleanQuery.TooManyClauses If a query would exceed
* {@link BooleanQuery#getMaxClauseCount()} clauses.
* @deprecated Use {@link BooleanQuery boolean queries} with
* {@link BooleanClause.Occur#FILTER} clauses instead
*/
@Deprecated
public final TopDocs search(Query query, Filter filter, int n)
那怎么使用filter功能呢?
//在Lucene 5 中 filter的使用:
//filter是一种特殊的query
Query query = new TermQuery(new Term("title","lucene"));
Query filter1 = new TermQuery(new Term("subject","lucene"));
Query filter2 = NumericRangeQuery.newIntRange("pubmonth", 199908, 201005, true, true);
BooleanQuery booleanFilter = new BooleanQuery.Builder()
.add(filter1, Occur.MUST)
.add(filter2, Occur.MUST)
.build();
QueryWrapperFilter filter = new QueryWrapperFilter(booleanFilter);
BooleanQuery booleanQuery = new BooleanQuery.Builder()
.add(filter, Occur.FILTER)
.add(query, Occur.MUST).build();
TopDocs topDocs = indexSearcher.search(booleanQuery, Integer.MAX_VALUE);
对滴,是通过
Occur.FILTER 标识对应的查询是filter,这个在查询是就会做对应的处理。