3

有人可以帮我找到所有 lucene 索引中的词频,
例如,如果 doc A 有 3 个词(B)而 doc C 有 2 个,我想要一种方法来返回 5,显示词的频率(B)在所有 lucene 索引中

4

3 回答 3

9

这已被多次询问:

于 2010-11-12T19:47:40.473 回答
3

假设您使用 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();
}

一些评论:

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

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

于 2010-11-12T19:48:21.050 回答
1

使用 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
}
于 2013-07-17T11:12:27.300 回答