@@ -316,7 +316,6 @@ def get_documents(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dic
316316 """
317317 if parameters is None :
318318 parameters = {}
319-
320319 return self .http .get (
321320 f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } ?{ urllib .parse .urlencode (parameters )} '
322321 )
@@ -346,11 +345,7 @@ def add_documents(
346345 MeiliSearchApiError
347346 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
348347 """
349- if primary_key is None :
350- url = f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } '
351- else :
352- primary_key = urllib .parse .urlencode ({'primaryKey' : primary_key })
353- url = f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } ?{ primary_key } '
348+ url = self ._build_url (primary_key )
354349 return self .http .post (url , documents )
355350
356351 def add_documents_in_batches (
@@ -391,6 +386,118 @@ def add_documents_in_batches(
391386
392387 return update_ids
393388
389+ def add_documents_json (
390+ self ,
391+ str_documents : str ,
392+ primary_key : Optional [str ] = None ,
393+ ) -> Dict [str , int ]:
394+ """Add string documents from JSON file to the index.
395+
396+ Parameters
397+ ----------
398+ str_documents:
399+ String of document from a JSON file.
400+ primary_key (optional):
401+ The primary-key used in index. Ignored if already set up.
402+
403+ Returns
404+ -------
405+ update:
406+ Dictionary containing an update id to track the action:
407+ https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
408+
409+ Raises
410+ ------
411+ MeiliSearchApiError
412+ 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
413+ """
414+ return self .add_documents_raw (str_documents , primary_key , 'application/json' )
415+
416+ def add_documents_csv (
417+ self ,
418+ str_documents : str ,
419+ primary_key : Optional [str ] = None ,
420+ ) -> Dict [str , int ]:
421+ """Add string documents from a CSV file to the index.
422+
423+ Parameters
424+ ----------
425+ str_documents:
426+ String of document from a CSV file.
427+ primary_key (optional):
428+ The primary-key used in index. Ignored if already set up.
429+
430+ Returns
431+ -------
432+ update:
433+ Dictionary containing an update id to track the action:
434+ https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
435+
436+ Raises
437+ ------
438+ MeiliSearchApiError
439+ 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
440+ """
441+ return self .add_documents_raw (str_documents , primary_key , 'text/csv' )
442+
443+ def add_documents_ndjson (
444+ self ,
445+ str_documents : str ,
446+ primary_key : Optional [str ] = None ,
447+ ) -> Dict [str , int ]:
448+ """Add string documents from a NDJSON file to the index.
449+
450+ Parameters
451+ ----------
452+ str_documents:
453+ String of document from a NDJSON file.
454+ primary_key (optional):
455+ The primary-key used in index. Ignored if already set up.
456+
457+ Returns
458+ -------
459+ update:
460+ Dictionary containing an update id to track the action:
461+ https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
462+
463+ Raises
464+ ------
465+ MeiliSearchApiError
466+ 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
467+ """
468+ return self .add_documents_raw (str_documents , primary_key , 'application/x-ndjson' )
469+
470+ def add_documents_raw (
471+ self ,
472+ str_documents : str ,
473+ primary_key : Optional [str ] = None ,
474+ content_type : Optional [str ] = None ,
475+ ) -> Dict [str , int ]:
476+ """Add string documents to the index.
477+
478+ Parameters
479+ ----------
480+ str_documents:
481+ String of document.
482+ primary_key (optional):
483+ The primary-key used in index. Ignored if already set up.
484+ type:
485+ The type of document. Type available: 'csv', 'json', 'jsonl'
486+
487+ Returns
488+ -------
489+ update:
490+ Dictionary containing an update id to track the action:
491+ https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
492+
493+ Raises
494+ ------
495+ MeiliSearchApiError
496+ 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
497+ """
498+ url = self ._build_url (primary_key )
499+ return self .http .post (url , str_documents , content_type )
500+
394501 def update_documents (
395502 self ,
396503 documents : List [Dict [str , Any ]],
@@ -416,11 +523,7 @@ def update_documents(
416523 MeiliSearchApiError
417524 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
418525 """
419- if primary_key is None :
420- url = f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } '
421- else :
422- primary_key = urllib .parse .urlencode ({'primaryKey' : primary_key })
423- url = f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } ?{ primary_key } '
526+ url = self ._build_url (primary_key )
424527 return self .http .put (url , documents )
425528
426529 def update_documents_in_batches (
@@ -1134,3 +1237,12 @@ def _iso_to_date_time(iso_date: Optional[Union[datetime, str]]) -> Optional[date
11341237
11351238 def __settings_url_for (self , sub_route : str ) -> str :
11361239 return f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .setting } /{ sub_route } '
1240+
1241+ def _build_url (
1242+ self ,
1243+ primary_key : Optional [str ] = None ,
1244+ ) -> str :
1245+ if primary_key is None :
1246+ return f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } '
1247+ primary_key = urllib .parse .urlencode ({'primaryKey' : primary_key })
1248+ return f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } ?{ primary_key } '
0 commit comments