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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值