Skip to content

Commit

Permalink
Switched to external collection ids for batchGetDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
HAKSOAT committed May 1, 2022
1 parent 9e6a01b commit 54e5baa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
13 changes: 6 additions & 7 deletions src/main/java/io/anserini/search/SimpleSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -682,21 +682,20 @@ public Document document(String docid) {
}

/**
* Returns a map of internal Lucene docid to Lucene {@link Document}.
* Batch version of {@link #document(int)}.
* Returns a map of collection docid to Lucene {@link Document}.
* Batch version of {@link #document(String)}.
*
* @param docids list of docids
* @return a map of docid to corresponding Lucene {@link Document}
*/
public Map<Integer, Document> batchDocument(List<Integer> docids, int threads){
public Map<String, Document> batchGetDocument(List<String> docids, int threads) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads);
ConcurrentHashMap<Integer, Document> results = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Document> results = new ConcurrentHashMap<>();

for (Integer docid: docids) {
for (String docid: docids) {
executor.execute(() -> {
Document result = null;
try {
result = reader.document(docid);
Document result = IndexReaderUtils.document(reader, docid);
results.put(docid, result);
} catch (Exception e){}
});
Expand Down
26 changes: 11 additions & 15 deletions src/test/java/io/anserini/search/SimpleSearcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,17 @@ public void testGetDoc() throws Exception {
public void testBatchGetDoc() throws Exception {
SimpleSearcher searcher = new SimpleSearcher(super.tempDir1.toString());

ArrayList<Integer> ldocIds = new ArrayList<Integer>();
ldocIds.add(0);
ldocIds.add(1);
ldocIds.add(2);
ldocIds.add(3);

Map<Integer, Document> results = searcher.batchDocument(ldocIds, 2);

assertEquals("here is some text here is some more text. city.",
results.get(0).get("contents"));
assertEquals("more texts",
results.get(1).get("contents"));
assertEquals("here is a test",
results.get(2).get("contents"));
assertNull(results.get(3));
ArrayList<String> docIds = new ArrayList<String>();
docIds.add("doc1");
docIds.add("doc2");
docIds.add("doc3");
docIds.add("fake_doc");

Map<String, Document> results = searcher.batchGetDocument(docIds, 2);
assertEquals("here is some text here is some more text. city.", results.get("doc1").get("contents"));
assertEquals("more texts", results.get("doc2").get("contents"));
assertEquals("here is a test", results.get("doc3").get("contents"));
assertNull(results.get("fake_doc"));

searcher.close();
}
Expand Down

0 comments on commit 54e5baa

Please sign in to comment.