【问题标题】:counting the word frequency in lucene index计算lucene索引中的词频
【发布时间】:2010-11-12 18:14:25
【问题描述】:

谁能帮我找到所有lucene索引中的词频
例如,如果文档 A 有 3 个单词 (B) 而文档 C 有 2 个,我想要一个返回 5 的方法,显示所有 lucene 索引中单词 (B) 的频率

【问题讨论】:

  • 你在看什么样的索引大小?取决于您可能想要考虑使用 Hadoop 来执行此操作,或者使用简单的索引解析器来收集地图中的词频。

标签: search lucene word-frequency


【解决方案1】:
【解决方案2】:

假设您使用 Lucene 3.x:

IndexReader ir = IndexReader.open(dir); 
TermDocs termDocs = ir.termDocs(new Term("your_field", "your_word"));
int count = 0;
while (termDocs.next()) {
   count += termDocs.freq();
}

一些cmets:

dir 是 Lucene Directory class 的实例。 RAM 和文件系统索引的创建不同,有关详细信息,请参阅 Lucene 文档。

"your_filed" 是用于搜索术语的字段。如果您有多个字段,您可以为所有字段运行程序,或者,当您索引文件时,您可以创建特殊字段(例如“_content”)并保留所有其他字段的连接值。

【讨论】:

  • 太糟糕了TermDocs 不在我使用的 lucene 5.3.1 中:(
【解决方案3】:

使用 lucene 3.4

获取计数的简单方法,但您需要两个数组:-/

int[] docs = new int[1000];
int[] freqs = new int[1000];
int count = indexReader.termDocs(term).read(docs, freqs);

注意:如果您使用 for read,您将无法再使用 next(),因为在 read() 之后,您已经处于枚举的末尾:

int[] docs = new int[1000];
int[] freqs = new int[1000];
TermDocs td = indexReader.termDocs(term);
int count = td.read(docs, freqs);
while (td.next()){ // always false, already at the end of the enumartion
}

【讨论】:

    猜你喜欢
    • 2018-05-23
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多