Lucene 8.7.0 版本中docFreq、totalTermFreq、getDocCount等方法的含义

8.7.0 版本lucene中,TermsEnum类有如下几个方法

java 复制代码
/** Returns the number of documents containing the current

term. Do not call this when the enum is unpositioned.
{@link SeekStatus#END}.*/
public abstract int docFreq() throws IOException;

/** Returns the total number of occurrences of this term
across all documents (the sum of the freq() for each
doc that has this term). Note that, like
other term measures, this measure does not take
deleted documents into account. */
public abstract long totalTermFreq() throws IOException;

Terms类有如下几个方法

java 复制代码
/** Returns the sum of {@link TermsEnum#totalTermFreq} for
all terms in this field. Note that, just like other term
measures, this measure does not take deleted documents
into account. */
public abstract long getSumTotalTermFreq() throws IOException;

/** Returns the sum of {@link TermsEnum#docFreq()} for
all terms in this field. Note that, just like other term
measures, this measure does not take deleted documents
into account. */
public abstract long getSumDocFreq() throws IOException;

/** Returns the number of documents that have at least one
term for this field. Note that, just like other term
measures, this measure does not take deleted documents
into account. */
public abstract int getDocCount() throws IOException;

他们有啥区别?


这几个 Lucene 方法确实有重要区别,让我通过例子来说明:

假设有以下文档集合:

复制代码
Doc1: "apple apple banana"
Doc2: "apple orange"  
Doc3: "banana orange orange"
Doc4: "grape"

各方法的区别:

1. docFreq() - 单个词项的文档频率

  • 含义:当前词项出现在多少个文档中
  • 示例
    • "apple" 的 docFreq = 2(出现在 Doc1, Doc2)
    • "orange" 的 docFreq = 2(出现在 Doc2, Doc3)
    • "grape" 的 docFreq = 1(仅在 Doc4)

2. totalTermFreq() - 单个词项的总频率

  • 含义:当前词项在所有文档中出现的总次数
  • 示例
    • "apple" 的 totalTermFreq = 3(Doc1中2次 + Doc2中1次)
    • "orange" 的 totalTermFreq = 3(Doc2中1次 + Doc3中2次)
    • "grape" 的 totalTermFreq = 1

3. getSumTotalTermFreq() - 字段中所有词项的总频率

  • 含义:该字段中所有词项出现次数的总和
  • 示例 :整个字段的 sumTotalTermFreq = 8
    • apple(3) + banana(2) + orange(3) + grape(1) = 9

4. getSumDocFreq() - 字段中所有词项的文档频率总和

  • 含义:所有词项的 docFreq 之和
  • 示例 :sumDocFreq = 7
    • apple(2) + banana(2) + orange(2) + grape(1) = 7

5. getDocCount() - 包含该字段的文档数

  • 含义:有多少文档至少包含该字段的一个词项
  • 示例:docCount = 4(所有4个文档都有内容)

实际应用场景:

java 复制代码
// 示例代码
Terms terms = reader.terms("content");
TermsEnum termsEnum = terms.iterator();

// 遍历某个词项
if (termsEnum.seekExact(new BytesRef("apple"))) {
    // 针对 "apple" 这个词
    int df = termsEnum.docFreq();        // 2 (在2个文档中)
    long tf = termsEnum.totalTermFreq(); // 3 (总共出现3次)
}

// 字段级统计
long sumTF = terms.getSumTotalTermFreq(); // 所有词的总频率
long sumDF = terms.getSumDocFreq();       // 所有词的文档频率和
int docCount = terms.getDocCount();       // 包含此字段的文档数

关键区别总结:

方法 作用域 统计内容
docFreq() 单个词项 文档数量
totalTermFreq() 单个词项 出现总次数
getSumTotalTermFreq() 整个字段 所有词项的总出现次数
getSumDocFreq() 整个字段 所有词项的文档频率之和
getDocCount() 整个字段 包含该字段的文档数

这些统计信息在计算 TF-IDF、BM25 等相关性评分时非常重要。

相关推荐
sunxunyong15 天前
ranger与solr&ldap&doris集成部署
solr·lucene
编码者卢布16 天前
【Azure AI Search】Index的字段使用默认Analyzer(standard.lucene) 和 en.microsoft 有什么不同?
microsoft·lucene·azure
2601_9618752419 天前
法考资料电子版|pdf|资料已整理
elasticsearch·搜索引擎·pdf·全文检索·solr·lucene·sphinx
2601_9618454219 天前
考研公共课资料推荐|英语数学政治|电子版|资料已整理
搜索引擎·中文分词·solr·lucene·sphinx·高考
解决问题no解决代码问题19 天前
漏洞详解|CVE-2026-44825 Apache Solr 隐藏默认账号漏洞(附检测+修复全套方案)
apache·solr·lucene
2601_9618454221 天前
高考真题下载|2025高考全科真题网盘分类整理
搜索引擎·中文分词·solr·lucene·sphinx·高考
2601_9611940225 天前
教资科三美术考什么|初中高中美术题型考点和模板资料
leetcode·elasticsearch·职场和发展·蓝桥杯·pat考试·lucene
2601_961194021 个月前
考研模拟卷谁的比较好|27李林合工大肖四肖八数学英语408PDF
考研·elasticsearch·全文检索·代理模式·lucene·桥接模式·访问者模式
醉颜凉1 个月前
Lucene底层原理:倒排索引实现原理与代码实战,彻底吃透搜索引擎核心
搜索引擎·mybatis·lucene
risc1234561 个月前
DocumentsWriterFlushQueue
lucene