执行最简单索引过程需要的几个类:IndexWriter、Directory、Analyzer、Document、Field。简单例子如下所示:
public static int index(File indexDir, File dataDir)
throws IOException {
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir
+ " does not exist or is not a directory");
}
if (dataDir.isHidden() || !dataDir.canRead()||!dataDir.getName().endsWith(".txt")) {
throw new IOException(dataDir
+ " error!");
}
IndexWriter writer = new IndexWriter(indexDir,
new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
//使用 IndexWriter's setUseCompoundFile(true) 创建复合文件,减少索引文件数量。
System.out.println("Indexing " + dataDir.getCanonicalPath());
Document doc = new Document();
doc.add(Field.Text("contents", new FileReader(dataDir)));
doc.add(Field.Keyword("filename", dataDir.getCanonicalPath()));
writer.addDocument(doc);
int numIndexed = writer.docCount();
writer.optimize();
writer.close();
return numIndexed;
}
相关推荐
《Lucene In Action》是关于Apache Lucene搜索引擎库的一本权威书籍,第五章主要探讨了高级搜索技术。在这个章节中,作者深入介绍了如何利用Lucene实现更为复杂和精确的搜索功能,以满足不同用户的需求。以下是对这...
- "Lucene in Action"书籍: 一本深入介绍Lucene的书籍,适合初学者和进阶者。 - Stack Overflow: 解决Lucene实际问题的好地方,有很多实战经验分享。 总的来说,Lucene提供了一套强大的文本检索工具,通过理解它的...
随着熟练度的提升,可以阅读更深入的书籍,如《Lucene in Action》等,了解其内部机制和高级特性。 总结来说,Lucene是一个强大的全文检索工具,通过掌握其基本概念和操作,开发者可以构建高效、灵活的搜索功能,为...
提到具体的书籍资源,"Lucene in Action.pdf" 是一本关于Apache Lucene的权威指南。Lucene是一个开源的全文搜索引擎库,广泛应用于各种搜索应用的开发。这本书会详细介绍如何使用Lucene构建高效的搜索引擎,包括索引...
- Lucene是一个高性能的全文搜索引擎库,它能够快速地建立索引并搜索文档。 #### 测试框架Junit - **Junit与Hamcrest兼容性问题**: - 如果遇到`java.lang.SecurityException`错误,可能是由于不同包之间的类...