lucene in action中文版-第一章-接触Lucene

1.4.1 创建一个索引
在本节中,你将看到一个名为Indexer的类和它的四个静态方法。它们共同递归遍历文件系统目录并索引所有具有.txt扩展名的文件。当Indexer执行完毕时,为它的后续Searcher(在1.4.2小节中介绍)留下一个创建好的lucene索引。
我们不期望你熟悉例子中用到的几个lucene类和方法,我们马上就会解释它们。在有注释的代码列表之后,我们向你展示了如何使用Indexer。如果你感觉在看到编码之前学习Indexer如何使用很有帮助,直接跳到代码后面的用法讨论部分。

使用Indexer来索引文本文件
列表1.1展示了Indexer命令行程序。它用到两个参数:
n 我们存放lucene索引的路径
n 包含我们要索引的文本文件的路径
列表 1.1 Indexer:遍历文件系统并且索引.txt文件
/**
* This code was originally written for
* Erik’s lucene intro java.NET article
*/
public class Indexer {
public static void main(String[] args) throws Exception {
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 Data().getTime();
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 {
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir
+ “ does not exist or is not a directory”);
}
IndexWriter writer = new IndexWriter(indexDir, ① 创建lucene索引
new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
indexDirectory(writer, dataDir);
int numIndexed = writer.docCount();
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;
if (f.isDirectory()) {
indexDirectory(writer, f); ② 递归
} else if (f.getName().endsWith(“.txt”)) {
indexFile(writer, f);
}
}
}
// method to actually index file using lucene
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 doc = new Document();
doc.add(Field.Text(“contents”, new FileReader(f))); ③ 索引文件
内容
doc.add(Field.Keyword(“filename”, f.getCannicalPath()));④ 索引
文件名称
writer.addDocument(doc); ⑤ 添加片段到lucene索引
}
}
有趣的是,代码的大部分是执行目录的遍历(②)。只有IndexWriter的创建和关闭(①)和IndexFile方法中的四行(③,④,⑤)使用了lucene API—有效的6行代码。
这个示例只关注.txt扩展名的文本文件是为了在说明lucene的用法和强大功能时保持尽量简单。在第7章,我们将向你展示如何处理非文本文件,并且我们开发了一个现成的小框架来分析和索引几种常见的格式的文档。
运行Indexer
在命令行中,我们针对包含lucene本身的源文件的本地工作目录运行Indexer。我们使Indexer索引/lucene目录下的文件并将Lucene 索引保存在build/index目录中。
% java lia.meetlucene.Indexer build/index /lucene
Indexing /lucene/build/test/TestDoc/test.txt
Indexing /lucene/build/test/TestDoc/test2.txt
Indexing /lucene/BUILD.txt
Indexing /lucene/CHANGES.txt
Indexing /lucene/LICENSE.txt
Indexing /lucene/README.txt
Indexing /lucene/src/JSP/README.txt
Indexing /lucene/src/test/org/apache/lucene/analysis/ru/
→ stemsUnicode.txt
Indexing /lucene/src/test/org/apache/lucene/analysis/ru/
→ test1251.txt
Indexing /lucene/src/test/org/apache/lucene/analysis/ru/
→ testKOI8.txt
Indexing /lucene/src/test/org/apache/lucene/analysis/ru/
→ testUnicode.txt
Indexing /lucene/src/test/org/apache/lucene/analysis/rn/
→ wordsUnicode.txt
Indexing /lucene/todo.txt
Indexing 13 files took 2205 milliseconds
Indexer打印出索引的文件名称,你可以看出它只索引扩展名为.txt的文本文件。
注意 如果你在Windows平台的命令行中运行这个程序,你需要调整命令行的目录和路径分割符。Windows命令行是java build\index c:\lucene
索引完成后,Indexer输出它索引的文件数目和所花费的时间。因为报告的时间包含文件目录遍历和索引,你不能把它做为一个正式的性能衡量依据。在我们的示例中,每个索引的文件都很小,但只有了2秒索引这些文件还是不错的。
索引速度是要关注的,我们将在第2章中讨论它。但是通常,搜索更加重要

posted on 2008-06-27 22:47  cy163  阅读(1057)  评论(0编辑  收藏  举报

导航