package com.dyhx.archive.lucene;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Indexer {
private static File indexDir = new File(Constants.INDEX_DIR);
private static File dataDir = new File(Constants.DATA_DIR);
public static void main(String[] args) throws Exception {
int numIndexed = index();
System.out.println("Indexing " + numIndexed + " files...");
}
@SuppressWarnings("deprecation")
public static int index() throws IOException {
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir
+ " does not exist or is not a directory");
}
IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir),
new StandardAnalyzer(Version.LUCENE_33), true,
IndexWriter.MaxFieldLength.LIMITED);
indexDirectory(indexWriter, dataDir);
int numIndexed = indexWriter.numDocs();
indexWriter.optimize();
indexWriter.close();
return numIndexed;
}
public 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); // directory iterator
} else {
String[] exts = Constants.EXTS.split(",");
for (int j = 0; j < exts.length; j++) {
if (f.getName().endsWith("." + exts[j])) {
indexFile(writer, f);
}
}
}
}
}
private static void indexFile(IndexWriter writer, File file)
throws IOException {
if (file.isHidden() || !file.exists() || !file.canRead()) {
return;
}
Document doc = new Document();
doc.add(new Field("name", file.getName().substring(0, file.getName().lastIndexOf(".")), Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("path", file.getCanonicalPath(), Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("parent", file.getParent().substring(file.getParent().lastIndexOf("\\") + 1), Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("content", new FileReader(file)));
writer.addDocument(doc);
}
}
Java多级多类型全文检索 - 基于Lucene3.3.0
5星 · 超过95%的资源 需积分: 9 186 浏览量
2011-09-20
13:55:45
上传
评论
收藏 1.14MB 7Z 举报
ZeroMitizvah
- 粉丝: 0
- 资源: 14
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