- 浏览: 284663 次
- 性别:
- 来自: 湖南岳阳
最新评论
-
ternus:
兄弟,我用boboBrowse 也遇到了排序的问题,上线了讨论 ...
lucene 分组 bobo-Browse 排序的问题 -
luli0822:
Awesome bookmarks of those guru ...
流行的jQuery信息提示插件(jQuery Tooltip Plugin) -
shenbai:
如果你要在前台运行,你应该run得是ElasticSearch ...
ElasticSearch 源码分析 环境入门 -
cl1154781231:
<s:peroperty value="#at ...
关于Struts2中标签的一些心得 -
RonQi:
转载的吗?http://blog.csdn.net/stray ...
利用bobo-browse 实现lucene的分组统计功能
建立索引,通过已经生成的索引文件,实现通过关键字检索。
写了一个类MySearchEngine,根据上述思想实现,把Lucene自带的递归建立索引的方法提取出来,加了一个搜索的方法:
package org.shirdrn.lucene;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.demo.FileDocument;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.LockObtainFailedException;
public class MySearchEngine {
private File file;
private String indexPath;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getIndexPath() {
return indexPath;
}
public void setIndexPath(String indexPath) {
this.indexPath = indexPath;
}
public void createIndex(IndexWriter writer, File file) throws IOException {
// file可以读取
if (file.canRead()) {
if (file.isDirectory()) { // 如果file是一个目录(该目录下面可能有文件、目录文件、空文件三种情况)
String[] files = file.list(); // 获取file目录下的所有文件(包括目录文件)File对象,放到数组files里
// 如果files!=null
if (files != null) {
for (int i = 0; i < files.length; i++) { // 对files数组里面的File对象递归索引,通过广度遍历
createIndex(writer, new File(file, files[i]));
}
}
}
else { // 到达叶节点时,说明是一个File,而不是目录,则为该文件
建立索引
try {
writer.addDocument(FileDocument.Document(file));
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
}
}
public void searchContent(String type,String keyword){ // 根据指定的检索内容类型type,以及检索关键字keyword进行检索操作
try {
IndexSearcher searcher = new IndexSearcher(this.indexPath); // 根据指定路径,构造一个IndexSearcher检索器
Term term = new Term(type,keyword); // 创建词条
Query query = new TermQuery(term); // 创建查询
Date startTime = new Date();
TermDocs termDocs = searcher.getIndexReader().termDocs(term); // 执行检索操作
while(termDocs.next()){ // 遍历输出根据指定词条检索的结果信息
System.out.println("搜索的该关键字【"+keyword+"】在文件\n"+searcher.getIndexReader().document(termDocs.doc()));
System.out.println("中,出现过 "+termDocs.freq()+" 次");
}
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();
}
}
}
这里引用了import org.apache.lucene.demo.FileDocument,在创建Field的时候,为每个Field都设置了三种属性:path、modified、contents。在检索的时候,只要指定其中的一个就可以从索引中检索出来。
public static void main(String[] args){
MySearchEngine mySearcher = new MySearchEngine();
String indexPath = "E:\\Lucene\\myindex";
File file = new File("E:\\Lucene\\txt");
mySearcher.setIndexPath(indexPath);
mySearcher.setFile(file);
IndexWriter writer;
try {
writer = new IndexWriter(mySearcher.getIndexPath(),new StandardAnalyzer(),true);
mySearcher.createIndex(writer, mySearcher.getFile());
mySearcher.searchContent("contents","server");
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
在构造IndexWriter的时候,选择分词器不同,对检索的结果有很大的影响。
我感觉,这里自带的StandardAnalyzer和SimpleAnalyzer对中文的支持不是很好,因为它是对像英文这样的,以空格作为分隔符的,中文不同英文的习惯,可能有时候根本检索不出中文。
使用StandardAnalyzer和SimpleAnalyzer的时候,检索关键字“server“,结果是相同的,结果如下所示:
搜索的该关键字【server】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\license.txt> stored/uncompressed,indexed<modified:200106301125>>
中,出现过 2 次
搜索的该关键字【server】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Patch.txt> stored/uncompressed,indexed<modified:200112160745>>
中,出现过 8 次
搜索的该关键字【server】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\REDIST.TXT> stored/uncompressed,indexed<modified:200511120152>>
中,出现过 1 次
搜索的该关键字【server】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\新建 文本文档.txt> stored/uncompressed,indexed<modified:200710270258>>
中,出现过 27 次
本次搜索所用的时间为 0 ms
但是,如果使用StandardAnalyzer检索中文,mySearcher.searchContent("contents","的");,结果可以看出来:
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\120E升级包安装说明.txt> stored/uncompressed,indexed<modified:200803101357>>
中,出现过 2 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\1实验题目.txt> stored/uncompressed,indexed<modified:200710160733>>
中,出现过 1 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\3实验题目.txt> stored/uncompressed,indexed<modified:200710300744>>
中,出现过 2 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\CustomKeyInfo.txt> stored/uncompressed,indexed<modified:200406041814>>
中,出现过 80 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\CustomKeysSample.txt> stored/uncompressed,indexed<modified:200610100451>>
中,出现过 8 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\FAQ.txt> stored/uncompressed,indexed<modified:200604130754>>
中,出现过 348 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\MyEclipse 注册码.txt> stored/uncompressed,indexed<modified:200712061059>>
中,出现过 5 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\readme.txt> stored/uncompressed,indexed<modified:200803101314>>
中,出现过 17 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Struts之AddressBooks学习笔记.txt> stored/uncompressed,indexed<modified:200710131711>>
中,出现过 8 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Update.txt> stored/uncompressed,indexed<modified:200707050028>>
中,出现过 177 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Visual Studio 2005注册升级.txt> stored/uncompressed,indexed<modified:200801300512>>
中,出现过 3 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\书籍网站.txt> stored/uncompressed,indexed<modified:200708071255>>
中,出现过 3 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\使用技巧集萃.txt> stored/uncompressed,indexed<modified:200511210413>>
中,出现过 153 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\关系记录.txt> stored/uncompressed,indexed<modified:200802201145>>
中,出现过 24 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\剑心补丁使用说明(readme).txt> stored/uncompressed,indexed<modified:200803101357>>
中,出现过 17 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\史上最强天籁之声及欧美流行曲超级精选【 FLAC分轨】.txt> stored/uncompressed,indexed<modified:200712231241>>
中,出现过 1 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\密码强度检验.txt> stored/uncompressed,indexed<modified:200712010901>>
中,出现过 1 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\新1建 文本文档.txt> stored/uncompressed,indexed<modified:200710311142>>
中,出现过 39 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\新建 文本文档.txt> stored/uncompressed,indexed<modified:200710270258>>
中,出现过 4 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\汉化说明.txt> stored/uncompressed,indexed<modified:200708210247>>
中,出现过 16 次
本次搜索所用的时间为 16 ms
使用SimpleAnalyzer的时候,检索中文关键字,结果很不准确。mySearcher.searchContent("contents","的");,结果可以看出来:
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\Update.txt> stored/uncompressed,indexed<modified:200707050028>>
中,出现过 1 次
搜索的该关键字【的】在文件
Document<stored/uncompressed,indexed<path:E:\Lucene\txt\使用技巧集萃.txt> stored/uncompressed,indexed<modified:200511210413>>
中,出现过 1 次
本次搜索所用的时间为 0 ms
现在感觉到了,分词在建立索引的时候是一件多么重要的事情啊。
发表评论
-
全文检索的基本原理
2010-02-25 10:22 876一、总论 根据http://lucene.apache.or ... -
lucene 分组 bobo-Browse 排序的问题
2010-02-01 16:18 2224今天碰到了一个问题,用bobo分组后对价格升序 居然100 ... -
开源搜索引擎
2010-02-01 14:31 1695开放源代码搜索引擎为 ... -
lucene中的filter器群组及其缓存大盘点
2010-01-20 23:18 1202lucene中的filter其实并不起眼,大家对其对性能的影响 ... -
利用bobo-browse 实现lucene的分组统计功能
2010-01-18 17:50 2940bobo-browse 是一用java写的lucene扩展组件 ... -
lucene Field部分参数设置含义
2009-11-07 17:51 1249<script type="text/ja ... -
刚下载,开始学习lucene时看的文章
2009-09-04 18:43 1431Lucene 2.0.0下载安装及测试 【下载】 下载链接 ... -
Lucene-2.3.1 阅读学习(42)
2009-09-04 18:42 945关于Hits类。 这个Hits类 ... -
Lucene 2.3.1 阅读学习(41)
2009-09-04 18:42 1412当执行Hits htis = search(query);这一 ... -
Lucene-2.3.1 源代码阅读学习(40)
2009-09-04 18:41 989关于Lucene检索结果的排序问题。 已经知道,Lucene ... -
Lucene-2.3.1 源代码阅读学习(39)
2009-09-04 18:41 1155关于Lucene得分的计算。 在IndexSearcher类 ... -
Lucene-2.3.1 源代码阅读学习(39)
2009-09-04 18:38 565关于Lucene得分的计算。 在IndexSearcher类 ... -
Lucene-2.3.1 源代码阅读学习(38)
2009-09-04 18:38 922关于QueryParser。 QueryParser是用来解 ... -
Lucene-2.3.1 源代码阅读学习(37)
2009-09-04 18:37 630关于MultiTermQuery查询。 这里研究继承自Mul ... -
Lucene-2.3.1 源代码阅读学习(36)
2009-09-04 18:37 808关于MultiTermQuery查询。 ... -
Lucene-2.3.1 源代码阅读学习(35)
2009-09-04 18:36 842关于MultiPhraseQuery(多短语查询)。 Mul ... -
Lucene-2.3.1 源代码阅读学习(34)
2009-09-04 18:36 640关于PhraseQuery。 PhraseQuery查询是将 ... -
Lucene-2.3.1 源代码阅读学习(33)
2009-09-04 18:35 870关于范围查询RangeQuery。 ... -
Lucene-2.3.1 源代码阅读学习(32)
2009-09-04 18:35 1156关于SpanQuery(跨度搜索),它是Query的子类,但是 ... -
Lucene-2.3.1 源代码阅读学习(31)
2009-09-04 18:34 873关于前缀查询PrefixQuery(前缀查询)。 准备工作就 ...
相关推荐
《Lucene-2.3.1 源代码阅读学习》 Lucene是Apache软件基金会的一个开放源码项目,它是一个高性能、全文本搜索库,为开发者提供了在Java应用程序中实现全文检索功能的基础架构。本篇文章将深入探讨Lucene 2.3.1版本...
总而言之,Lucene 2.3.1作为一款经典的搜索引擎框架,它的源代码不仅提供了学习信息检索理论的机会,也是实践和掌握Java编程、数据结构和算法的宝贵资源。通过对压缩包中的文件进行分析,开发者可以深入了解Lucene的...
通过深入学习和理解这些源代码文件,开发者可以更好地掌握 Lucene.Net 的核心功能,如索引构建、查询解析、搜索排序、分词和性能优化。这有助于在实际项目中实现高效、精确的全文搜索引擎。同时,研究源码也能提升对...
4.其中src文件夹内为全部源代码,WebRoot为web应用部署文件 5.本系统的最小有效组件集合为:(约定:以下“*.*”均表示目录下的所有单独文件,不包括文件夹,而“/s”则表示所有的文件夹及其内部内容) src\*.* /s ...
### Lucene+Solor知识点概述 #### 一、搜索引擎基础理论 **1.1 Google神话** - **起源与发展:** - Google成立于1998年,由Larry Page和Sergey Brin创立。 - 初期以PageRank算法为核心,有效解决了当时互联网...
- **开源协议**:使用Apache License 2.0协议,源代码完全开源,没有商业限制。 - **技术栈成熟**:使用当前最主流的J2EE开发框架和技术,易于学习和维护。 - **数据库支持广泛**:支持多种数据库,如MySQL、Oracle...
- **1.4.1 目录结构说明**:Solr项目的目录结构清晰,主要包括src/main/java下的源代码、src/main/resources下的资源文件等。 - **1.4.2 Solrhome说明**:Solrhome是Solr实例的工作目录,包含了索引数据、配置文件等...
- **1.4.1 目录结构说明**:Solr的核心源码主要由几个关键部分组成,如`src/main/java`包含Java源代码,`src/main/resources`存放配置文件等。 - **1.4.2 Solrhome说明**:Solrhome是Solr运行时使用的根目录,包含了...
CAS (Central Authentication Service) 是一种开放源代码的单点登录协议和服务实现,主要用于Web应用的安全身份验证。CAS支持跨域的身份验证管理,允许用户通过一个中心服务进行一次登录即可访问多个应用系统。 **...
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 中的缺省...