`
wjboy49
  • 浏览: 284717 次
  • 性别: Icon_minigender_1
  • 来自: 湖南岳阳
社区版块
存档分类
最新评论

Lucene-2.3.1 源代码阅读学习(33)

阅读更多

关于范围查询RangeQuery。

RangeQuery是由两个词条作为上界和下界进行查询,同时指定了一个Boolean型参数,表示是否包括边界,这可以从
RangeQuery的构造方法看到:

    public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive)
    {
        if (lowerTerm == null && upperTerm == null)
        {
            throw new IllegalArgumentException("At least one term must be non-null");
        }
        if (lowerTerm != null && upperTerm != null && lowerTerm.field() != upperTerm.field())
        {
            throw new IllegalArgumentException("Both terms must be for the same field");
        }

        // if we have a lowerTerm, start there. otherwise, start at beginning
        if (lowerTerm != null) {
            this.lowerTerm = lowerTerm;
        }
        else {
            this.lowerTerm = new Term(upperTerm.field(), "");
        }

        this.upperTerm = upperTerm;
        this.inclusive = inclusive;
    }

在构造一个RangeQuery的时候,不能使Term lowerTerm和Term upperTerm都为null,因为这样构造没有意义的。使用RangeQuery对于时间、数字序号等类似特征的词条具有很好的效果,但是作为普通词条意义不大,它会按照字母序来确定搜索范围。

可以指定Term lowerTerm和Term upperTerm中的一个为null,如果指定了Term upperTerm为null,会以Term lowerTerm为上界(即>=),同理,如果Term lowerTerm为null,会以Term upperTerm作为下界(即<=)。

依然使用文章 Lucene-2.2.0 源代码阅读学习(32) 使用的索引文件,建立测试文件,代码如下所示:

package org.apache.lucene.shirdrn.main;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanQuery;

public class RangeQuerySearcher {

public static void main(String[] args) {
   String indexPath = "E:\\Lucene\\index";
   try {
    IndexSearcher searcher = new IndexSearcher(indexPath);
    String keywordA = "这些";
    Term termA = new Term("contents",keywordA);
   
    String keywordB = "辩论";
    Term termB = new Term("contents",keywordB);
   
   RangeQuery rangeQuery = new RangeQuery(termA,termA,true);
   
    Date startTime = new Date();
    Hits hits = searcher.search(rangeQuery);
    for(int i=0;i<hits.length();i++){
     System.out.println("Document的内部编号为 : "+hits.id(i));
     Document doc = hits.doc(i);
     System.out.println("Document的得分为 : "+hits.score(i));
     List fieldList = doc.getFields();
     System.out.println("Document(编号) "+hits.id(i)+" 的Field的信息: ");
     for(int j=0;j<fieldList.size();j++){
      Field field = (Field)fieldList.get(j);
      System.out.println("    Field的name : "+field.name());
      System.out.println("    Field的stringValue : "+field.stringValue());
      System.out.println("    ------------------------------------");
     }
    }
    System.out.println("********************************************************************");
    Date finishTime = new Date();
    long timeOfSearch = finishTime.getTime() - startTime.getTime();
    System.out.println("本次搜索所用的时间为 "+timeOfSearch+" ms");
   
   } catch (CorruptIndexException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
}
}

看上面构造RangeQuery的时候,使用的是同一个Term作为上界和下界,而且inclusive指定为true,这时其实检索的就是termA,结果如下所示:

Document的内部编号为 : 1
Document的得分为 : 0.35136628
Document(编号) 1 的Field的信息:
    Field的name : contents
    Field的stringValue : 谁知道宇宙空间的奥秘,在我们这些人当中?
    ------------------------------------
    Field的name : contents
    Field的stringValue : 宇宙飞船。
    ------------------------------------
    Field的name : contents
    Field的stringValue : 我们的太空宇宙。
    ------------------------------------
********************************************************************
本次搜索所用的时间为 93 ms

含有词条“这些”的Document只有编号为1的满足条件。如果上面程序中inclusive指定为false,表示检索的termA作为上界和下界的开区间,很容易想到,检索的检索一定是空集。

如果修改RangeQuery的构造如下:

RangeQuery rangeQuery = new RangeQuery(termA,termB,false );

查询结果还是空集,要知道:“这些”在字母排序时要在“辩论”之后,而且取的是开区间。

如果修改为:

RangeQuery rangeQuery = new RangeQuery(termA,termB,true );

则只是对两个边界进行检索,结果可能会存在,我的测试结果如下所示:

Document的内部编号为 : 1
Document的得分为 : 0.35136628
Document(编号) 1 的Field的信息:
    Field的name : contents
    Field的stringValue : 谁知道宇宙空间的奥秘,在我们这些人当中?
    ------------------------------------
    Field的name : contents
    Field的stringValue : 宇宙飞船。
    ------------------------------------
    Field的name : contents
    Field的stringValue : 我们的太空宇宙。
    ------------------------------------
********************************************************************
本次搜索所用的时间为 93 ms

分享到:
评论

相关推荐

    Lucene-2.3.1 源代码阅读学习

    《Lucene-2.3.1 源代码阅读学习》 Lucene是Apache软件基金会的一个开放源码项目,它是一个高性能、全文本搜索库,为开发者提供了在Java应用程序中实现全文检索功能的基础架构。本篇文章将深入探讨Lucene 2.3.1版本...

    lucene2.3.1

    总而言之,Lucene 2.3.1作为一款经典的搜索引擎框架,它的源代码不仅提供了学习信息检索理论的机会,也是实践和掌握Java编程、数据结构和算法的宝贵资源。通过对压缩包中的文件进行分析,开发者可以深入了解Lucene的...

    Lucene.Net2.3源码,最新版

    通过深入学习和理解这些源代码文件,开发者可以更好地掌握 Lucene.Net 的核心功能,如索引构建、查询解析、搜索排序、分词和性能优化。这有助于在实际项目中实现高效、精确的全文搜索引擎。同时,研究源码也能提升对...

    Baioogle-SearchEngine(百歌搜索引擎)

    4.其中src文件夹内为全部源代码,WebRoot为web应用部署文件 5.本系统的最小有效组件集合为:(约定:以下“*.*”均表示目录下的所有单独文件,不包括文件夹,而“/s”则表示所有的文件夹及其内部内容) src\*.* /s ...

    一个专业搜索公司关于lucene+solar资料(1)

    ### Lucene+Solor知识点概述 #### 一、搜索引擎基础理论 **1.1 Google神话** - **起源与发展:** - Google成立于1998年,由Larry Page和Sergey Brin创立。 - 初期以PageRank算法为核心,有效解决了当时互联网...

    JeeSite开发说明文档

    - **开源协议**:使用Apache License 2.0协议,源代码完全开源,没有商业限制。 - **技术栈成熟**:使用当前最主流的J2EE开发框架和技术,易于学习和维护。 - **数据库支持广泛**:支持多种数据库,如MySQL、Oracle...

    solr教材-PDF版

    - **1.4.1 目录结构说明**:Solr项目的目录结构清晰,主要包括src/main/java下的源代码、src/main/resources下的资源文件等。 - **1.4.2 Solrhome说明**:Solrhome是Solr实例的工作目录,包含了索引数据、配置文件等...

    Solr3.5开发应用指导

    - **1.4.1 目录结构说明**:Solr的核心源码主要由几个关键部分组成,如`src/main/java`包含Java源代码,`src/main/resources`存放配置文件等。 - **1.4.2 Solrhome说明**:Solrhome是Solr运行时使用的根目录,包含了...

    lifery6.1+cas初始化环境搭建及门户解决方案

    CAS (Central Authentication Service) 是一种开放源代码的单点登录协议和服务实现,主要用于Web应用的安全身份验证。CAS支持跨域的身份验证管理,允许用户通过一个中心服务进行一次登录即可访问多个应用系统。 **...

    ZendFramework中文文档

    2.3.1. 保存 ACL 数据确保持久性 2.3.2. 使用声明(Assert)来编写条件性的 ACL 规则 3. Zend_Auth 3.1. 简介 3.1.1. 适配器 3.1.2. 结果 3.1.3. 身份的持久(Persistence) 3.1.3.1. 在PHP Session 中的缺省...

Global site tag (gtag.js) - Google Analytics