4
尊敬的用户我正在使用apache lucene进行索引和搜索。 我必须索引存储在本地计算机光盘上的html文件。我必须对html文件的文件名和内容进行索引。我能够将文件名存储在lucene索引中,但不能存储html文件内容,该文件内容不仅应该索引数据,还会索引整个页面组成的图像链接和url,以及如何从索引文件 访问内容以索引我使用下面的代码:html文件的lucene索引
File indexDir = new File(indexpath);
File dataDir = new File(datapath);
String suffix = ".htm";
IndexWriter indexWriter = new IndexWriter(
FSDirectory.open(indexDir),
new SimpleAnalyzer(),
true,
IndexWriter.MaxFieldLength.LIMITED);
indexWriter.setUseCompoundFile(false);
indexDirectory(indexWriter, dataDir, suffix);
numIndexed = indexWriter.maxDoc();
indexWriter.optimize();
indexWriter.close();
private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException {
try {
for (File f : dataDir.listFiles()) {
if (f.isDirectory()) {
indexDirectory(indexWriter, f, suffix);
} else {
indexFileWithIndexWriter(indexWriter, f, suffix);
}
}
} catch (Exception ex) {
System.out.println("exception 2 is" + ex);
}
}
private void indexFileWithIndexWriter(IndexWriter indexWriter, File f,
String suffix) throws IOException {
try {
if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {
return;
}
if (suffix != null && !f.getName().endsWith(suffix)) {
return;
}
Document doc = new Document();
doc.add(new Field("contents", new FileReader(f)));
doc.add(new Field("filename", f.getFileName(),
Field.Store.YES, Field.Index.ANALYZED));
indexWriter.addDocument(doc);
} catch (Exception ex) {
System.out.println("exception 4 is" + ex);
}
}
在此先感谢
先生哪里是文字类:
从文件中获得的InputStream整洁无法使用它,我怎么能给文件位置的输入流对象感谢和问候 – adesh
文本类是org.w3c.dom.Text。它带有Java。 –
编辑答案显示如何从文件位置获取输入流 –