现在的位置: 首页 > 综合 > 正文

【Lucene】IndexReader类的maxDoc()与numDocs()的区别

2014年10月15日 ⁄ 综合 ⁄ 共 725字 ⁄ 字号 评论关闭

http://hi.baidu.com/sunky/item/31fdfef9ad1a9d763d198b47

1)Let us see the difference in the comment of  Class IndexReader

  /** Returns the number of documents in this index. */
  public abstract int numDocs();

  /** Returns one greater than the largest possible document number.
   This may be used to, e.g., determine how big to allocate an array which
   will have an element for every document number in an index.
   */
  public abstract int maxDoc();

2) Actually, the real code to implement is the best explanation. The implemetation Class of IndexReader is SegmentReader. Let us see theire implementations as below:

  public final int maxDoc() {
    return fieldsReader.size();
  }

  public final int numDocs() {
    int n = maxDoc();
    if (deletedDocs != null)
      n -= deletedDocs.count();
    return n;
  }

  I think it is easy to understand the reason now !

抱歉!评论已关闭.