lucene-1.4入门例子的详细注释

[size=small]才开始学习lucene,所以从网上找了个不错的入门例子来看,并且加了很详细的注释,假如有不对的地方,还请大家指正:
Indexer.java
// 索引生成类
public class Indexer {
// 传入2个参数,一个是索引文件路径,一个是数据文件路径
public static void main(String[] args) throws Exception {
// 如果没有传入2个文件,则抛出异常
if (args.length != 2) {
throw new Exception("Usage: java " + Indexer.class.getName()
+ " <index dir> <data dir>");
}
// 读取索引文件
File indexDir = new File(args[0]);
// 读取数据文件
File dataDir = new File(args[1]);
long start = new Date().getTime();
// 调用index方法生成索引
int numIndexed = index(indexDir, dataDir);
long end = new Date().getTime();
// 打印文件数目和花费时间
System.out.println("Indexing " + numIndexed + " files took "
+ (end - start) + " milliseconds");
}

// open an index and start file directory traversal
public static int index(File indexDir, File dataDir) throws IOException {
// 如果2个文件有一个找不到,则抛出异常
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir
+ " does not exist or is not a directory");
}
// IndexWriter: 创建和维护索引的类
// StandardAnalyzer: 分析器,筛选出无用的词语
// true: 是否重建索引. false: 扩展索引
IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
true);
// true:在创建索引库时,会合并多个 Segments 文件到一个 .cfs 中。此方式有助于减少索引文件数量,减少同时打开的文件数量。
writer.setUseCompoundFile(false);
// 读取数据文件
indexDirectory(writer, dataDir);
// 得到当前索引文件中的文件数量
int numIndexed = writer.docCount();
/*
* IndexWriter为了减少大量的io维护操作,在每得到一定量的索引后建立新的小索引文件(笔者测试索引批量的最小单位为10),
* 然后再定期将它们整合到一个索引文件中,因此在索引结束时必须进行 writer.optimize(),以便将所有索引合并优化。
*/
writer.optimize();
// 关闭对象
writer.close();
// 返回索引文件的文件数量
return numIndexed;
}

// recursive method that calls itself when it finds a directory
// 读取数据文件,假如是目录则遍历
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];
// 假如读入的是目录,那么递归调用自己
if (f.isDirectory()) {
indexDirectory(writer, f);
// 假如读入的是txt文件,读取它
} else if (f.getName().endsWith(".txt")) {
indexFile(writer, f);
}
}
}

// method to actually index file using Lucene
// 将一个文件读取到IndexWriter对象中去
private static void indexFile(IndexWriter writer, File f)
throws IOException {
// 假如准备读取的数据文件为隐藏,不存在,不能读,则返回不进行操作
if (f.isHidden() || !f.exists() || !f.canRead()) {
return;
}
// 打印出数据文件的路径
System.out.println("Indexing " + f.getCanonicalPath());
// Document:类似一条数据库中的一条数据
// Field:类似字段
Document doc = new Document();
// Text(String,Reader):本方法适用于简短的文本,如title,subject,String可以被检索,但是不可以从检索结果中得到
doc.add(Field.Text("contents", new FileReader(f)));
// Keyword(String,String):本方法适用在date和url上,String可以被检索,也可以从检索结果中得到
doc.add(Field.Keyword("filename", f.getCanonicalPath()));
// 索引添加一个文档
writer.addDocument(doc);
}
}


Searcher.java
// 检索类
public class Searcher {
// 传入2个参数,目录文件名和检索关键字
public static void main(String[] args) throws Exception {
if (args.length != 2) {
throw new Exception("Usage: java " + Searcher.class.getName()
+ " <index dir> <auery>");
}
File indexDir = new File(args[0]);
String q = args[1];
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir
+ " does not exist or is not a directory.");
}
search(indexDir, q);
}

public static void search(File indexDir, String q) throws Exception {
// 将目录文件的目录封装成对象
Directory fsDir = FSDirectory.getDirectory(indexDir, false);
// 创建查询器,在提供的目录上查找索引文件
IndexSearcher is = new IndexSearcher(fsDir);
// QueryParser.parse():1.检索的关键字;2.检索的范围,常用的是subject,text,contents,title等等;3.分析器
Query query = QueryParser.parse(q, "contents", new StandardAnalyzer());
long start = new Date().getTime();
// 使用查询器进行查询,Hit是结果集
Hits hits = is.search(query);
long end = new Date().getTime();
System.err.println("Found " + hits.length() + " document(s) (in "
+ (end - start) + " milliseconds) that matched query ‘" + q
+ "’:");
// 遍历hit结果集
for (int i = 0; i < hits.length(); i++) {
// 返回第i个的文档的所有字段
Document doc = hits.doc(i);
System.out.println(doc.get("filename"));
}
}
}


[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值