学习lucene in action 的例子学习总结

刚开始学习lucene,很多不熟,先买了本lucene原理分析与应用书,很适合自己看这类书,原理及各个类之间的关系讲得很好,但自己写起代码来不咋好使,后来下载lucene in action这本书,慢慢学习并动手搞一下例子。

现在将学习点滴记录下来,便于日后查看 :D
1、环境:MyEclipse6.0+JDK 1.5+lucene 1.43(现在的lucene版本是2.4.0,为了配合lucene in action 中的例子,去下载了个lucene1.4.3 http://download.csdn.net/source/746086)

2、先学一下indexer这个过程
源代码照书抄了遍,哈哈,如下:


/*******************************************************************************
* 该类主要功能及特点:use lucene's indexer Class to index text files
*
* @see(与该类相关的类:)
*
* 开发公司或单位:XX软件有限公司研发中心 版权:本软件版权归属XX公司研发中心
*
* @author (作者):张XX
* @since (该文件所支持的jdk版本):jdk1.5或以上
* @version (版本):1.0
*
* @date ():2009-2-20 最后更改日期:2009-2-20
*
* 修改人:张XX
*
* 复审人:张三,李四,王五
*
*/
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
public class indexer {
public static void main(String[] args) throws Exception {

if (args.length != 2) {
throw new Exception("usage:java " + indexer.class.getName()
+ "<indexfilepath,datefilespath>");
}
// the path of original files
File dataFilesPath = new File(args[0]);
File indexFilesPath = new File(args[1]);
System.out.println("indexFilesPath=" + indexFilesPath);// 由lucene建立产生的索引文件的输出路径
System.out.println("dataFilesPath=" + dataFilesPath);// 这是将被索引的所有文本文件所在目录
Long startTime = new Date().getTime();
int filesNum = index(dataFilesPath, indexFilesPath);
Long endTime = new Date().getTime();
System.out.println("indexing " + filesNum + " files has consumed "
+ (endTime - startTime) + " ms");
}

public static int index(File dataFilesPath, File indexFilePath)
throws IOException {
if (!dataFilesPath.exists() || !dataFilesPath.isDirectory()) {
throw new IOException(dataFilesPath
+ "don't exists or is not a directory");
}
// lucene 2.4.0已将此构造函数deprecated
System.out.println("****************" + indexFilePath);
IndexWriter indexwr = new IndexWriter(indexFilePath,
new StandardAnalyzer(), true);
indexwr.setUseCompoundFile(false);
indexwr.mergeFactor = 2;

// 建立索引
indexDirectory(indexwr, dataFilesPath);
System.out.println("****************" + indexFilePath);
int indexedNum = indexwr.docCount();
indexwr.optimize();
indexwr.close();

return indexedNum;
}

// recursive method that calls itself when it find a directorys;

private static void indexDirectory(IndexWriter writer, File dir)
throws IOException {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
System.out.println(f.getName());
if (f.isDirectory()) {
indexDirectory(writer, f);// recurse
} else if (f.getName().endsWith(".txt")) {

indexFile(writer, f);
}

}

}

private static void indexFile(IndexWriter writer, File f)
throws IOException {

/*
* if (!f.isHidden() || !f.exists() || !f.canRead()) { return; }
*/

System.out.println("it get the file now");

Document doc = new Document();
doc.add(Field.Text("contents", new FileReader(f)));
doc.add(Field.Keyword("filename", f.getCanonicalPath()));
writer.addDocument(doc);
System.out.println(f.getName());

}
}

具体含义lucene in action已讲得很清楚,问题总结:
1)在eclipse中设置argments(本程序是控制台程序测试),Run>Open Run Dialog,在argments中加入c:\data c:\indexfile参数
2)刚开始参数弄反了,运行后程序把data目录下的文件给全删除了(我辛苦下载的软件安装包也全删除,好险啊,没有C盘整个目录),郁闷吧 :wink: ,呵呵,第一次用这个玩意儿。
new IndexWriter(indexFilePath,new StandardAnalyzer(), rue)
;[b]设置为true,会把原索引目录下的文件先全删除再重新建立索引[/b],设置为false则按增量添加索引,要求索引目录至少建立一次相应索引,否则会报找不到相应索引文件异常。
3)
if (!f.isHidden() || !f.exists() || !f.canRead()) { return; }
运行过不了,所以干脆注释掉,主要原因是前面一个条件不对 !f.isHidden(),算了,今天就写到这儿吧
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值