计算lucene指数中的词频

时间:2010-11-12 18:14:26

标签: search lucene word-frequency

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

3 个答案:

答案 0 :(得分:9)

答案 1 :(得分: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 class的实例。它的创建因RAM和文件系统索引而异,有关详细信息,请参阅Lucene文档。

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

答案 2 :(得分:1)

使用lucene 3.4

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

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

要注意:如果你用于读取,你就不能再使用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
}