diff --git a/knowledgehub/storages/docstores/elasticsearch.py b/knowledgehub/storages/docstores/elasticsearch.py index 723e960..902e823 100644 --- a/knowledgehub/storages/docstores/elasticsearch.py +++ b/knowledgehub/storages/docstores/elasticsearch.py @@ -153,7 +153,12 @@ class ElasticsearchDocumentStore(BaseDocumentStore): def delete(self, ids: Union[List[str], str]): """Delete document by id""" - raise NotImplementedError("Delete by-id is a Work-in-Progress.") + if not isinstance(ids, list): + ids = [ids] + + query = {"query": {"terms": {"_id": ids}}} + self.client.delete_by_query(index=self.index_name, body=query) + self.client.indices.refresh(index=self.index_name) def save(self, path: Union[str, Path]): """Save document to path""" diff --git a/tests/test_docstores.py b/tests/test_docstores.py index 9a14e84..d8ebe51 100644 --- a/tests/test_docstores.py +++ b/tests/test_docstores.py @@ -176,6 +176,34 @@ _elastic_search_responses = [ }, }, ), + # delete + ( + meta_success, + { + "took": 10, + "timed_out": False, + "total": 1, + "deleted": 1, + "batches": 1, + "version_conflicts": 0, + "noops": 0, + "retries": {"bulk": 0, "search": 0}, + "throttled_millis": 0, + "requests_per_second": -1.0, + "throttled_until_millis": 0, + "failures": [], + }, + ), + # check exists + ( + meta_success, + {"_shards": {"total": 2, "successful": 1, "failed": 0}}, + ), + # count + ( + meta_success, + [{"epoch": "1700549363", "timestamp": "06:49:23", "count": "2"}], + ), ] @@ -260,4 +288,8 @@ def test_elastic_document_store(elastic_api): docs = store.query("text") assert len(docs) == 3, "Document store query() failed" + # delete test + store.delete(first_doc.doc_id) + assert store.count() == 2, "Document store delete() failed" + elastic_api.assert_called()