分类: LINUX
2008-10-18 11:07:58
package test;
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 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建索引文件和要索引文件所在的目录
File indexDir = new File(Constants.INDEX_STORE_PATH);//调用Constants中的常量
File dataDir = new File(Constants.INDEX_FILE_PATH);
//获取建立索引开始的时间
long start =new Date().getTime();
int numIndexed = 0;
try {
numIndexed = index(indexDir,dataDir);返回要索引文件的数量
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = new Date().getTime();
System.out.print("Indexing"+ numIndexed + "files took"
+ (end - start) + "milliseconds");//计算整个索引过程所需要的时间
}
private static int index(File indexDir, File dataDir) throws IOException{
// TODO Auto-generated method stub
if(!dataDir.exists() || !dataDir.isDirectory())
{
throw new IOException(dataDir + "does not exit or" +
"is not a directory");
}
//第一个参数是索引文件存放目录,第二个参数是分析器(可选),第三个参数确定是否
覆盖原有索引,建立此对象对索引进行写操作。
IndexWriter writer = new IndexWriter(indexDir,new StandardAnalyzer(),true);
writer.setUseCompoundFile(false);//使用复合文件?
indexDirectory(writer,dataDir);
int numIndexed = writer.docCount();
writer.optimize();//进行优化
writer.close();//关闭writer对象
return numIndexed;
}
//对文件夹和文件查找遍历。
private static void indexDirectory(IndexWriter writer, File dataDir)
throws IOException{
// TODO Auto-generated method stub
File[] files = dataDir.listFiles();
for(int i = 0;i
{
File f = files[i];
if(f.isDirectory())
{
indexDirectory(writer,f);
}
else if(f.getName().endsWith(".txt"))
{
indexFile(writer,f);
}
}
}
private static void indexFile(IndexWriter writer, File f) throws IOException{
// TODO Auto-generated method stub
if(f.isHidden() || !f.exists() || !f.canRead())
{
return;
}
System.out.println("Indexing" + f.getCanonicalPath());
//最主要的部分
Document doc = new Document();
Field fieldContents = new Field("contents", new FileReader(f));//得到文本的内容域
doc.add(fieldContents);
Field fieldName = new Field("name",f.getCanonicalPath(),Field.Store.YES,Field.Index.UN_TOKENIZED);//得到文本的
路径名称,不同的内容会有不同的操作方法(四种)
doc.add(fieldName);
writer.addDocument(doc);
}
}