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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM