Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions haystack/components/joiners/document_joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ def run(self, documents: Variadic[list[Document]], top_k: int | None = None) ->
"score, so those with score=None were sorted as if they had a score of -infinity."
)

if top_k:
if top_k is not None:
output_documents = output_documents[:top_k]
elif self.top_k:
elif self.top_k is not None:
output_documents = output_documents[: self.top_k]

return {"documents": output_documents}
Expand Down
9 changes: 9 additions & 0 deletions test/components/joiners/test_document_joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ def test_run_with_top_k_in_run_method(self):
output = joiner.run([documents_1, documents_2], top_k=top_k)
assert len(output["documents"]) == top_k

def test_run_with_top_k_zero_in_run_method_overrides_init_top_k(self):
# A run-time top_k=0 must be honored (return no documents), not treated as "unset"
# and fall back to the instance's top_k.
joiner = DocumentJoiner(top_k=5)
documents_1 = [Document(content="a"), Document(content="b"), Document(content="c")]
documents_2 = [Document(content="d"), Document(content="e"), Document(content="f")]
output = joiner.run([documents_1, documents_2], top_k=0)
assert len(output["documents"]) == 0

def test_sort_by_score_without_scores(self, caplog):
joiner = DocumentJoiner()
with caplog.at_level(logging.INFO):
Expand Down