Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docarray/array/storage/elastic/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ElasticConfig:
hosts: str = 'http://localhost:9200'
index_name: Optional[str] = None
es_config: Dict[str, Any] = field(default_factory=dict)
batch_size: int = 64


class BackendMixin(BaseBackendMixin):
Expand Down
47 changes: 25 additions & 22 deletions docarray/array/storage/elastic/seqlike.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, Iterable
from typing import Union, Iterable, Dict

from ..base.seqlike import BaseSequenceLikeMixin
from .... import Document
Expand Down Expand Up @@ -57,25 +57,28 @@ def __repr__(self):
"""
return f'<{self.__class__.__name__} (length={len(self)}) at {id(self)}>'

def extend(self, values: Iterable['Document']) -> None:
"""Extends the array with the given values

:param values: Documents to be added
"""

request = []
for value in values:
request.append(
{
"_op_type": "index",
'_id': value.id,
'_index': self._config.index_name,
'embedding': self._map_embedding(value.embedding),
'blob': value.to_base64(),
}
)
self._offset2ids.append(value.id)

if len(request) > 0:
self._send_requests(request)
def _document_to_elastic(self, doc: 'Document') -> Dict:
return {
"_op_type": "index",
'_id': doc.id,
'_index': self._config.index_name,
'embedding': self._map_embedding(doc.embedding),
'blob': doc.to_base64(),
}

def _upload_batch(self, docs: Iterable['Document']):
batch = []
for doc in docs:
batch.append(self._document_to_elastic(doc))
if len(batch) > self._config.batch_size:
self._send_requests(batch)
self._refresh(self._config.index_name)
batch = []
if len(batch) > 0:
self._send_requests(batch)
self._refresh(self._config.index_name)

def extend(self, docs: Iterable['Document']):
docs = list(docs)
self._upload_batch(docs)
self._offset2ids.extend([doc.id for doc in docs])