faiss-haystack 3.0.0__tar.gz → 3.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,29 @@
1
1
  # Changelog
2
2
 
3
+ ## [integrations/faiss-v3.0.1] - 2026-11-09
4
+
5
+ ### 🐛 Bug Fixes
6
+
7
+ - *(faiss)* Don't leave orphaned vectors when a batch repeats a document ID (#4180)
8
+
9
+
10
+ ## [integrations/faiss-v3.0.0] - 2026-10-16
11
+
12
+ ### 🧪 Testing
13
+
14
+ - Use Python 3.11 in tests instead of 3.10 (#4103)
15
+ - Test compatible integrations with Python 3.15; update pyproject (#4104)
16
+
17
+ ### ⚙️ CI
18
+
19
+ - Improve changelog generation; fix existing changelogs (#3883)
20
+
21
+ ### 🧹 Chores
22
+
23
+ - Switch remaining document stores to haystack.logging (#4011)
24
+ - [**breaking**] Drop Python 3.10 support (#4105)
25
+
26
+
3
27
  ## [integrations/faiss-v2.1.0] - 2026-09-01
4
28
 
5
29
  ### 🐛 Bug Fixes
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: faiss-haystack
3
- Version: 3.0.0
3
+ Version: 3.1.0
4
4
  Summary: An integration of FAISS (library for efficient similarity search of dense vectors) with Haystack
5
5
  Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/faiss#readme
6
6
  Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
@@ -152,9 +152,9 @@ class FAISSDocumentStore:
152
152
  msg = f"Document with id '{doc.id}' already exists."
153
153
  raise DuplicateDocumentError(msg)
154
154
 
155
- # Process documents
156
- ids_to_add_to_index = []
157
- vectors_to_add = []
155
+ # Process documents. Pending vectors are keyed by document ID so that a later duplicate in the same
156
+ # batch replaces the earlier one instead of leaving an orphaned vector in the FAISS index.
157
+ pending_vectors: dict[str, tuple[int, list[float]]] = {}
158
158
 
159
159
  docs_written = 0
160
160
 
@@ -168,6 +168,7 @@ class FAISSDocumentStore:
168
168
  # FAISS doesn't support easy update. We'd have to remove and add.
169
169
  # For MVP, let's implement remove then add for overwrite.
170
170
  self.delete_documents([doc.id])
171
+ pending_vectors.pop(doc.id, None)
171
172
 
172
173
  self.documents[doc.id] = doc
173
174
 
@@ -179,15 +180,14 @@ class FAISSDocumentStore:
179
180
  self.id_map[int_id] = doc.id
180
181
  self.inverse_id_map[doc.id] = int_id
181
182
 
182
- ids_to_add_to_index.append(int_id)
183
- vectors_to_add.append(doc.embedding)
183
+ pending_vectors[doc.id] = (int_id, doc.embedding)
184
184
 
185
185
  docs_written += 1
186
186
 
187
187
  # Add to FAISS
188
- if vectors_to_add:
189
- vectors = np.array(vectors_to_add, dtype="float32")
190
- ids = np.array(ids_to_add_to_index, dtype="int64")
188
+ if pending_vectors:
189
+ vectors = np.array([vector for _, vector in pending_vectors.values()], dtype="float32")
190
+ ids = np.array([int_id for int_id, _ in pending_vectors.values()], dtype="int64")
191
191
  index = self._get_index_or_raise()
192
192
  index.add_with_ids(vectors, ids)
193
193
 
@@ -246,41 +246,43 @@ class FAISSDocumentStore:
246
246
  # Ensure embedding format
247
247
  query_vec = np.array([query_embedding], dtype="float32")
248
248
 
249
- # Search in FAISS
250
- # Valid strategy for pre-filtering vs post-filtering:
251
- # Since FAISS `IndexIDMap` doesn't support pre-filtering natively comfortably
252
- # without `RangeSearch` or specialized impls, we fetch more and filter post-retrieval.
253
-
249
+ # Filters are checked in Python on the documents, so they are applied to the FAISS hits (post-filtering).
250
+ # If too few of the fetched neighbours match the filters, search once more over the whole index, so matching
251
+ # documents further away are not missed. One full search is cheaper than growing k step by step: exhaustive
252
+ # indexes like "Flat" compare against every vector whatever k is, and each retry re-filters the same hits.
254
253
  fetch_k = top_k
255
254
  if filters:
256
- fetch_k = min(self.index.ntotal, top_k * 10) # Simple heuristic
255
+ fetch_k = min(self.index.ntotal, top_k * 10)
257
256
 
258
- distances, indices = self.index.search(query_vec, fetch_k)
257
+ while True:
258
+ distances, indices = self.index.search(query_vec, fetch_k)
259
259
 
260
- results = []
261
- for dist, int_id in zip(distances[0], indices[0], strict=False):
262
- if int_id == -1:
263
- continue
260
+ results = []
261
+ for dist, int_id in zip(distances[0], indices[0], strict=False):
262
+ if int_id == -1:
263
+ continue
264
264
 
265
- doc_id = self.id_map.get(int_id)
266
- if not doc_id or doc_id not in self.documents:
267
- continue
265
+ doc_id = self.id_map.get(int_id)
266
+ if not doc_id or doc_id not in self.documents:
267
+ continue
268
268
 
269
- doc = self.documents[doc_id]
269
+ doc = self.documents[doc_id]
270
270
 
271
- if filters and not self._matches_filters(doc, filters):
272
- continue
271
+ if filters and not self._matches_filters(doc, filters):
272
+ continue
273
273
 
274
- # Build a new instance instead of mutating score in place.
275
- score = float(1 / (1 + dist)) if self.index_string == "Flat" else float(dist)
276
- result_doc = replace(doc, score=score)
274
+ # Build a new instance instead of mutating score in place.
275
+ score = float(1 / (1 + dist)) if self.index_string == "Flat" else float(dist)
276
+ result_doc = replace(doc, score=score)
277
277
 
278
- results.append(result_doc)
278
+ results.append(result_doc)
279
279
 
280
- if len(results) >= top_k:
281
- break
280
+ if len(results) >= top_k:
281
+ break
282
282
 
283
- return results
283
+ if not filters or len(results) >= top_k or fetch_k >= self.index.ntotal:
284
+ return results
285
+ fetch_k = self.index.ntotal
284
286
 
285
287
  def _check_condition(self, doc: Document, condition: dict[str, Any]) -> bool:
286
288
  if "operator" not in condition and "conditions" not in condition:
@@ -55,6 +55,21 @@ class TestFAISSDocumentStoreUnit:
55
55
  with pytest.raises(DuplicateDocumentError, match="already exists"):
56
56
  ds.write_documents([Document(id="dup", content="second")], policy=DuplicatePolicy.FAIL)
57
57
 
58
+ def test_write_documents_overwrite_duplicate_in_same_batch_leaves_no_orphan_vector(self):
59
+ ds = FAISSDocumentStore(embedding_dim=2)
60
+ docs = [
61
+ Document(id="a", content="old", embedding=[0.0, 0.0]),
62
+ Document(id="a", content="new", embedding=[0.1, 0.1]),
63
+ Document(id="b", content="b", embedding=[5.0, 5.0]),
64
+ ]
65
+ ds.write_documents(docs, policy=DuplicatePolicy.OVERWRITE)
66
+
67
+ assert ds.count_documents() == 2
68
+ assert ds.index.ntotal == 2
69
+ results = ds.search([0.0, 0.0], top_k=2)
70
+ assert [doc.id for doc in results] == ["a", "b"]
71
+ assert results[0].content == "new"
72
+
58
73
  @pytest.mark.parametrize(
59
74
  ("filters", "error_match"),
60
75
  [
@@ -70,6 +85,17 @@ class TestFAISSDocumentStoreUnit:
70
85
  with pytest.raises(FilterError, match=error_match):
71
86
  ds.filter_documents(filters=filters)
72
87
 
88
+ def test_search_with_filters_finds_match_outside_first_candidates(self):
89
+ # The only matching document is the farthest one, outside the first `top_k * 10` neighbours.
90
+ ds = FAISSDocumentStore(embedding_dim=2)
91
+ docs = [Document(id=f"en{i}", embedding=[1.0, i / 100], meta={"lang": "en"}) for i in range(30)]
92
+ docs.append(Document(id="de", embedding=[-1.0, 0.0], meta={"lang": "de"}))
93
+ ds.write_documents(docs)
94
+
95
+ results = ds.search([1.0, 0.0], top_k=1, filters={"field": "meta.lang", "operator": "==", "value": "de"})
96
+
97
+ assert [doc.id for doc in results] == ["de"]
98
+
73
99
 
74
100
  @pytest.mark.integration
75
101
  class TestFAISSDocumentStore(
File without changes