Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions rerankers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ def prep_docs(
"'None' doc_ids detected, reverting to auto-generated integer ids..."
)
doc_ids = list(range(len(docs)))
# Write the generated ids back onto the documents (the string-input
# path below already does this); otherwise every returned Document
# keeps doc_id=None and get_score_by_docid/get_result_by_docid break.
for doc, doc_id in zip(docs, doc_ids):
doc.doc_id = doc_id

if metadata is not None:
if docs[0].metadata is not None:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ def test_result_validation_error():
with pytest.raises(ValueError) as excinfo:
Result(document=Document(doc_id=2, text="Doc 2"))
assert "Either score or rank must be provided." in str(excinfo.value)


def test_prep_docs_document_input_autogenerates_ids():
from rerankers.utils import prep_docs

# Document objects with no explicit doc_id must receive auto-generated
# integer ids so get_score_by_docid / get_result_by_docid can resolve them.
docs = [Document(text="a"), Document(text="b"), Document(text="c")]
out = prep_docs(docs)
assert [d.doc_id for d in out] == [0, 1, 2]