Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54da26f

Browse files
authored
Merge pull requesttypesense#22 from pratheekrebala/support-iterator-import
Support batching of calls to `Documents.import_`
2 parentse92044a +bdd9032 commit54da26f

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

‎typesense/documents.py‎

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

55
from .documentimportDocument
66
from .loggerimportlogger
7-
7+
fromcollections.abcimportIterable
88

99
classDocuments(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-
defimport_(self,documents,params=None):
56-
ifisinstance(documents,list):
57-
document_strs= []
58-
fordocumentindocuments:
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-
forres_obj_strinres_obj_strs:
67-
try:
68-
res_obj_json=json.loads(res_obj_str)
69-
exceptjson.JSONDecodeErrorase:
70-
raiseTypesenseClientError("Invalid response")frome
71-
response_objs.append(res_obj_json)
55+
defimport_(self,documents,params=None,batch_size=None):
56+
ifisinstance(documents,Iterable):
57+
ifbatch_size:
58+
response_objs= []
59+
batch= []
60+
fordocumentindocuments:
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+
ifbatch:
67+
api_response=self.import_(batch,params)
68+
response_objs.extend(api_response)
69+
70+
else:
71+
document_strs= []
72+
fordocumentindocuments:
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+
forres_obj_strinres_obj_strs:
81+
try:
82+
res_obj_json=json.loads(res_obj_str)
83+
exceptjson.JSONDecodeErrorase:
84+
raiseTypesenseClientError("Invalid response")frome
85+
response_objs.append(res_obj_json)
7286

7387
returnresponse_objs
7488
else:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp