在lucene的搜索中,最重要的无疑就是对query的理解和掌握了。这里扒拉一下源码(版本3.5.0)的query和query实现:

query是一个抽象类,实现类有以下几个:

termQuery

multiTermQuery

booleanQuery*

wildCardQuery  不推荐使用,效率低

PhraseQuery

FuzzyQuery 不推荐使用,效率低

TermRangeQuery

NumericRangeQuery

SpanQuery

/** The abstract base class for queries.
    <p>Instantiable subclasses are:
    <ul>
    <li> {@link TermQuery}
    <li> {@link MultiTermQuery}
    <li> {@link BooleanQuery}
    <li> {@link WildcardQuery}
    <li> {@link PhraseQuery}
    <li> {@link PrefixQuery}
    <li> {@link MultiPhraseQuery}
    <li> {@link FuzzyQuery}
    <li> {@link TermRangeQuery}
    <li> {@link NumericRangeQuery}
    <li> {@link org.apache.lucene.search.spans.SpanQuery}
    </ul>
    <p>A parser for queries is contained in:
    <ul>
    <li>{@link org.apache.lucene.queryParser.QueryParser QueryParser}
    </ul>
*/
public abstract class Query implements java.io.Serializable, Cloneable {
  private float boost = 1.0f;                     // query boost factor

  /** Sets the boost for this query clause to <code>b</code>.  Documents
   * matching this clause will (in addition to the normal weightings) have
   * their score multiplied by <code>b</code>.
   */
  public void setBoost(float b) { boost = b; }

  /** Gets the boost for this clause.  Documents matching
   * this clause will (in addition to the normal weightings) have their score
   * multiplied by <code>b</code>.   The boost is 1.0 by default.
   */
  public float getBoost() { return boost; }

  /** Prints a query to a string, with <code>field</code> assumed to be the
   * default field and omitted.
   * <p>The representation used is one that is supposed to be readable
   * by {@link org.apache.lucene.queryParser.QueryParser QueryParser}. However,
   * there are the following limitations:
   * <ul>
   *  <li>If the query was created by the parser, the printed
   *  representation may not be exactly what was parsed. For example,
   *  characters that need to be escaped will be represented without
   *  the required backslash.</li>
   * <li>Some of the more complicated queries (e.g. span queries)
   *  don't have a representation that can be parsed by QueryParser.</li>
   * </ul>
   */
  public abstract String toString(String field);

  /** Prints a query to a string. */
  @Override
  public String toString() {
    return toString("");
  }

  /**
   * Expert: Constructs an appropriate Weight implementation for this query.
   *
   * <p>
   * Only implemented by primitive queries, which re-write to themselves.
   */
  public Weight createWeight(Searcher searcher) throws IOException {
    throw new UnsupportedOperationException("Query " + this + " does not implement createWeight");
  }

  /**
   * Expert: Constructs and initializes a Weight for a <b>top-level</b> query.
   * @deprecated never ever use this method in {@link Weight} implementations.
   * Subclasses of {@code Query} should use {@link #createWeight}, instead.
   */
  @Deprecated
  public final Weight weight(Searcher searcher) throws IOException {
    return searcher.createNormalizedWeight(this);
  }

  /** Expert: called to re-write queries into primitive queries. For example,
   * a PrefixQuery will be rewritten into a BooleanQuery that consists
   * of TermQuerys.
   */
  public Query rewrite(IndexReader reader) throws IOException {
    return this;
  }

  /** Expert: called when re-writing queries under MultiSearcher.
   *
   * Create a single query suitable for use by all subsearchers (in 1-1
   * correspondence with queries). This is an optimization of the OR of
   * all queries. We handle the common optimization cases of equal
   * queries and overlapping clauses of boolean OR queries (as generated
   * by MultiTermQuery.rewrite()).
   * Be careful overriding this method as queries[0] determines which
   * method will be called and is not necessarily of the same type as
   * the other queries.
  */
  public Query combine(Query[] queries) {
    HashSet<Query> uniques = new HashSet<Query>();
    for (int i = 0; i < queries.length; i++) {
      Query query = queries[i];
      BooleanClause[] clauses = null;
      // check if we can split the query into clauses
      boolean splittable = (query instanceof BooleanQuery);
      if(splittable){
        BooleanQuery bq = (BooleanQuery) query;
        splittable = bq.isCoordDisabled();
        clauses = bq.getClauses();
        for (int j = 0; splittable && j < clauses.length; j++) {
          splittable = (clauses[j].getOccur() == BooleanClause.Occur.SHOULD);
        }
      }
      if(splittable){
        for (int j = 0; j < clauses.length; j++) {
          uniques.add(clauses[j].getQuery());
        }
      } else {
        uniques.add(query);
      }
    }
    // optimization: if we have just one query, just return it
    if(uniques.size() == 1){
        return uniques.iterator().next();
    }
    BooleanQuery result = new BooleanQuery(true);
    for (final Query query : uniques)
      result.add(query, BooleanClause.Occur.SHOULD);
    return result;
  }

