generated from deepset-ai/document-store
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4bfef8
commit 1b6f9cc
Showing
3 changed files
with
30 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
# SPDX-FileCopyrightText: 2023-present Silvano Cerza <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import logging | ||
from typing import Any, Dict, List, Optional, Union, Mapping | ||
import json | ||
import logging | ||
from typing import Any, Dict, List, Mapping, Optional, Union | ||
|
||
from elasticsearch import Elasticsearch, helpers | ||
from elastic_transport import NodeConfig | ||
import numpy as np | ||
from pandas import DataFrame | ||
|
||
from elastic_transport import NodeConfig | ||
from elasticsearch import Elasticsearch, helpers | ||
from haystack.preview.dataclasses import Document | ||
from haystack.preview.document_stores.decorator import document_store | ||
from haystack.preview.document_stores.errors import DuplicateDocumentError | ||
from haystack.preview.document_stores.protocols import DuplicatePolicy | ||
from pandas import DataFrame | ||
|
||
from elasticsearch_haystack.filters import _normalize_filters | ||
|
||
|
@@ -36,7 +35,7 @@ def __init__(self, *, hosts: Optional[Hosts] = None, index: str = "default", **k | |
:param hosts: List of hosts running the Elasticsearch client. Defaults to None | ||
:param index: Name of index in Elasticsearch, if it doesn't exist it will be created. Defaults to "default" | ||
:param \*\*kwargs: Optional arguments that ``Elasticsearch`` takes. | ||
:param **kwargs: Optional arguments that ``Elasticsearch`` takes. | ||
""" | ||
self._client = Elasticsearch(hosts, **kwargs) | ||
self._index = index | ||
|
@@ -149,7 +148,8 @@ def write_documents(self, documents: List[Document], policy: DuplicatePolicy = D | |
""" | ||
if len(documents) > 0: | ||
if not isinstance(documents[0], Document): | ||
raise ValueError("param 'documents' must contain a list of objects of type Document") | ||
msg = "param 'documents' must contain a list of objects of type Document" | ||
raise ValueError(msg) | ||
|
||
action = "index" if policy == DuplicatePolicy.OVERWRITE else "create" | ||
_, errors = helpers.bulk( | ||
|
@@ -165,8 +165,9 @@ def write_documents(self, documents: List[Document], policy: DuplicatePolicy = D | |
if errors and policy == DuplicatePolicy.FAIL: | ||
# TODO: Handle errors in a better way, we're assuming that all errors | ||
# are related to duplicate documents but that could be very well be wrong. | ||
ids = ', '.join((e["create"]["_id"] for e in errors)) | ||
raise DuplicateDocumentError(f"IDs '{ids}' already exist in the document store.") | ||
ids = ", ".join(e["create"]["_id"] for e in errors) | ||
msg = f"IDs '{ids}' already exist in the document store." | ||
raise DuplicateDocumentError(msg) | ||
|
||
def _deserialize_document(self, hit: Dict[str, Any]) -> Document: | ||
""" | ||
|
@@ -231,7 +232,7 @@ def delete_documents(self, document_ids: List[str]) -> None: | |
# | ||
helpers.bulk( | ||
client=self._client, | ||
actions=({"_op_type": "delete", "_id": id} for id in document_ids), | ||
actions=({"_op_type": "delete", "_id": id_} for id_ in document_ids), | ||
refresh="wait_for", | ||
index=self._index, | ||
raise_on_error=False, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
# SPDX-FileCopyrightText: 2023-present Silvano Cerza <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from typing import List | ||
|
||
import pytest | ||
from haystack.preview.testing.document_store import DocumentStoreBaseTests | ||
from haystack.preview.dataclasses.document import Document | ||
from haystack.preview.document_stores.protocols import DuplicatePolicy | ||
from haystack.preview.document_stores.errors import DuplicateDocumentError | ||
from haystack.preview.document_stores.protocols import DuplicatePolicy | ||
from haystack.preview.testing.document_store import DocumentStoreBaseTests | ||
|
||
from elasticsearch_haystack.document_store import ElasticsearchDocumentStore | ||
|
||
|