Javen-Studio 咖啡小屋

http://javenstudio.org - C++ Java 分布式 搜索引擎
Naven's Research Laboratory - Thinking of Life, Imagination of Future

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  24 随笔 :: 57 文章 :: 170 评论 :: 4 Trackbacks

4           索引是如何创建的

为了使用Lucene来索引数据,首先你比把它转换成一个纯文本(plain-texttokens的数据流(stream),并通过它创建出Document对象,其包含的Fields成员容纳这些文本数据。一旦你准备好些Document对象,你就可以调用IndexWriter类的addDocument(Document)方法来传递这些对象到Lucene并写入索引中。当你做这些的时候,Lucene首先分析(analyzer)这些数据来使得它们更适合索引。详见《Lucene In Action

    // Store the index on disk
    Directory directory = FSDirectory.getDirectory("/tmp/testindex");
    
// Use standard analyzer
    Analyzer analyzer = new StandardAnalyzer(); 
    
// Create IndexWriter object
    IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
    iwriter.setMaxFieldLength(
25000);
    
// make a new, empty document
    Document doc = new Document();
    File f 
= new File("/tmp/test.txt");
    
// Add the path of the file as a field named "path".  Use a field that is 
    
// indexed (i.e. searchable), but don't tokenize the field into words.
    doc.add(new Field("path", f.getPath(), Field.Store.YES, Field.Index.UN_TOKENIZED));
    String text 
= "This is the text to be indexed.";
    doc.add(
new Field("fieldname", text, Field.Store.YES,      Field.Index.TOKENIZED));
    
// Add the last modified date of the file a field named "modified".  Use 
    
// a field that is indexed (i.e. searchable), but don't tokenize the field
    
// into words.
    doc.add(new Field("modified",
        DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE),
        Field.Store.YES, Field.Index.UN_TOKENIZED));
    
// Add the contents of the file to a field named "contents".  Specify a Reader,
    
// so that the text of the file is tokenized and indexed, but not stored.
    
// Note that FileReader expects the file to be in the system's default encoding.
    
// If that's not the case searching for special characters will fail.
    doc.add(new Field("contents"new FileReader(f)));
    iwriter.addDocument(doc);
    iwriter.optimize();
    iwriter.close();

下面详细介绍每一个类的处理机制。

4.1       索引创建类IndexWriter

一个IndexWriter对象创建并且维护(maintains) 一条索引.

它的构造函数(constructor)create参数(argument)确定(determines)是否一条新的索引将被创建,或者是否一条已经存在的索引将被打开。需要注意的是你可以使用create=true参数打开一条索引,即使有其他readers也在在使用这条索引。旧的readers将继续检索它们已经打开的”point in time”快照(snapshot),并不能看见那些新已创建的索引,直到它们再次打开(re-open)。另外还有一个没有create参数的构造函数,如果提供的目录(provided path)中没有已经存在的索引,它将创建它,否则将打开此存在的索引。

另一方面(in either case),添加文档使用addDocument()方法,删除文档使用removeDocument()方法,而且一篇文档可以使用updateDocument()方法来更新(仅仅是先执行delete在执行add操作而已)。当完成了添加、删除、更新文档,应该需要调用close方法。

这些修改会缓存在内存中(buffered in memory),并且定期地(periodically)刷新到(flushDirectory中(在上述方法的调用期间)。一次flush操作会在如下时候触发(triggered):当从上一次flush操作后有足够多缓存的delete操作(参见setMaxBufferedDeleteTerms(int)),或者足够多已添加的文档(参见setMaxBufferedDocs(int)),无论哪个更快些(whichever is sooner)。当一次flush发生时,等待的(pendingdeleteadd文档都会被flush到索引中。一次flush可能触发一个或更多的片断合并(segment merges)。

构造函数中的可选参数(optional argumentautoCommit控制(controls)修改对IndexReader实体(instance)读取相同索引的能见度(visibility)。当设置为false时,修改操作将不可见(visible)直到close()方法被调用后。需要注意的是修改将依然被flushDirectory,就像新文件一样(as new files),但是却不会被提交(commit)(没有新的引用那些新文件的segments_N文件会被写入(written referencing the new files))直道close()方法被调用。如果在调用close()之前发生了某种严重错误(something goes terribly wrong)(例如JVM崩溃了),于是索引将反映(reflect)没有任何修改发生过(none of changes made)(它将保留它开始的状态(remain in its starting state))。你还可以调用close(),这样可以关闭那些没有提交任何修改操作的writers,并且清除所有那些已经flush但是现在不被引用的(unreferenced)索引文件。这个模式(mode)对防止(preventreaders在一个错误的时间重新刷新(refresh)非常有用(例如在你完成所有delete操作后,但是在你完成添加操作前的时候)。它还能被用来实现简单的single-writer的事务语义(transactional semantics)("all or none")。

autoCommit设为true的时候,每次flush也会是一次提交(IndexReader实体将会把每次flush当作一次提交)。这是缺省的设置,目的是为了匹配(match2.2版本之前的行为(behavior)。当以这种模式运行时,当优化(optimize)或者片断合并(segment merges)正在进行(take place)的时候需要小心地重新刷新(refresh)你的readers,因为这两个操作会绑定(tie up)可观的(substantial)磁盘空间。

当一条索引暂时(for a while)将不会有更多的文档被添加,并且期望(desired)得到最理想(optimal)的检索性能(performance),于是optimize()方法应该在索引被关闭之前被调用。

打开IndexWriter会为使用的Directory创建一个lock文件。尝试对相同的Directory打开另一个IndexWriter将会导致(lead to)一个LockObtainFailedException异常。如果一个建立在相同的DirectoryIndexReader对象被用来从这条索引中删除文档的时候,这个异常也会被抛出。

专家(Expert):IndexWriter允许指定(specify)一个可选的(optionalIndexDeletionPolicy实现。你可以通过这个控制什么时候优先的提交(prior commit)从索引中被删除。缺省的策略(policy)是KeepOnlyLastCommitDeletionPolicy类,在一个新的提交完成的时候它会马上所有的优先提交(prior commit)(这匹配2.2版本之前的行为)。创建你自己的策略能够允许你明确地(explicitly)保留以前的”point in time”提交(commit)在索引中存在(alive)一段时间。为了让readers刷新到新的提交,在它们之下没有被删除的旧的提交(without having the old commit deleted out from under them)。这对那些不支持“在最后关闭时才删除”语义(”delete on last close semantics)的文件系统(filesystem)如NFS,而这是Lucene的“point in time”检索通常所依赖的(normally rely on)。

 

 

 

 

 

 

 

Annotated Lucene 作者:naven 日期:2007-5-1

posted on 2007-05-10 00:07 Javen-Studio 阅读(1160) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理