  /**
   * Expert: adds all terms occurring in this query to the terms set. Only
   * works if this query is in its {@link #rewrite rewritten} form.
   *
   * @throws UnsupportedOperationException if this query is not yet rewritten
   */
  public void extractTerms(Set<Term> terms) {
    // needs to be implemented by query subclasses
    throw new UnsupportedOperationException();
  }

  /** Expert: merges the clauses of a set of BooleanQuery's into a single
   * BooleanQuery.
   *
   *<p>A utility for use by {@link #combine(Query[])} implementations.
   */
  public static Query mergeBooleanQueries(BooleanQuery... queries) {
    HashSet<BooleanClause> allClauses = new HashSet<BooleanClause>();
    for (BooleanQuery booleanQuery : queries) {
      for (BooleanClause clause : booleanQuery) {
        allClauses.add(clause);
      }
    }

    boolean coordDisabled =
      queries.length==0? false : queries[0].isCoordDisabled();
    BooleanQuery result = new BooleanQuery(coordDisabled);
    for(BooleanClause clause2 : allClauses) {
      result.add(clause2);
    }
    return result;
  }

lucene query的更多相关文章

  1. Lucene Query Term Weighting

    方法 public static Query TermWeighting(Query tquery,Map<String,Float>term2weight){ BooleanQuery ...

  2. Lucene Query In Kibana

    1. Terms 一个查询由词条与操作组成.词条可以是单词,或者短语. hello #单独项 "hello pzdn" #双引号引起来短语 2. Field Lucene 支持字段 ...

  3. 使用lucene query的CharFilter 去掉字符中的script脚本和html标签

    1.准备数据,这里我从数据库读取一个带有html标签和script脚本的数据

  4. Lucene 查询工具 LQT

    Lucene Query Tool (lqt) 是一个命令行工具用来执行 Lucene 查询并对结果进行格式化输出. 使用方法: 01 $ ./lqt 02 usage: LuceneQueryToo ...

  5. Query DSL for elasticsearch Query

    Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsea ...

  6. lucene 3.0.2 操作进阶

    转自:Bannings http://blog.csdn.net/zhangao0086/article/details/ Analyzer(分词器) 分词器能以某种规则对关键字进行分词,将分好的词放 ...

  7. lucene 查询的使用

    各种查询方式一:使用QueryParser与查询语法.(会使用分词器) MultiFieldQueryParser查询字符串 ------------------------> Query对象 ...

  8. lucene 区分大小写 问题以及解决方案

    转自:http://blog.csdn.net/huaishu/article/details/8543236 本文介绍lucene区分大小的原因,和解决方案.关于lucene大小写敏感问题我总结一下 ...

  9. lucene、lucene.NET详细使用与优化详解

    lucene.lucene.NET详细使用与优化详解 2010-02-01 13:51:11 分类: Linux 1 lucene简介1.1 什么是luceneLucene是一个全文搜索框架,而不是应 ...

随机推荐

  1. Windows Server 2008 R2 下配置证书服务器和HTTPS方式访问网站

    http://www.cnblogs.com/zhongweiv/archive/2013/01/07/https.html 配置环境 了解HTTPS 配置CA证书服务器 新建示例网站并发布在IIS ...

  2. php 使用curl模拟登录discuz以及模拟发帖

    <?php$discuz_url = 'http://127.0.0.1/discuz/';//论坛地址$login_url = $discuz_url .'logging.php?action ...

  3. ListView的基础应用

    在写完基础的布局之后,下一课我们会学习一下如何使用Android中一个非常重要,但是对于新手略有困难的ListView,甚至很久以前都有人说过,会不会写ListView是Android能否入门的第一步 ...

  4. css使input中的值自动变大写

    <style type="text/css"> input { text-transform:uppercase; } </style>

  5. VR快速发展下,从业者如何把握机会?

      美国科技博客VentureBeat周末刊登赛斯·沙赫纳(Seth Schachner)的文章,分析了在虚拟现实快速发展的情况下,业内所面临的机会,以及如何把握这些机会.   沙赫纳是资深的数字战略 ...

  6. js选中checkbox赋值给文本框

    //js $(function(){ var name=""; var kmname=[]; var n= $("input[type=checkbox]"). ...

  7. Tomcat 的三种(bio,nio.apr) 高级 Connector 运行模式

    tomcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成功,可以看他的启动控制台,或者启动日志.或者登录他们的默认页面http://localhost:8080/查看其中的服务器状态. ...

  8. mybatis-generator 代码自动生成工具(maven方式)

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,mybatis-gennerator插件帮我们自动生成mybatis所需要的 ...

  9. [js高手之路]Node.js模板引擎教程-jade速学与实战4-模板引用,继承,插件使用

    一.block 模块复用 把需要复用的模块用block定义 block后面跟上模块的名字,引用一次block 内容就会被复用一次 编译之后的结果: 二,继承模板(extends) 在实际开发中,网站的 ...

  10. Java 8 特性 —— 默认方法和静态方法

    Java 8 新增了接口的默认方法.简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法.我们只需在方法名前面加个 default 关键字即可实现默认方法. 为什么要有这个特性?之前的 ...