且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

FieldCache与频繁更新的索引

更新时间:2023-12-04 14:46:01

该FieldCache使用索引读者弱引用,作为他们的缓存键。 (通过调用 IndexReader.GetCacheKey 这一直未过时。)与标准调用 IndexReader.Open FSDirectory 将使用池的读者,一个是每一个部分。

The FieldCache uses weak references to index readers as keys for their cache. (By calling IndexReader.GetCacheKey which has been un-obsoleted.) A standard call to IndexReader.Open with a FSDirectory will use a pool of readers, one for every segment.

您应该总是通过最里面的读者的FieldCache。看看 ReaderUtil 对于一些辅助的东西来检索文件包含在单独的阅读器。文档ID不会在段内的变化,它们的含义描述为未predictable时/挥发性的是,它会改变之间的两个指标承诺。删除的文件可能已被proned,部分已被合并,而这种行动。

You should always pass the innermost reader to the FieldCache. Check out ReaderUtil for some helper stuff to retrieve the individual reader a document is contained within. Document ids wont change within a segment, what they mean when describing it as unpredictable/volatile is that it will change between two index commits. Deleted documents could have been proned, segments have been merged, and such actions.

一个承诺需要删除的磁盘段(合并/优化掉),这意味着新的读者不会有汇集段的读者,以及垃圾收集将尽快全部老年读者被关闭删除它。

A commit needs to remove the segment from disk (merged/optimized away), which means that new readers wont have the pooled segment reader, and the garbage collection will remove it as soon as all older readers are closed.

永远,永远,调用 FieldCache.PurgeAllCaches()。它的意思进行测试,而不是生产中使用。

Never, ever, call FieldCache.PurgeAllCaches(). It's meant for testing, not production use.

补充2011-04-03;例如:code。使用subreaders。

Added 2011-04-03; example code using subreaders.

var directory = FSDirectory.Open(new DirectoryInfo("index"));
var reader = IndexReader.Open(directory, readOnly: true);
var documentId = 1337;

// Grab all subreaders.
var subReaders = new List<IndexReader>();
ReaderUtil.GatherSubReaders(subReaders, reader);

// Loop through all subreaders. While subReaderId is higher than the
// maximum document id in the subreader, go to next.
var subReaderId = documentId;
var subReader = subReaders.First(sub => {
    if (sub.MaxDoc() < subReaderId) {
        subReaderId -= sub.MaxDoc();
        return false;
    }

    return true;
});

var values = FieldCache_Fields.DEFAULT.GetInts(subReader, "newsdate");
var value = values[subReaderId];