Use TermDocs
to get the term frequency for a given document. Like the document frequency, you get the term documents from an IndexReader
, using the term of interest.
You won't find a faster method than TermDocs
without losing some generality. TermDocs
reads directly from the ".frq" file in an index segment, where each term frequency is listed in document order.
If that's "too slow", make sure that you've optimized your index to merge multiple segments into a single segment. Iterate over the documents in order (skips are alright, but you can't jump back and forth in the document list efficiently).
Your next step might be additional processing to create an even more specialized file structure that leaves out the SkipData
. Personally I would look for a better algorithm to achieve my objective, or provide better hardware—lots of memory, either to hold a RAMDirectory
, or to give to the OS for use on its own file-caching system.