- 浏览: 272427 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (195)
- EXT学习 (2)
- hibernate (3)
- drools (1)
- TDD测试驱动开发 (3)
- js (7)
- php (3)
- appfuse (2)
- css (5)
- 站长文库 (15)
- flex (6)
- lucene (43)
- 业务建模 (1)
- Pentaho Report Designer (1)
- 代码质量 (10)
- webservice (2)
- 美工 (3)
- dot net (7)
- 人生 (5)
- 方法论 (3)
- html (4)
- 需求管理 (2)
- 资源分享 (2)
- JAVA (6)
- IDE--intelij文章收集 (5)
- 爬虫学习 (1)
- air (2)
- json转换 (1)
- Linux (2)
- C C++ (1)
- mysql word export 导出 (1)
- avast windows server 2003 (3)
- Linux yum (1)
- flash as3 actionscript 错误码 参考 (1)
- actionscript (1)
- 快速开发 (2)
- ios (0)
- erLang (1)
- 手机开发 (1)
- mysql (1)
- 苹果 MacOs (1)
最新评论
-
cuidongdong1234:
有没有源码分析呀?
初步了解jackson -
ieblaze:
您好!我测试了下 ,启动不成警告: Could not get ...
Embed Tomcat 开发,调试项目 -
Feegle7:
楼主,你这个ppt太花了,估计,大家根本没心思看内容了
drools的学习总结 -
filix:
zhoche2008 写道本来写得挺好的。非要搞一些PPT动画 ...
drools的学习总结 -
zhoche2008:
这PPT真耗资源,服了
drools的学习总结
关于前缀查询PrefixQuery(前缀查询)。
准备工作就是为指定的数据源文件建立索引。这里,我使用了ThesaurusAnalyzer分析器,该分析器有自己特定的词库,这个分词组件可以从网上下载。
PrefixQuery其实就是指定一个词条的前缀,不如以前缀“文件”作为前缀的词条有很多:文件系统、文件管理、文件类型等等。但,是在你要检索一个有指定的前缀构成的词条(只有一个前最也是一个词条)时,必须保证你在建立索引的时候,也就是分词生成的词条要有具有这个前缀构成的词条,否则什么也检索不出来。
Lucene中,指定某个前缀,检索过程中会以该前缀作为一个词条进行检索,比如“文件”前缀,如果词条文件中包含“文件”这个词条,而且有一个文件中只有一个句子:“我们要安全地管理好自己的文件。”使用PrefixQuery是也是可以检索出该文件的。
当然了,可以使用BooleanQuery对若干个查询子句进行组合,子句可以是TermQuery子句,可以是PrefixQuery子句,实现复杂查询。
先做个简单的例子,使用一下PrefixQuery。
测试主函数如下所示:
package org.apache.lucene.shirdrn.main;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hit;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
public class PrefixQuerySearcher {
public static void main(String[] args) {
String indexPath = "E:\\Lucene\\myindex";
try {
IndexSearcher searcher = new IndexSearcher(indexPath);
String keywordPrefix = "文件"; // 就以“文件”作为前缀
Term prefixTerm = new Term("contents",keywordPrefix);
Query prefixQuery = new PrefixQuery(prefixTerm);
Date startTime = new Date();
Hits hits = searcher.search(prefixQuery);
Iterator it = hits.iterator();
System.out.println("********************************************************************");
while(it.hasNext()){
Hit hit = (Hit)it.next();
System.out.println("Hit的ID 为 : "+hit.getId());
System.out.println("Hit的score 为 : "+hit.getScore());
System.out.println("Hit的boost 为 : "+hit.getBoost());
System.out.println("Hit的toString 为 : "+hit.toString());
System.out.println("Hit的Dcoment 为 : "+hit.getDocument());
System.out.println("Hit的Dcoment 的 Fields 为 : "+hit.getDocument().getFields());
for(int i=0;i<hit.getDocument().getFields().size();i++){
Field field = (Field)hit.getDocument().getFields().get(i);
System.out.println(" -------------------------------------------------------------");
System.out.println(" Field的Name为 : "+field.name());
System.out.println(" Field的stringValue为 : "+field.stringValue());
}
System.out.println("********************************************************************");
}
System.out.println("满足指定前缀的Hits长度为 : "+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();
}
}
}
测试结果输出如下所示:
********************************************************************
Hit的ID 为 : 41
Hit的score 为 : 0.3409751
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [0] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\Update.txt> stored/uncompressed,indexed<modified:200707050028>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\Update.txt>, stored/uncompressed,indexed<modified:200707050028>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\Update.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200707050028
********************************************************************
Hit的ID 为 : 46
Hit的score 为 : 0.3043366
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [1] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\使用技巧集萃.txt> stored/uncompressed,indexed<modified:200511210413>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\使用技巧集萃.txt>, stored/uncompressed,indexed<modified:200511210413>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\使用技巧集萃.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200511210413
********************************************************************
Hit的ID 为 : 24
Hit的score 为 : 0.25827435
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [2] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\FAQ.txt> stored/uncompressed,indexed<modified:200604130754>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\FAQ.txt>, stored/uncompressed,indexed<modified:200604130754>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\FAQ.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200604130754
********************************************************************
Hit的ID 为 : 44
Hit的score 为 : 0.23094007
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [3] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\Visual Studio 2005注册升级.txt> stored/uncompressed,indexed<modified:200801300512>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\Visual Studio 2005注册升级.txt>, stored/uncompressed,indexed<modified:200801300512>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\Visual Studio 2005注册升级.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200801300512
********************************************************************
Hit的ID 为 : 57
Hit的score 为 : 0.16743648
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [4] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\新建 文本文档.txt> stored/uncompressed,indexed<modified:200710270258>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\新建 文本文档.txt>, stored/uncompressed,indexed<modified:200710270258>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\新建 文本文档.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200710270258
********************************************************************
Hit的ID 为 : 12
Hit的score 为 : 0.14527147
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [5] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\CustomKeyInfo.txt> stored/uncompressed,indexed<modified:200406041814>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\CustomKeyInfo.txt>, stored/uncompressed,indexed<modified:200406041814>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\CustomKeyInfo.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200406041814
********************************************************************
Hit的ID 为 : 63
Hit的score 为 : 0.091877736
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [6] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\疑问即时记录.txt> stored/uncompressed,indexed<modified:200711141408>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\疑问即时记录.txt>, stored/uncompressed,indexed<modified:200711141408>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\疑问即时记录.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200711141408
********************************************************************
Hit的ID 为 : 59
Hit的score 为 : 0.08039302
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [7] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\汉化说明.txt> stored/uncompressed,indexed<modified:200708210247>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\汉化说明.txt>, stored/uncompressed,indexed<modified:200708210247>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\汉化说明.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200708210247
********************************************************************
Hit的ID 为 : 14
Hit的score 为 : 0.020302303
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@1a05308 [8] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\CustomKeysSample.txt> stored/uncompressed,indexed<modified:200610100451>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\CustomKeysSample.txt>, stored/uncompressed,indexed<modified:200610100451>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\CustomKeysSample.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200610100451
********************************************************************
满足指定前缀的Hits长度为 : 9
本次搜索所用的时间为 297 ms
可以看出,包含前缀“文件”的查询结果,一共检索出9项符合条件。
关于以“文件”作为前缀(包含前缀“文件”),在分析器ThesaurusAnalyzer分词组件的词库中具有下面的一些词条:
文件
文件匯編
文件名
文件夹
文件夾
文件尾
文件汇编
文件精神
假如有这样一种需求:想要检索全部以“文件”作为前缀的词条,而不想要单独出现的以“文件”作为词条的结果。
这时,可以指定一个TermQuery子句,再使用BooleanQuery实现。
在上面的测试主函数的基础上,添加如下代码:
String keyword = "文件";
Term term = new Term("contents",keyword);
Query tQuery = new TermQuery(term);
BooleanQuery bQuery = new BooleanQuery();
bQuery.add(tQuery,BooleanClause.Occur.MUST_NOT);
bQuery.add(prefixQuery,BooleanClause.Occur.MUST);
修改Hits hits = searcher.search(prefixQuery);为:
Hits hits = searcher.search(bQuery);
由于不包含单独的以“文件”作为词条的结果,所以使用MUST_NOT逻辑非运算符。
执行查询后,只匹配出一项,如下所示:
********************************************************************
Hit的ID 为 : 44
Hit的score 为 : 0.23393866
Hit的boost 为 : 1.0
Hit的toString 为 : Hit<org.apache.lucene.search.Hits@ab50cd [0] resolved>
Hit的Dcoment 为 : Document<stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\Visual Studio 2005注册升级.txt> stored/uncompressed,indexed<modified:200801300512>>
Hit的Dcoment 的 Fields 为 : [stored/uncompressed,indexed<path:E:\Lucene\txt1\mytxt\Visual Studio 2005注册升级.txt>, stored/uncompressed,indexed<modified:200801300512>]
-------------------------------------------------------------
Field的Name为 : path
Field的stringValue为 : E:\Lucene\txt1\mytxt\Visual Studio 2005注册升级.txt
-------------------------------------------------------------
Field的Name为 : modified
Field的stringValue为 : 200801300512
********************************************************************
满足指定前缀的Hits长度为 : 1
本次搜索所用的时间为 187 ms
现在看一下PrefixQuery实现的源代码。在PrefixQuery中,只给出了一种构造方法:
private Term prefix;
public PrefixQuery(Term prefix) {
this.prefix = prefix;
}
它是通过一个Term作为参数构造的,非常容易掌握。
在PrefixQuery中有一个重要的rewrite()方法:
public Query rewrite(IndexReader reader) throws IOException {
BooleanQuery query = new BooleanQuery(true);
TermEnum enumerator = reader.terms(prefix);
try {
String prefixText = prefix.text();
String prefixField = prefix.field();
do {
Term term = enumerator.term();
if (term != null &&
term.text().startsWith(prefixText) &&
term.field() == prefixField)
{
TermQuery tq = new TermQuery(term);
tq.setBoost(getBoost());
query.add(tq, BooleanClause.Occur.SHOULD); // 构造了一个BooleanQuery,向其中添加子句,个子句是逻辑或运算
} else {
break;
}
} while (enumerator.next());
} finally {
enumerator.close();
}
return query;
}
该方法通过打开一个IndexReader输入流,使用IndexReader的terms()方法获取到,以“给定前缀构造的词条”的所有词条。然后,以返回的这些词条构造多个TermQuery子句,再将这些子句添加到BooleanQuery中,返回一个新的Query(就是BooleanQuery),这个BooleanQuery中的各个子句是逻辑或的关系,最后使用这个包含了多个子句的BooleanQuery实现复杂查询。
实际上,执行了多个TermQuery,然后将得到的结果集做SHOULD运算。
Lucene中,允许最大的子句上限是1024个,如果超过这个上限就会抛出异常。使用PrefixQuery的主要思想就是向一个BooleanQuery中添加多个参与SHOULD逻辑运算的TermQuery子句,感觉这里面有一个效率问题:对每个子句都进行执行的时候,如果子句的数量小效率还是不错,但是,如果有1000000个甚至更多的TermQuery子句被添加到BooleanQuery中,结果不会很乐观,而且需要重新设定Lucene中默认的最大子句上限,效率应该不能很好。
发表评论
-
Lucene-2.2.0 源代码阅读学习(40)
2009-06-04 14:37 1209关于Lucene检索结果的排序问题。 已经知道,Luce ... -
Lucene-2.2.0 源代码阅读学习(39)
2009-06-04 14:35 899关于Lucene得分的计算。 在IndexSearche ... -
Lucene-2.2.0 源代码阅读学习(38)
2009-06-04 14:34 1500关于QueryParser。 QueryPars ... -
Lucene-2.2.0 源代码阅读学习(37)
2009-06-04 14:32 970关于MultiTermQuery查询。 ... -
Lucene-2.2.0 源代码阅读学习(36)
2009-06-04 14:23 1054关于MultiTermQuery查询。 ... -
Lucene-2.2.0 源代码阅读学习(35)
2009-06-04 14:22 763关于MultiPhraseQuery(多短语查询)。 Mul ... -
Lucene-2.2.0 源代码阅读学习(34)
2009-06-04 14:21 1130关于PhraseQuery。 PhraseQuery查询是将 ... -
Lucene-2.2.0 源代码阅读学习(33)
2009-06-04 14:20 819关于范围查询RangeQuery ... -
Lucene-2.2.0 源代码阅读学习(32)
2009-06-04 14:18 1128关于SpanQuery(跨度搜索),它是Query的子类,但是 ... -
Lucene-2.2.0 源代码阅读学习(30)
2009-06-04 14:14 865关于Query的学习。 主要使用TermQuery和Bool ... -
Lucene-2.2.0 源代码阅读学习(29)
2009-06-04 14:12 1051关于IndexSearcher检索器。 ... -
Lucene-2.2.0 源代码阅读学习(28)
2009-06-04 14:09 948关于检索的核心IndexSearcher类。 IndexSe ... -
Lucene-2.2.0 源代码阅读学习(27)
2009-06-04 14:07 871关于Lucene的检索(IndexSearcher)的内容 ... -
Lucene-2.2.0 源代码阅读学习(26)
2009-06-04 14:06 1137如果在初始化一个IndexWr ... -
Lucene-2.2.0 源代码阅读学习(25)
2009-06-04 14:03 875复合索引文件格式(.cfs)是如何产生的?从这个问题出发,研究 ... -
Lucene-2.2.0 源代码阅读学习(24)
2009-06-04 13:58 954阅读了这么多代码, ... -
Lucene-2.2.0 源代码阅读学习(23)
2009-06-04 13:55 830通过对DocumentWriter类的writePosting ... -
Lucene-2.2.0 源代码阅读学习(22)
2009-06-04 13:54 878关于FieldInfos类和FieldInfo类。 Fi ... -
Lucene-2.2.0 源代码阅读学习(21)
2009-06-04 13:53 850回到IndexWriter索引器类 ... -
Lucene-2.2.0 源代码阅读学习(20)
2009-06-04 13:52 859关于Field类和Document类。 初始化一个Index ...
相关推荐
lucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-...
《深入剖析Lucene 2.2.0源代码》 Lucene是一款强大的开源全文搜索引擎库,由Apache软件基金会开发并维护。它为Java开发者提供了一种高性能、可扩展的文本检索核心工具。本文将深入探讨Lucene 2.2.0版本的源代码,...
在前面Lucene-2.2.0 源代码阅读学习(1)中,根据Lucene提供的一个Demo,详细分析研究一下索引器org.apache.lucene.index.IndexWriter类,看看它是如果定义的,掌握它建立索引的机制。 通过IndexWriter类的实现源代码...
2. **Tomcat 安装**: Tomcat 是一个免费的开放源代码的 Web 应用服务器,文中提到使用的是 Tomcat 5.0 版本。同样地,当前的 Tomcat 版本已经更新到了更高的版本,例如 Tomcat 9 或者 10。安装完成后,需要配置 ...
安装过程可以分为两步:首先下载 Atlas2.2.0 的源代码,然后使用 Maven 编译和打包 Atlas2.2.0。 Atlas2.2.0 的配置 Atlas2.2.0 的配置主要包括两个部分:元数据管理层的配置和数据访问层的配置。元数据管理层的...
3. **Compass** (compass-2.2.0.jar): Compass 是一个基于Lucene的搜索引擎库,它使得在Java应用中集成全文搜索引擎变得更加简单。Compass 提供了ORM级别的搜索引擎功能,可以自动同步数据到索引,使得实时搜索成为...
这款软件的开发遵循开源软件的原则,意味着其源代码对公众开放,允许用户查看、修改和分发,促进社区的协作与改进。 在提供的压缩包文件中,我们可以看到以下组件: 1. `makejar.bat`:这是一个批处理文件,通常...