Lucene查询对象之BooleanQuery(备忘)
在看这个对象之前,我们要知道BooleanQuery这个对象能干什么。它能干什么呢,它能进行组合查询。大家都知道,一般的高级查询(比如前程无忧的职位搜索应该用到了组合查询)都会用到组合查询。它了组合,它应该是搜索多个条目,每个条目应该是它的Clause。
别的不多说,我们来看看这个BooleanQuery类的主要属性和方法。
/** A Query that matches documents matching boolean combinations of other
* queries, e.g. {@link TermQuery}s, {@link PhraseQuery}s or other
* BooleanQuerys.
*/
public class BooleanQuery extends Query implements Iterable<BooleanClause>
可以清晰的看到BooleanQuery继承Query这个抽象类。
/** Adds a clause to a boolean query.
*
* @throws TooManyClauses if the new number of clauses exceeds the maximum clause number
* @see #getMaxClauseCount()
*/
public void add(Query query, BooleanClause.Occur occur) {//可以将其他的Query对象加入作为它的一个条件 Occur来控制它的组合关系
add(new BooleanClause(query, occur));
}
/** Adds a clause to a boolean query.
* @throws TooManyClauses if the new number of clauses exceeds the maximum clause number
* @see #getMaxClauseCount()
*/
public void add(BooleanClause clause) {
if (clauses.size() >= maxClauseCount)//-----------加查询条件有个上限,如果大于这个上限就会报异常
throw new TooManyClauses();
clauses.add(clause);
}
接下来我们来看看各个条件的组合关系怎么控制的,有几种关系。下面我们来分析Occour.它是一个枚举
/** Specifies how clauses are to occur in matching documents. */
public static enum Occur {
/** Use this operator for clauses that <i>must</i> appear in the matching documents. */
MUST { @Override public String toString() { return "+"; } },
/** Use this operator for clauses that <i>should</i> appear in the
* matching documents. For a BooleanQuery with no <code>MUST</code>
* clauses one or more <code>SHOULD</code> clauses must match a document
* for the BooleanQuery to match.
* @see BooleanQuery#setMinimumNumberShouldMatch
*/
SHOULD { @Override public String toString() { return ""; } },
/** Use this operator for clauses that <i>must not</i> appear in the matching documents.
* Note that it is not possible to search for queries that only consist
* of a <code>MUST_NOT</code> clause. */
MUST_NOT { @Override public String toString() { return "-"; } };
}
组合关系代表的意思如下:
1、MUST和MUST表示“与”的关系,即“并集”。
2、MUST和MUST_NOT前者包含后者不包含。
3、MUST_NOT和MUST_NOT没意义
4、SHOULD与MUST表示MUST,SHOULD失去意义;
5、SHOUlD与MUST_NOT相当于MUST与MUST_NOT。
6、SHOULD与SHOULD表示“或”的概念。
OK看示例吧!
Directory dic=new SimpleFSDirectory(new File(ILuceneManager.DEFAULT_REGION_LUCENE_INDEX_PATH));
IndexSearcher searcher=new IndexSearcher(dic);
//----------组合查询
BooleanQuery query=new BooleanQuery();
//-----------地名带有浙字
Term term1=new Term("ADDRESS", "浙");
TermQuery tq1=new TermQuery(term1);
BooleanClause clause=new BooleanClause(tq1, BooleanClause.Occur.SHOULD);
query.add(clause);
//--------权重最高的
Term term2=new Term("weight", "1");
TermQuery tq2=new TermQuery(term2);
BooleanClause clause2=new BooleanClause(tq2, BooleanClause.Occur.MUST);
query.add(clause2);
TopDocs tops=searcher.search(query,10);
ScoreDoc[] scores=tops.scoreDocs;
int length=tops.totalHits;
for(int i=0;i<(length>LuceneManagerImpl.DEFAULT_QUERY_NUM?LuceneManagerImpl.DEFAULT_QUERY_NUM:length);i++){
Document targetDoc=searcher.doc(scores[i].doc);
System.out.println(targetDoc.getFields("NAME")[0].stringValue());
};
System.out.println("查询所得条数:"+tops.totalHits);