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

expose document level term positions #1337

Merged
merged 6 commits into from
Aug 4, 2020
Merged
Changes from 1 commit
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
Next Next commit
add get_document_posting
  • Loading branch information
nsndimt committed Jul 27, 2020
commit 3f7892fcf799d7c8c1b4b7f22686ad39d24138ac
43 changes: 43 additions & 0 deletions src/main/java/io/anserini/index/IndexReaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,49 @@ public static Map<String, Long> getDocumentVector(IndexReader reader, String doc
return docVector;
}

/**
* Returns the document posting list for a particular document as a map of terms to term posting list. Note that this
* method explicitly returns {@code null} if the document does not exist (as opposed to an empty map), so that the
* caller is explicitly forced to handle this case.
*
* @param reader index reader
* @param docid collection docid
* @return the document posting list for a particular document as a map of terms to term posting list or {@code null} if
* document does not exist.
* @throws IOException if error encountered during query
* @throws NotStoredException if the term vector is not stored
*/
public static Map<String, List<Long>> getDocumentPostings(IndexReader reader, String docid) throws IOException, NotStoredException {
int ldocid = convertDocidToLuceneDocid(reader, docid);
if (ldocid == -1) {
return null;
}
Terms terms = reader.getTermVector(ldocid, IndexArgs.CONTENTS);
if (terms == null) {
throw new NotStoredException("Document vector not stored!");
}
TermsEnum te = terms.iterator();
if (te == null) {
throw new NotStoredException("Document vector not stored!");
}

Map<String, List<Long>> docPostings = new HashMap<>();
PostingsEnum positions = null;

while ((te.next()) != null) {
List<Long> postings = new ArrayList<>();
Long freq = te.totalTermFreq();
positions = te.postings(positions, PostingsEnum.POSITIONS);
positions.nextDoc();
for ( int i = 0; i < freq; i++ ) {
postings.add(Long.valueOf(positions.nextPosition()));
}
docPostings.put(te.term().utf8ToString(), postings);
}

return docPostings;
}

/**
* Returns the Lucene {@link Document} based on a collection docid. The method is named to be consistent with Lucene's
* {@link IndexReader#document(int)}, contra Java's standard method naming conventions.
Expand Down