package lia.meetlucene;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.FileReader;
public class Indexer {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
throw new IllegalArgumentException("Usage: java " + Indexer.class.getName()
+ " <index dir> <data dir>");
}
String indexDir = args[0]; //1
String dataDir = args[1]; //2
long start = System.currentTimeMillis();
Indexer indexer = new Indexer(indexDir);
int numIndexed;
try {
numIndexed = indexer.index(dataDir);
} finally {
indexer.close();
}
long end = System.currentTimeMillis();
System.out.println("Indexing " + numIndexed + " files took "
+ (end - start) + " milliseconds");
}
private IndexWriter writer;
public Indexer(String indexDir) throws IOException {
Directory dir = FSDirectory.open(new File(indexDir));
writer = new IndexWriter(dir, //3
new StandardAnalyzer( //3
Version.LUCENE_30),//3
true, //3
IndexWriter.MaxFieldLength.UNLIMITED); //3
}
public void close() throws IOException {
writer.close(); //4
}
public int index(String dataDir)
throws Exception {
try{
File[] files = new File(dataDir).listFiles();
for (File f: files) {
************************************************
if(f.isDirectory()) // I added this if block which is causing error
{
index(dataDir);
}
************************************************
else if (!f.isDirectory() &&
!f.isHidden() &&
f.exists() &&
f.canRead()
) {
indexFile(f);
}
}
}
catch (IOException e) {
e.printStackTrace();
}
return writer.numDocs(); //5
}
protected Document getDocument(File f) throws Exception {
Document doc = new Document();
doc.add(new Field("contents", new FileReader(f))); //7
doc.add(new Field("filename", f.getName(), //8
Field.Store.YES, Field.Index.NOT_ANALYZED));//8
doc.add(new Field("fullpath", f.getCanonicalPath(), //9
Field.Store.YES, Field.Index.NOT_ANALYZED));//9
return doc;
}
private void indexFile(File f) throws Exception {
System.out.println("Indexing " + f.getCanonicalPath());
Document doc = getDocument(f);
writer.addDocument(doc); //10
}
}
這是給出Lucene的在行動書的程序。它僅索引不在子文件夾中的父文件夾中的文件。所以我添加了一個if塊以遞歸方式查找子文件夾中的文件。但是在運行這個程序之後,它正在創建write.lock文件,並且即使在關閉命令提示符後它也會繼續創建索引文件。代碼有什麼問題?錯誤而索引文件
我是新來的Lucene
和Java
,以前我嘗試過使用apache commons io來查找子文件夾,但是我得到的包不存在錯誤(package org.apache.commons.io does not exist error)。
,你能否告訴我們的錯誤和線造成的錯誤? – Salah
用* – samnaction
@JUBA標出該行不顯示錯誤,繼續運行 – samnaction