Repository: deepset-ai/haystack

Issue:
Vector_IDs collide when using two document indexes and updating the embeddings (FAISS)
**Describe the bug**
I have two document indexes: `document` and `eval_document`. The document store is FAISS.

When I update the embeddings as follows:
```python
document_store.update_embeddings(retriever)  # index='document'
document_store.update_embeddings(retriever, index='eval_document')
```
I get the stacktrace below on the second line. If I switch the order, this remains to be the case.

**Should I not be updating the embeddings of the documents in my evaluation set?**

---


**Error message**
```
Traceback (most recent call last):
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/haystack/document_stores/faiss.py", line 331, in update_embeddings
    self.update_vector_ids(vector_id_map, index=index)
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/haystack/document_stores/sql.py", line 411, in update_vector_ids
    self.session.query(DocumentORM).filter(
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 3276, in update
    result = self.session.execute(
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1692, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1614, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 325, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1481, in _execute_clauseelement
    ret = self._execute_context(
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1845, in _execute_context
    self._handle_dbapi_exception(
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2026, in _handle_dbapi_exception
    util.raise_(
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1802, in _execute_context
    self.dialect.do_execute(
  File "/opt/miniconda3/envs/retrieval3/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: document.vector_id
[SQL: UPDATE document SET vector_id=CASE document.id WHEN ? THEN ? WHEN ? THEN ? END WHERE document.id IN (?, ?) AND document."index" = ?]
[parameters: ('3ffce9fc723abe1adff1d56ffb2a4015', 0, '71000ce590376327c506aebcb8f33966', 1, '3ffce9fc723abe1adff1d56ffb2a4015', '71000ce590376327c506aebcb8f33966', 'eval_document')]
```

**Expected behavior**
VectorIDs seem to be colliding, as the vector_map relies on the highest vector_id within the index
https://github.com/deepset-ai/haystack/blob/0b0b9689a494ae8f6e24ea2dd73717adba13e2a0/haystack/document_stores/faiss.py#L308

but the vector_ids are enforced on the entire table. It thus collides when a second index is used.
https://github.com/deepset-ai/haystack/blob/0b0b9689a494ae8f6e24ea2dd73717adba13e2a0/haystack/document_stores/sql.py#L25-L35

**Possible solution**
Check vector_id count over all indices
```python
 vector_id = sum([self.faiss_indexes[index].ntotal for index in self.faiss_indexes.keys()])
```

**FAQ Check**
- [X] Have you had a look at [our new FAQ page](https://haystack.deepset.ai/overview/faq)?

**System:**
 - OS: Ubuntu 20.04 LTS
 - GPU/CPU: CPU
 - Haystack version (commit or version number):  1.0.0
 - DocumentStore: FAISS
 - Reader: FacebookDPR
 - Retriever: None



Discussion:

Resolving patch:
diff --git a/haystack/document_stores/faiss.py b/haystack/document_stores/faiss.py
--- a/haystack/document_stores/faiss.py
+++ b/haystack/document_stores/faiss.py
@@ -305,7 +305,7 @@ def update_embeddings(
             return
 
         logger.info(f"Updating embeddings for {document_count} docs...")
-        vector_id = self.faiss_indexes[index].ntotal
+        vector_id = sum([self.faiss_indexes[index].ntotal for index in self.faiss_indexes.keys()])
 
         result = self._query(
             index=index,
@@ -329,7 +329,7 @@ def update_embeddings(
 
                 vector_id_map = {}
                 for doc in document_batch:
-                    vector_id_map[doc.id] = vector_id
+                    vector_id_map[str(doc.id)] = str(vector_id)
                     vector_id += 1
                 self.update_vector_ids(vector_id_map, index=index)
                 progress_bar.set_description_str("Documents Processed")


Test patch: