Repository: deepset-ai/haystack

Issue:
Improve speed of FAISSDocumentStore.delete_all_documents()
Currently, deleting 100k documents from the FAISSDocumentStore takes ~ 26 min (benchmark run on p3.2xlarge instance with postgres docker). It seems to increase nonlinearly with the number of docs. Let's optimize it or introduce an alternative that deletes all tables created by the current doc store (e.g. `delete_collection()`)

```
12/01/2020 19:34:13 - INFO - retriever -   ##### Start indexing run: dpr, faiss_flat, 100000 docs ##### 
haystack-postgres
d5f14cc50ba0981c5e60fa5751224505937d2490f4195f4630c875f85405c301
CREATE DATABASE
12/01/2020 19:34:26 - WARNING - farm.modeling.language_model -   Could not automatically detect from language model name what language it is. 
	 We guess it's an *ENGLISH* model ... 
	 If not: Init the language model by supplying the 'language' param.
12/01/2020 19:34:33 - WARNING - farm.modeling.language_model -   Could not automatically detect from language model name what language it is. 
	 We guess it's an *ENGLISH* model ... 
	 If not: Init the language model by supplying the 'language' param.
12/01/2020 19:34:33 - INFO - farm.file_utils -   Downloading from s3://ext-haystack-retriever-eval/nq2squad-dev.json to ../../data/retriever/
12/01/2020 19:34:33 - INFO - farm.file_utils -   Skipping nq2squad-dev.json (exists locally)
12/01/2020 19:34:33 - INFO - farm.file_utils -   Downloading from s3://ext-haystack-retriever-eval/psgs_w100_minus_gold_1m.tsv to ../../data/retriever/
12/01/2020 19:34:34 - INFO - farm.file_utils -   Skipping psgs_w100_minus_gold_1m.tsv (exists locally)
12/01/2020 19:37:22 - INFO - haystack.document_store.faiss -   Updating embeddings for 100000 docs...
Creating Embeddings: 100%|██████████| 6250/6250 [07:37<00:00, 13.67 Batches/s]
12/01/2020 19:49:12 - INFO - haystack.document_store.faiss -   Indexing embeddings and updating vectors_ids...
100%|██████████| 10/10 [01:19<00:00,  7.96s/it]
12/01/2020 19:50:32 - INFO - retriever -   Deleting all docs from this run ...
12/01/2020 20:16:12 - INFO - retriever -   ##### Start indexing run: dpr, faiss_hnsw, 100000 docs ##### 
```


Discussion:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 21 days if no further activity occurs.

@tholor Is this still issue?
We haven't benchmarked it recently. There were a few changes to the ORM since the creation of the issue, but it's very likely still slow.

It would be a great help in case you want to tackle it!
I will be working on it.

Resolving patch:
diff --git a/haystack/document_store/sql.py b/haystack/document_store/sql.py
--- a/haystack/document_store/sql.py
+++ b/haystack/document_store/sql.py
@@ -471,18 +471,21 @@ def delete_documents(self, index: Optional[str] = None, filters: Optional[Dict[s
         :return: None
         """
         index = index or self.index
-        document_ids_to_delete = self.session.query(DocumentORM.id).filter_by(index=index)
+
         if filters:
             # documents_query = documents_query.join(MetaORM)
+            document_ids_to_delete = self.session.query(DocumentORM.id).filter_by(index=index)
             for key, values in filters.items():
                 document_ids_to_delete = document_ids_to_delete.filter(
                         MetaORM.name == key,
                         MetaORM.value.in_(values),
                         DocumentORM.id == MetaORM.document_id
                 )
+            self.session.query(DocumentORM).filter(DocumentORM.id.in_(document_ids_to_delete)).delete(
+                    synchronize_session=False)
+        else:
+            self.session.query(DocumentORM).filter_by(index=index).delete(synchronize_session=False)
 
-        self.session.query(DocumentORM).filter(DocumentORM.id.in_(document_ids_to_delete)).delete(
-                synchronize_session=False)
         self.session.commit()
 
     def _get_or_create(self, session, model, **kwargs):


Test patch: