faiss-haystack 3.0.1__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,12 @@
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
+
3
10
  ## [integrations/faiss-v3.0.0] - 2026-10-16
4
11
 
5
12
  ### 🧪 Testing
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: faiss-haystack
3
- Version: 3.0.1
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
@@ -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:
@@ -85,6 +85,17 @@ class TestFAISSDocumentStoreUnit:
85
85
  with pytest.raises(FilterError, match=error_match):
86
86
  ds.filter_documents(filters=filters)
87
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
+
88
99
 
89
100
  @pytest.mark.integration
90
101
  class TestFAISSDocumentStore(
File without changes