Add method to update meta fields for documents in Elasticsearch (#242)

This commit is contained in:
Tanay Soni 2020-07-16 15:34:55 +02:00 committed by GitHub
parent a6ec430931
commit 5210c8c2ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -143,6 +143,10 @@ class ElasticsearchDocumentStore(BaseDocumentStore):
documents_to_index.append(_doc)
bulk(self.client, documents_to_index, request_timeout=300)
def update_document_meta(self, id: str, meta: Dict[str, str]):
body = {"doc": meta}
self.client.update(index=self.index, doc_type="_doc", id=id, body=body)
def get_document_count(self, index: Optional[str] = None,) -> int:
if index is None:
index = self.index

View File

@ -1,3 +1,6 @@
import pytest
import time
from haystack.database.base import Document
@ -10,3 +13,12 @@ def test_get_all_documents(document_store_with_docs):
doc = document_store_with_docs.get_document_by_id(documents[0].id)
assert doc.id == documents[0].id
assert doc.text == documents[0].text
@pytest.mark.parametrize("document_store_with_docs", [("elasticsearch")], indirect=True)
def test_elasticsearch_update_meta(document_store_with_docs):
document = document_store_with_docs.query(query=None, filters={"name": ["filename1"]})[0]
document_store_with_docs.update_document_meta(document.id, meta={"meta_field": "updated_meta"})
time.sleep(1)
updated_document = document_store_with_docs.query(query=None, filters={"name": ["filename1"]})[0]
assert updated_document.meta["meta_field"] == "updated_meta"