簡體   English   中英

計算lucene指數中的詞頻

[英]counting the word frequency in lucene index

有人可以幫我找到所有lucene指數中的單詞頻率
例如,如果文檔A有3個單詞(B),而文檔C有2個單詞,我想要一個返回5的方法,顯示所有lucene索引中單詞(B)的頻率

假設您使用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”)並保留所有其他字段的連接值。

使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM