Skip to content

Commit

Permalink
Improved error checking when attempting to fetch doc vector of non-ex…
Browse files Browse the repository at this point in the history
…istent document (#1208)
  • Loading branch information
lintool committed May 18, 2020
1 parent ca231bb commit 33b0684
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/io/anserini/index/IndexReaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,23 @@ public static List<Posting> getPostingsListWithAnalyzer(IndexReader reader, Stri
}

/**
* Returns the document vector for a particular document as a map of terms to term frequencies.
* Returns the document vector for a particular document as a map of terms to term frequencies. 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 vector for a particular document as a map of terms to term frequencies
* @return the document vector for a particular document as a map of terms to term frequencies 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, Long> getDocumentVector(IndexReader reader, String docid) throws IOException, NotStoredException {
Terms terms = reader.getTermVector(convertDocidToLuceneDocid(reader, docid), IndexArgs.CONTENTS);
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!");
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/io/anserini/index/IndexReaderUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ public void testDocumentVector() throws Exception {
assertEquals(Long.valueOf(1), documentVector.get("here"));
assertEquals(Long.valueOf(1), documentVector.get("test"));

// Invalid docid.
assertTrue(IndexReaderUtils.getDocumentVector(reader, "foo") == null);

reader.close();
dir.close();
}
Expand Down

0 comments on commit 33b0684

Please sign in to comment.