Skip to content

Commit 54da26f

Browse files
authored
Merge pull request #22 from pratheekrebala/support-iterator-import
Support batching of calls to `Documents.import_`
2 parents e92044a + bdd9032 commit 54da26f

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

typesense/documents.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .document import Document
66
from .logger import logger
7-
7+
from collections.abc import Iterable
88

99
class Documents(object):
1010
RESOURCE_PATH = 'documents'
@@ -52,23 +52,37 @@ def import_jsonl(self, documents_jsonl):
5252

5353
# `documents` can be either a list of document objects (or)
5454
# JSONL-formatted string containing multiple documents
55-
def import_(self, documents, params=None):
56-
if isinstance(documents, list):
57-
document_strs = []
58-
for document in documents:
59-
document_strs.append(json.dumps(document))
60-
61-
docs_import = '\n'.join(document_strs)
62-
api_response = self.api_call.post(self._endpoint_path('import'), docs_import, params, as_json=False)
63-
res_obj_strs = api_response.split('\n')
64-
65-
response_objs = []
66-
for res_obj_str in res_obj_strs:
67-
try:
68-
res_obj_json = json.loads(res_obj_str)
69-
except json.JSONDecodeError as e:
70-
raise TypesenseClientError("Invalid response") from e
71-
response_objs.append(res_obj_json)
55+
def import_(self, documents, params=None, batch_size=None):
56+
if isinstance(documents, Iterable):
57+
if batch_size:
58+
response_objs = []
59+
batch = []
60+
for document in documents:
61+
batch.append(document)
62+
if (len(batch) == batch_size):
63+
api_response = self.import_(batch, params)
64+
response_objs.extend(api_response)
65+
batch = []
66+
if batch:
67+
api_response = self.import_(batch, params)
68+
response_objs.extend(api_response)
69+
70+
else:
71+
document_strs = []
72+
for document in documents:
73+
document_strs.append(json.dumps(document))
74+
75+
docs_import = '\n'.join(document_strs)
76+
api_response = self.api_call.post(self._endpoint_path('import'), docs_import, params, as_json=False)
77+
res_obj_strs = api_response.split('\n')
78+
79+
response_objs = []
80+
for res_obj_str in res_obj_strs:
81+
try:
82+
res_obj_json = json.loads(res_obj_str)
83+
except json.JSONDecodeError as e:
84+
raise TypesenseClientError("Invalid response") from e
85+
response_objs.append(res_obj_json)
7286

7387
return response_objs
7488
else:

0 commit comments

Comments
 (0)