Skip to content

Commit a30e627

Browse files
committed
Adding new format and new function to handle it
1 parent 1c72931 commit a30e627

File tree

2 files changed

+142
-8
lines changed

2 files changed

+142
-8
lines changed

meilisearch/_httprequests.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,27 @@ def send_request(
2020
self,
2121
http_method: Callable,
2222
path: str,
23-
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str]]] = None,
23+
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
24+
content_type: Optional[str] = None,
2425
) -> Any:
26+
if content_type:
27+
self.headers['Content-Type'] = content_type
2528
try:
2629
request_path = self.config.url + '/' + path
27-
request = http_method(
28-
request_path,
29-
timeout=self.config.timeout,
30-
headers=self.headers,
31-
data=json.dumps(body) if body else "null"
32-
)
30+
if not content_type:
31+
request = http_method(
32+
request_path,
33+
timeout=self.config.timeout,
34+
headers=self.headers,
35+
data=json.dumps(body) if body else "null"
36+
)
37+
else:
38+
request = http_method(
39+
request_path,
40+
timeout=self.config.timeout,
41+
headers=self.headers,
42+
data=body
43+
)
3344
return self.__validate(request)
3445

3546
except requests.exceptions.Timeout as err:
@@ -46,8 +57,9 @@ def post(
4657
self,
4758
path: str,
4859
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str]]] = None,
60+
content_type: Optional[str] = None,
4961
) -> Any:
50-
return self.send_request(requests.post, path, body)
62+
return self.send_request(requests.post, path, body, content_type)
5163

5264
def put(
5365
self,

meilisearch/index.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,128 @@ def add_documents_in_batches(
391391

392392
return update_ids
393393

394+
def add_documents_json(
395+
self,
396+
str_documents: str,
397+
primary_key: Optional[str] = None,
398+
) -> Dict[str, int]:
399+
"""Add string documents from JSON file to the index.
400+
401+
Parameters
402+
----------
403+
str_documents:
404+
String of document from a JSON file.
405+
primary_key (optional):
406+
The primary-key used in index. Ignored if already set up.
407+
408+
Returns
409+
-------
410+
update:
411+
Dictionary containing an update id to track the action:
412+
https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
413+
414+
Raises
415+
------
416+
MeiliSearchApiError
417+
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
418+
"""
419+
return self.add_documents_raw(str_documents, primary_key, 'json')
420+
421+
def add_documents_csv(
422+
self,
423+
str_documents: str,
424+
primary_key: Optional[str] = None,
425+
) -> Dict[str, int]:
426+
"""Add string documents from a CSV file to the index.
427+
428+
Parameters
429+
----------
430+
str_documents:
431+
String of document from a CSV file.
432+
primary_key (optional):
433+
The primary-key used in index. Ignored if already set up.
434+
435+
Returns
436+
-------
437+
update:
438+
Dictionary containing an update id to track the action:
439+
https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
440+
441+
Raises
442+
------
443+
MeiliSearchApiError
444+
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
445+
"""
446+
return self.add_documents_raw(str_documents, primary_key, 'csv')
447+
448+
def add_documents_ndjson(
449+
self,
450+
str_documents: str,
451+
primary_key: Optional[str] = None,
452+
) -> Dict[str, int]:
453+
"""Add string documents from a NDJSON file to the index.
454+
455+
Parameters
456+
----------
457+
str_documents:
458+
String of document from a NDJSON file.
459+
primary_key (optional):
460+
The primary-key used in index. Ignored if already set up.
461+
462+
Returns
463+
-------
464+
update:
465+
Dictionary containing an update id to track the action:
466+
https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
467+
468+
Raises
469+
------
470+
MeiliSearchApiError
471+
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
472+
"""
473+
return self.add_documents_raw(str_documents, primary_key, 'jsonl')
474+
475+
def add_documents_raw(
476+
self,
477+
str_documents: str,
478+
primary_key: Optional[str] = None,
479+
type: Optional[str] = None,
480+
) -> Dict[str, int]:
481+
"""Add string documents to the index.
482+
483+
Parameters
484+
----------
485+
str_documents:
486+
String of document.
487+
primary_key (optional):
488+
The primary-key used in index. Ignored if already set up.
489+
type:
490+
The type of document. Type available: 'csv', 'json', 'jsonl'
491+
492+
Returns
493+
-------
494+
update:
495+
Dictionary containing an update id to track the action:
496+
https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
497+
498+
Raises
499+
------
500+
MeiliSearchApiError
501+
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
502+
"""
503+
if primary_key is None:
504+
url = f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}'
505+
else:
506+
primary_key = urllib.parse.urlencode({'primaryKey': primary_key})
507+
url = f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{primary_key}'
508+
if type == "json":
509+
content_type = 'application/json'
510+
if type == "jsonl":
511+
content_type = 'application/x-ndjson'
512+
if type == "csv":
513+
content_type = 'text/csv'
514+
return self.http.post(url, str_documents, content_type)
515+
394516
def update_documents(
395517
self,
396518
documents: List[Dict[str, Any]],

0 commit comments

Comments
 (0)