- 浏览: 284647 次
- 性别:
- 来自: 湖南岳阳
-
最新评论
-
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的分组统计功能
Lucene-2.3.1 源代码阅读学习
源码下载地址:http://lucene.apache.org
package org.apache.lucene.demo;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
//为指定目录下的所有文件建立索引
public class IndexFiles {
private IndexFiles() {}
static final File INDEX_DIR = new File("index"); //存放建立索引的目录
public static void main(String[] args) {
String usage = "java org.apache.lucene.demo.IndexFiles <root_directory>";
// 如果在DOS下直接输入命令java org.apache.lucene.demo.IndexFiles,而没有指定目录名。
if (args.length == 0) { //args没有接收到任何输入
System.err.println("Usage: " + usage);
System.exit(1);
}
// 如果在DOS下输入命令java org.apache.lucene.demo.IndexFiles myDir,而myDir=index目录已经存在。
if (INDEX_DIR.exists()) {
System.out.println("Cannot save index to '" +INDEX_DIR+ "' directory, please delete it first");
System.exit(1);
}
// 如果在DOS下输入命令java org.apache.lucene.demo.IndexFiles myDir,而myDir目录不存在,则无法创建索引,退出。
final File docDir = new File(args[0]); // 通过输入的第一个参数构造一个File
if (!docDir.exists() || !docDir.canRead()) {
System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path");
System.exit(1);
}
// 如果不存在以上问题,按如下流程执行:
Date start = new Date();
try {
// 通过目录INDEX_DIR构造一个IndexWriter对象
IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true);
System.out.println("Indexing to directory '" +INDEX_DIR+ "'...");
indexDocs(writer, docDir);
System.out.println("Optimizing...");
writer.optimize();
writer.close();
// 计算创建索引文件所需要的时间
Date end = new Date();
System.out.println(end.getTime() - start.getTime() + " total milliseconds");
} catch (IOException e) {
System.out.println(" caught a " + e.getClass() +
"\n with message: " + e.getMessage());
}
}
static void indexDocs(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对象递归索引,通过广度遍历
indexDocs(writer, new File(file, files[i]));
}
}
} else { // 到达叶节点时,说明是一个File,而不是目录,则建立索引
System.out.println("adding " + file);
try {
writer.addDocument(FileDocument.Document(file)); // 通过writer,使用file对象构造一个Document对象,添加到writer中,以便能够通过建立的索引查找到该文件
}
catch (FileNotFoundException fnfe) {
;
}
}
}
}
}
上面是一个简单的Demo,主要使用了org.apache.lucene.index包里面的IndexWriter类。IndexWriter有很多构造方法,这个Demo使用了它的如下的构造方法,使用String类型的目录名作为参数之一构造一个索引器:
public IndexWriter(String path, Analyzer a, boolean create)
throws CorruptIndexException, LockObtainFailedException, IOException {
init(FSDirectory.getDirectory(path), a, create, true, null, true);
}
这里,FSDirectory是文件系统目录,该类的方法都是static的,可以直接方便地获取与文件系统目录相关的一些参数,以及对文件系统目录的操作。FSDirectory类继承自抽象类Directory。
如果想要建立索引,需要从IndexWriter的构造方法开始入手:
可以使用一个File对象构造一个索引器:
public IndexWriter(File path, Analyzer a, boolean create)
throws CorruptIndexException, LockObtainFailedException, IOException {
init(FSDirectory.getDirectory(path), a, create, true, null, true);
}
可以使用一个Directory对象构造:
public IndexWriter(Directory d, Analyzer a, boolean create)
throws CorruptIndexException, LockObtainFailedException, IOException {
init(d, a, create, false, null, true);
}
使用具有两个参数的构造函数老构造索引器,指定一个与文件系统目录有关的参数,和一个分词工具,IndexWriter类提供了3个:
public IndexWriter(String path, Analyzer a)
throws CorruptIndexException, LockObtainFailedException, IOException {
init(FSDirectory.getDirectory(path), a, true, null, true);
}
public IndexWriter(File path, Analyzer a)
throws CorruptIndexException, LockObtainFailedException, IOException {
init(FSDirectory.getDirectory(path), a, true, null, true);
}
public IndexWriter(Directory d, Analyzer a)
throws CorruptIndexException, LockObtainFailedException, IOException {
init(d, a, false, null, true);
}
另外,还有5个构造方法,可以参考源文件IndexWriter类。
Analyzer是一个抽象类,能够对数据源进行分析,过滤,主要功能是进行分词:
package org.apache.lucene.analysis;
java.io.Reader;
public abstract class Analyzer {
public abstract TokenStream tokenStream(String fieldName, Reader reader);
public int getPositionIncrementGap(String fieldName)
{
return 0;
}
}
通过使用StandardAnalyzer类(继承自Analyzer抽象类),构造一个索引器IndexWriter。StandardAnalyzer类,对进行检索的word进行了过滤,因为在检索的过程中,有很多对检索需求没有用处的单词。比如一些英文介词:at、with等等,StandardAnalyzer类对其进行了过滤。看下StandardAnalyzer类的源代码:
package org.apache.lucene.analysis.standard;
import org.apache.lucene.analysis.*;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.Set;
public class StandardAnalyzer extends Analyzer {
private Set stopSet;
// StopAnalyzer类对检索的关键字进行过滤,这些关键字如果以STOP_WORDS数组中指定的word结尾
public static final String[] STOP_WORDS = StopAnalyzer.ENGLISH_STOP_WORDS;
// 构造一个StandardAnalyzer分析器,下面的几个构造函数都是以不同的方式构造一个限制检索关键字结尾字符串的StandardAnalyzer分析器,可以使用默认的,也可以根据自己的需要设置
public StandardAnalyzer() {
this(STOP_WORDS);
}
public StandardAnalyzer(Set stopWords) {
stopSet = stopWords;
}
public StandardAnalyzer(String[] stopWords) {
stopSet = StopFilter.makeStopSet(stopWords);
}
public StandardAnalyzer(File stopwords) throws IOException {
stopSet = WordlistLoader.getWordSet(stopwords);
}
public StandardAnalyzer(Reader stopwords) throws IOException {
stopSet = WordlistLoader.getWordSet(stopwords);
}
看看StopAnalyzer类,它的构造方法和StandardAnalyzer类的很相似,其中默认的ENGLISH_STOP_WORDS指定了下面这些:
public static final String[] ENGLISH_STOP_WORDS = {
"a", "an", "and", "are", "as", "at", "be", "but", "by",
"for", "if", "in", "into", "is", "it",
"no", "not", "of", "on", "or", "such",
"that", "the", "their", "then", "there", "these",
"they", "this", "to", "was", "will", "with"
};
也可以使用带参数的构造函数,根据需要自己指定。
发表评论
-
全文检索的基本原理
2010-02-25 10:22 876一、总论 根据http://lucene.apache.or ... -
lucene 分组 bobo-Browse 排序的问题
2010-02-01 16:18 2223今天碰到了一个问题,用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 944关于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 988关于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 1155关于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支持跨域的身份验证管理,允许用户通过一个中心服务进行一次登录即可访问多个应用系统。 **...
1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问...