大家用的最多的就是IndexReader上的open,其实在lucene里有DirectoryIndexerReader,关系如下:
abstract class DirectoryIndexReader extends IndexReader{}
其中定义的几个方法,请注意2.4和2.3.2的open方法的差别:
2.3.2:
static DirectoryIndexReader open(final Directory directory, final boolean closeDirectory, final IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, IOException
2.4.0:
static DirectoryIndexReader open(final Directory directory, final boolean closeDirectory, final IndexDeletionPolicy deletionPolicy, final IndexCommit commit, final boolean readOnly) throws CorruptIndexException, IOException
实际上在2.4上赋予了Directory一个io核心的概念,我们可以看到所有的io操作最终都会转换成directory上的操作,因此才会在directory上赋予了commit和只读概念.
我们来看两个函数的变化
1 init:
在2.4中增加了如下的代码:
if (!readOnly && segmentInfos != null) {
// We assume that this segments_N was previously
// properly sync'd:
for(int i=0;i<segmentInfos.size();i++) {
final SegmentInfo info = segmentInfos.info(i);
List files = info.files();
for(int j=0;j<files.size();j++)
synced.add(files.get(j));
}
}
这段新加的代码在directory上增加了区段信息,区段信息可以做两件事情:
1 索引版本
2 是否优化
因此在reopen中2.4的代码得到了很多的改进,我曾经说过2.3里的reopen是个垃圾,因为它把所有的判断扔还给了开发人员,这是不合理的,下面看看reopen的新变化:
public final synchronized IndexReader reopen() throws CorruptIndexException, IOException {
ensureOpen();
if (this.hasChanges || this.isCurrent()) {
// this has changes, therefore we have the lock and don't need to reopen
这个应该就是针对changes中的:
LUCENE-1228: IndexWriter.commit() was not updating the index version and as result IndexReader.reopen() failed to sense index changes.
(Doron Cohen)
// OR: the index in the directory hasn't changed - nothing to do here
return this;
}
return (DirectoryIndexReader) new SegmentInfos.FindSegmentsFile(directory) {
protected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {
SegmentInfos infos = new SegmentInfos();
infos.read(directory, segmentFileName);
DirectoryIndexReader newReader = doReopen(infos);
if (DirectoryIndexReader.this != newReader) {
newReader.init(directory, infos, closeDirectory, readOnly);
newReader.deletionPolicy = deletionPolicy;
}
return newReader;
}
}.run();
}
最后看看open:
SegmentInfos.FindSegmentsFile finder = new SegmentInfos.FindSegmentsFile(directory) {
protected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {
SegmentInfos infos = new SegmentInfos();
infos.read(directory, segmentFileName);
DirectoryIndexReader reader;
if (infos.size() == 1) { // index is optimized
reader = SegmentReader.get(readOnly, infos, infos.info(0), closeDirectory);
} else if (readOnly) {
reader = new ReadOnlyMultiSegmentReader(directory, infos, closeDirectory);
} else {
原来代码中一旦没有优化就会使用多区段reader但是现在增加了ReadOnly版本,应该是commit策略有关的玩意,稍后我们来看看这东西好了. reader = new MultiSegmentReader(directory, infos, closeDirectory, false);
}
reader.setDeletionPolicy(deletionPolicy);
return reader;
}
};
if (commit == null)
return (DirectoryIndexReader) finder.run();
else {
if (directory != commit.getDirectory())
throw new IOException("the specified commit does not match the specified Directory");
// This can & will directly throw IOException if the
// specified commit point has been deleted:
return (DirectoryIndexReader) finder.doBody(commit.getSegmentsFileName());
}
commit上 2.4中已经废止了原来的commitPoint类,改成了IndexCommit,作用类似主要方法定义如下:
* Returns all index files referenced by this commit point.
*/
public abstract Collection getFileNames() throws IOException;
/**
* Returns the {@link Directory} for the index.
*/
public abstract Directory getDirectory();
增加如上的两个成员之后使得对于文件控制直接挂载到了directory,正像我说的2.4开始选择Directory为核心io处理了,呵呵.
最后来看看我们的readonlyMutiSegmentReader
class ReadOnlyMultiSegmentReader extends MultiSegmentReader {
ReadOnlyMultiSegmentReader(Directory directory, SegmentInfos sis, boolean closeDirectory) throws IOException {
super(directory, sis, closeDirectory, true);
}
ReadOnlyMultiSegmentReader(Directory directory, SegmentInfos infos, boolean closeDirectory, SegmentReader[] oldReaders, int[] oldStarts, Map oldNormsCache) throws IOException {
super(directory, infos, closeDirectory, oldReaders, oldStarts, oldNormsCache, true);
}
protected void acquireWriteLock() {
ReadOnlySegmentReader.noWrite();
}
}
看来花头不多,只是改变了默认参数后需要增加这样一个类加以判断而已.今天就这些吧,下次继续...
分享到:
相关推荐
Lucene2.4入门总结lucene2.4 人门 field 多条件搜索
java拼车网雏形(Ext2.0+SSH+oracle10g+lucene2.4)
lucene 2.4 jar lucene2.4版本的JAR包
能够直接运行的Lucene2.4样例以及全面注释; + Lucene中文文档,能够帮助你最快掌握Lucene2.4
ictclas4j for lucene 2.4 任何人不得将此用于商业用途,仅限个人学习研究之用.该开源项目遵循Apache License 2.0
庖丁解牛 源码 for Lucene 2.4
Luene2.4版本在以前的基础上进行了不少的改动,性能上也提升了不少,这里边学习边写了一个简单的例子,给初学者行个方便!
struts2 + spring2.5 + hibernate 3.2 + lucene 2.4 + compass 2.0 包含所有jar包,按readme.txt导入并运行即可 开始不用分了................
lucene2.4手册,是开发搜索引擎的好帮手.
NULL 博文链接:https://chenhua-1984.iteye.com/blog/380779
Lucene2.4.1的源代码,包括core包和demos包的内容。
支持net4.0环境下运行,Lucene.net版本为3.0,PanGu版本为2.4
lucene-core-2.4.0.jar,lucene-analyzer-2.4.0.jar,lucene-highlighter-2.4.0.jar,je-analysis-1.5.3.jar
基于lucene 2.4简单的一个索引和搜索实例
might not be compatible with the Snowball module in Lucene 2.4 or greater. For more information about this issue see: https://issues.apache.org/jira/browse/LUCENE-1142 For more information on ...
lucene.net用于在.net平台中开发搜索引擎。使得.net开发全文搜索变得很方便。
lucene-2.3.1.jar.7z
lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例
lucene,lucene教程,lucene讲解。 为了对文档进行索引,Lucene 提供了五个基础的类 public class IndexWriter org.apache.lucene.index.IndexWriter public abstract class Directory org.apache.lucene.store....