Skip to content
Merged
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
3 changes: 3 additions & 0 deletions haystack/document_stores/in_memory/document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ def delete_documents(self, document_ids: list[str]) -> None:
doc_len = doc_stats.doc_len

self._freq_vocab_for_idf.subtract(Counter(freq.keys()))
for token in freq:
if self._freq_vocab_for_idf[token] <= 0:
del self._freq_vocab_for_idf[token]
try:
self._avg_doc_len = (self._avg_doc_len * (len(self._bm25_attr) + 1) - doc_len) / len(self._bm25_attr)
except ZeroDivisionError:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fixes:
- |
Remove zero-frequency vocabulary entries after deleting documents from
``InMemoryDocumentStore`` so previous document history no longer changes
BM25Okapi scores for the active corpus.
20 changes: 20 additions & 0 deletions test/document_stores/test_in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,26 @@ def test_bm25_avg_doc_len_after_delete(self, in_memory_doc_store):
# After removing "hello world" (2 tokens), only "foo bar baz" (3 tokens) remains
assert in_memory_doc_store._avg_doc_len == pytest.approx(3.0)

def test_bm25_okapi_scores_do_not_depend_on_deleted_documents(self):
active_documents = [
Document(id="d1", content="common alpha"),
Document(id="d2", content="common beta"),
Document(id="d3", content="common gamma"),
]
fresh_store = InMemoryDocumentStore(bm25_algorithm="BM25Okapi", shared=False)
reused_store = InMemoryDocumentStore(bm25_algorithm="BM25Okapi", shared=False)
deleted_document = Document(content="one two three four five six seven eight nine ten")

reused_store.write_documents([deleted_document])
reused_store.delete_documents([deleted_document.id])
fresh_store.write_documents(active_documents)
reused_store.write_documents(active_documents)

fresh_scores = {doc.id: doc.score for doc in fresh_store.bm25_retrieval(query="common", top_k=3)}
reused_scores = {doc.id: doc.score for doc in reused_store.bm25_retrieval(query="common", top_k=3)}

assert reused_scores == pytest.approx(fresh_scores)


class TestMemoryDocumentStoreNotShared(TestMemoryDocumentStore):
"""
Expand Down
Loading