I'm trying to study Lucene 8, this is my first time with Lucene.
I want to have the TF-IDF of each term, in order to obtain the top 10.000 tokens in my Lucene Directory. I've tried in many ways but I'm stuck, I don't know how to proceed. This is an example of what I did:
private static void getTokensForField(IndexReader reader, String fieldName) throws IOException {
List<LeafReaderContext> list = reader.leaves();
Similarity similarity = new ClassicSimilarity();
int docnum = reader.numDocs();
for (LeafReaderContext lrc : list) {
Terms terms = lrc.reader().terms(fieldName);
if (terms != null) {
TermsEnum termsEnum = terms.iterator();
BytesRef term;
while ((term = termsEnum.next()) != null) {
double tf = termsEnum.totalTermFreq() / terms.size();
double idf =Math.log(docnum / termsEnum.docFreq());
// System.out.println(term.utf8ToString() + "\tTF: " + tf + "\tIDF: " + idf);
}
}
}
}
I'm actually studying this topic, but the resources I've found are not really useful.
I've also searched on the Internet, but everything is deprecated.
Do you have some suggestions?
