关于Lucene的一些问题,请高人帮忙,分不够再给!!!
相关代码我在后面给出了,请教三个问题:
第一:判断一个文件的索引是否已经建立;
lucene包已经提供了一个IndexReader.indexExists(File file)的方法,但是我用过多次后发现返回的boolean类型总是false,打印出来看也都存在!我想可能使用方法错了,那怎么才能正确的使用这个方法!
第二:查找一个文件的索引位置;
为了要删除一个索引,必须使用IndexReader.delete(int i)的方法,参数i为索引的位置,我如何才能找到这个索引的位置而删除这个索引,我自己写了一个for循环,但是如果文件多的话效率肯定有问题!
第三:是否提供了修改索引的方法;
要修改索引的话是否有方法可以调用,如果没有的话,我可以根据第二个解决方案删除旧索引,然后再追加一个索引.
谢谢各位高手来解决,分不够再开版给分,绝不食言!!!
import java.io.*;
import java.util.Date;
import org.apache.lucene.index.*;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.document.*;
public class NewIndex {
public static void main(String args[]) throws Exception {
//IndexWriter indexWriter = new IndexWriter("index", new ChineseAnalyzer(), false);
File file = new File("D:\\lucene\\text\\aa.txt");
IndexReader indexReader = IndexReader.open("index");
try {
Date start = new Date();
//if the file has exists then replace, can't add new index files
Document doc;
for (int i=0; i<indexReader.numDocs(); i++) {
doc = indexReader.document(i);
System.out.println(doc.get("path"));
System.out.println(indexReader.indexExists(new File(doc.get("path"))));
//这里返回的总是false
}
System.out.println(indexReader.numDocs());
System.out.println(indexReader.indexExists(file));
System.out.println(indexReader.lastModified(file));
}
catch (Exception e) {
}
finally {
//indexWriter.close();
indexReader.close();
}
}
}