我正在尝试索引和存储文件内容(纯文本),但似乎使用这种方式是不可能的:
protected Document getDocument(File f) throws Exception {
Document doc = new Document();
Field contents = new Field("contents", new FileReader(f));
Field filename = new Field("filename", f.getName(), Field.Store.YES, Field.Index.ANALYZED);
doc.add(contents);
return doc;
}
如何存储纯文本文件的内容(没有任何标签)?
答案 0 :(得分:2)
只需阅读文件内容并使用另一个Field构造函数,例如
protected Document getDocument(File f) throws Exception {
Document doc = new Document();
Field contents = new Field("contents", new Scanner(f).useDelimiter("\\A").next(), Store.YES, Index.NO); // you should actually close the scanner
Field filename = new Field("filename", f.getName(), Store.YES, Index.ANALYZED);
doc.add(contents);
doc.add(filename);
return doc;
}
答案 1 :(得分:1)
看看Apache Tika(http://tika.apache.org/)。他们有一个很好的库,用于从HTML和其他结构化文档中提取文本。这将有助于从HTML中提取文本。
至于存储在lucene索引中,根据您的需要,您可以在存储之前去掉标签。或者,您可以使用它创建一个分析器,以便在索引时设置标记。