【发布时间】:2010-12-01 04:34:46
【问题描述】:
我正在创建一个可以索引不同文件夹中的许多文本文件的程序。所以这意味着每个包含文本文件的文件夹都会被索引,并且它的索引存储在另一个文件夹中。所以这个另一个文件夹就像我电脑中所有文件的通用索引。我正在使用 lucene 来实现这一点,因为 lucene 完全支持增量更新。这是我用来索引的源代码。
public class SimpleFileIndexer {
public static void main(String[] args) throws Exception {
int i=0;
while(i<2) {
File indexDir = new File("C:/Users/Raden/Documents/myindex");
File dataDir = new File("C:/Users/Raden/Documents/indexthis");
String suffix = "txt";
SimpleFileIndexer indexer = new SimpleFileIndexer();
int numIndex = indexer.index(indexDir, dataDir, suffix);
System.out.println("Total files indexed " + numIndex);
i++;
Thread.sleep(1000);
}
}
private int index(File indexDir, File dataDir, String suffix) throws Exception {
RAMDirectory ramDir = new RAMDirectory(); // 1
@SuppressWarnings("deprecation")
IndexWriter indexWriter = new IndexWriter(
ramDir, // 2
new StandardAnalyzer(Version.LUCENE_CURRENT),
true,
IndexWriter.MaxFieldLength.UNLIMITED);
indexWriter.setUseCompoundFile(false);
indexDirectory(indexWriter, dataDir, suffix);
int numIndexed = indexWriter.maxDoc();
indexWriter.optimize();
indexWriter.close();
Directory.copy(ramDir, FSDirectory.open(indexDir), false); // 3
return numIndexed;
}
private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException {
File[] files = dataDir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(indexWriter, f, suffix);
}
else {
indexFileWithIndexWriter(indexWriter, f, suffix);
}
}
}
private void indexFileWithIndexWriter(IndexWriter indexWriter, File f, String suffix) throws IOException {
if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {
return;
}
if (suffix!=null && !f.getName().endsWith(suffix)) {
return;
}
System.out.println("Indexing file " + f.getCanonicalPath());
Document doc = new Document();
doc.add(new Field("contents", new FileReader(f)));
doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
indexWriter.addDocument(doc);
} }
这是我用于搜索 lucene 创建的索引的源代码
public class SimpleSearcher {
public static void main(String[] args) throws Exception {
File indexDir = new File("C:/Users/Raden/Documents/myindex");
String query = "revolution";
int hits = 100;
SimpleSearcher searcher = new SimpleSearcher();
searcher.searchIndex(indexDir, query, hits);
}
private void searchIndex(File indexDir, String queryStr, int maxHits) throws Exception {
Directory directory = FSDirectory.open(indexDir);
IndexSearcher searcher = new IndexSearcher(directory);
@SuppressWarnings("deprecation")
QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", new StandardAnalyzer(Version.LUCENE_CURRENT));
Query query = parser.parse(queryStr);
TopDocs topDocs = searcher.search(query, maxHits);
ScoreDoc[] hits = topDocs.scoreDocs;
for (int i = 0; i < hits.length; i++) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println(d.get("filename"));
}
System.out.println("Found " + hits.length);
}
}
我现在遇到的问题是我在上面创建的索引程序似乎无法进行任何增量更新。我的意思是我可以搜索一个文本文件,但只能搜索我已经索引到的最后一个文件夹中存在的文件,而我已经索引的另一个以前的文件夹似乎在搜索结果中丢失并且没有显示.你能告诉我我的代码出了什么问题吗?我只是希望能够在我的源代码中具有增量更新功能。所以本质上,我的程序似乎是用新的索引覆盖现有的索引,而不是合并它。
谢谢
【问题讨论】: