Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counts unique terms if not available in Lucene #2053

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/main/java/io/anserini/index/IndexReaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,20 @@ public static String convertLuceneDocidToDocid(IndexReader reader, int docid) {
}
}

// Internal helper: counts the number of unique terms
private static long getUniqueTerms(IndexReader reader) throws IOException {
Terms terms = MultiTerms.getTerms(reader, Constants.CONTENTS);
long UniqueTerms = terms.size();
if (UniqueTerms == -1) { // terms.size() may not be available in Lucene
UniqueTerms = 0;
TermsEnum it = terms.iterator();
while (it.next() != null) {
UniqueTerms += 1;
}
}
return UniqueTerms;
}

/**
* Returns index statistics.
*
Expand All @@ -786,7 +800,7 @@ public static Map<String, Object> getIndexStats(IndexReader reader) {

indexStats.put("documents", reader.numDocs());
indexStats.put("non_empty_documents", reader.getDocCount(Constants.CONTENTS));
indexStats.put("unique_terms", terms.size());
indexStats.put("unique_terms", getUniqueTerms(reader));
indexStats.put("total_terms", reader.getSumTotalTermFreq(Constants.CONTENTS));
} catch (IOException e) {
// Eat any exceptions and just return null.
Expand Down