读书人

lucene应用教程3 -常用类的对象

发布时间: 2012-09-10 22:20:12 作者: rapoo

lucene使用教程3 --常用类的对象

你需要以下类来执行这个简单的索引与搜索的过程:

1、IndexWriter

2、IndexSearcher

3、IndexReader

4、Directory

5、Analyzer

6、Document

7、Field

8、Term

9、Query

10、TermQuery

11、Hits

接下来是对这些类的一个简短的浏览,针对它们在Lucene的角色,给出你粗略的概念。接下来我们开始介绍这些类。

IndexWriter类

IndexWriter是在索引过程中的中心组件。这个类创建一个新的索引并且添加文档到一个已有的索引中。你可以把IndexWriter想象成让你可以对索引进行写操作的对象,但是不能让你读取或搜索。此处使用线程同步的方式确保对象的使用高效、安全和减少内存的压力。

以下代码是从网络上粘贴过来的,代码可能会有错误,但是原理都是一样的,明白原理,什么都简单

MyIndexWriter中对IndexWriter的创建和回收:

/** *  * @function 合并索引文件 * @param fromFilePaths 需要合并的索引文件路径 * @param toFilePath 合并完成的索引文件路径 */private static void mergeIndex(String[] fromFilePaths, String toFilePath) {IndexWriter indexWriter = null;File file = null;FSDirectory fsd = null;try {// System.out.println("正在合并索引文件!\t ");indexWriter = MyIndexWriter.getInstance(toFilePath + "/index",false);for (String fromFilePath : fromFilePaths) {file = new File(fromFilePath + "/index");if (file.exists()) {fsd = FSDirectory.open(file);indexWriter.addIndexesNoOptimize(fsd);}}} catch (Exception e) {e.printStackTrace();logger.error(e.getMessage());} finally {try {MyIndexWriter.close();if (fsd != null) {fsd.close();}file = null;} catch (Exception e) {e.printStackTrace();logger.error(e.getMessage());}}}






读书人网 >编程

热点推荐