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

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

阅读更多

关于PhraseQuery。

PhraseQuery查询是将多个短语进行合并,得到一个新的词条,从索引库中检索出这个复杂的词条所对应的目标数据文件。

举个例子:假如用户输入关键字“网络安全”,如果索引库中没有单独的“网络安全”这个词条,但是具有“网络”和“安全”这两个词条,我们可以使用PhraseQuery进行查询,将“网络”和“安全”这两个词条合并后能够检索出匹配“网络安全”的所有词条对应的结果集。

现在,使用StandardAnalyzer分析器,对目标数据进行建立索引,也就是,把单独的每个汉字都作为一个词条,存储到索引文件中。可想而知,建立索引花费的时间可能会比较多,因为要对单个汉字进行Tokenizer。

测试程序使用“文件”这个词条,因为使用StandardAnalyzer分析器,索引库中没有词条“文件”,我们使用PhraseQuery来构造实现检索关键字“文件”。

测试主函数如下所示:

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.PhraseQuery;

public class PhraseQuerySearcher {

public static void main(String[] args) {
   String path = "E:\\Lucene\\myindex";
   String keywordA = "文";
   Term termA = new Term("contents",keywordA);
  
   String keywordB = "件";
   Term termB = new Term("contents",keywordB);

// 根据上面搜索关键字构造的两个词条,将它们添加到PhraseQuery中,进行检索
  
   PhraseQuery phraseQuery = new PhraseQuery();
   phraseQuery.add(termA);
   phraseQuery.add(termB);
  
   try {
    Date startTime = new Date();
    IndexSearcher searcher = new IndexSearcher(path);
    Hits hits = searcher.search(phraseQuery);
    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("********************************************************************");
    System.out.println("共检索出符合条件的Document "+hits.length()+" 个。");
    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();
   }

}

}

测试结果如下所示:

Document的内部编号为 : 56
Document的得分为 : 1.0
Document(编号) 56 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\文件.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200804200649
    ------------------------------------
Document的内部编号为 : 41
Document的得分为 : 0.57587546
Document(编号) 41 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\Update.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200707050028
    ------------------------------------
Document的内部编号为 : 46
Document的得分为 : 0.5728219
Document(编号) 46 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\使用技巧集萃.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200511210413
    ------------------------------------
Document的内部编号为 : 24
Document的得分为 : 0.45140085
Document(编号) 24 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\FAQ.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200604130754
    ------------------------------------
Document的内部编号为 : 44
Document的得分为 : 0.4285714
Document(编号) 44 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\Visual Studio 2005注册升级.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200801300512
    ------------------------------------
Document的内部编号为 : 12
Document的得分为 : 0.39528468
Document(编号) 12 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\CustomKeyInfo.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200406041814
    ------------------------------------
Document的内部编号为 : 58
Document的得分为 : 0.33881545
Document(编号) 58 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\新建 文本文档.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200710270258
    ------------------------------------
Document的内部编号为 : 64
Document的得分为 : 0.28571427
Document(编号) 64 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\疑问即时记录.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200711141408
    ------------------------------------
Document的内部编号为 : 60
Document的得分为 : 0.17857142
Document(编号) 60 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\汉化说明.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200708210247
    ------------------------------------
Document的内部编号为 : 14
Document的得分为 : 0.06313453
Document(编号) 14 的Field的信息:
    Field的name : path
    Field的stringValue : E:\Lucene\txt1\mytxt\CustomKeysSample.txt
    ------------------------------------
    Field的name : modified
    Field的stringValue : 200610100451
    ------------------------------------
********************************************************************
共检索出符合条件的Document 10 个。
本次搜索所用的时间为 640 ms

可见一共检索出10个Document满足条件,即10个Document中都存在与词条“文件”匹配的文件,当然是Field的contents。

PhraseQuery仅仅提供了一个构造方法:

   public PhraseQuery() {}

没有参数,没有方法体内容,但是,在使用的时候要用到PhraseQuery的add方法,将由关键字构造的多个词条添加到构造的这个PhraseQuery实例中,实现复杂的检索。

add方法有两个重载的方法,含有一个参数Term的只是把构造的简单词条添加到PhraseQuery中,另一个含有两个参数:

public void add(Term term, int position)

其中,position指定了多个根据用户提交的检索关键字进行分词,分成多个简单的词条,这些词条之间可以存在position个空位,比如用户输入“天地”,如果使用StandardAnalyzer分析器实现后台分词,并且指定了position=1,则目标文件中含有“惊天动地”、“天高地厚”等等词语都能被检索出来。

另外,PhraseQuery还提供了下面方法:

public void setSlop(int s) { slop = s; }

slop 默认值为0,即表示单个简单的词条严格按照顺序组合成新的词条进行检索,亦即:它们之间没有空隙,如果设置为3,表示这些简单的词条之间可以“漏掉”或者“多添”了至多3个无关的字。它与

public void add(Term term, int position)

中的position不同,position是严格按照position个空缺位置检索。

而slop 是>=slop 个空缺都都可以,它可以包含0,1,……,slop-1,是一个空缺长度不同的范围。

分享到:
评论

相关推荐

    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