From 9355cc5c11a09d67d372e723e8411f3c1e469ef7 Mon Sep 17 00:00:00 2001 From: alallema Date: Tue, 5 Oct 2021 18:15:54 +0200 Subject: [PATCH] Adding new format and new function to handle it --- meilisearch/_httprequests.py | 26 ++++++-- meilisearch/index.py | 122 +++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 7 deletions(-) diff --git a/meilisearch/_httprequests.py b/meilisearch/_httprequests.py index 865080f9..eff32fce 100644 --- a/meilisearch/_httprequests.py +++ b/meilisearch/_httprequests.py @@ -21,15 +21,26 @@ def send_request( http_method: Callable, path: str, body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str]]] = None, + content_type: Optional[str] = None, ) -> Any: + if content_type: + self.headers['Content-Type'] = content_type try: request_path = self.config.url + '/' + path - request = http_method( - request_path, - timeout=self.config.timeout, - headers=self.headers, - data=json.dumps(body) if body else "null" - ) + if not content_type: + request = http_method( + request_path, + timeout=self.config.timeout, + headers=self.headers, + data=json.dumps(body) if body else "null" + ) + else: + request = http_method( + request_path, + timeout=self.config.timeout, + headers=self.headers, + data=body + ) return self.__validate(request) except requests.exceptions.Timeout as err: @@ -46,8 +57,9 @@ def post( self, path: str, body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str]]] = None, + content_type: Optional[str] = None, ) -> Any: - return self.send_request(requests.post, path, body) + return self.send_request(requests.post, path, body, content_type) def put( self, diff --git a/meilisearch/index.py b/meilisearch/index.py index 480ef4c0..bebcee74 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -391,6 +391,128 @@ def add_documents_in_batches( return update_ids + def add_documents_json( + self, + str_documents: str, + primary_key: Optional[str] = None, + ) -> Dict[str, int]: + """Add string documents from JSON file to the index. + + Parameters + ---------- + str_documents: + String of document from a JSON file. + primary_key (optional): + The primary-key used in index. Ignored if already set up. + + Returns + ------- + update: + Dictionary containing an update id to track the action: + https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status + + Raises + ------ + MeiliSearchApiError + 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 + """ + return self.add_documents_raw(str_documents, primary_key, 'json') + + def add_documents_csv( + self, + str_documents: str, + primary_key: Optional[str] = None, + ) -> Dict[str, int]: + """Add string documents from a CSV file to the index. + + Parameters + ---------- + str_documents: + String of document from a CSV file. + primary_key (optional): + The primary-key used in index. Ignored if already set up. + + Returns + ------- + update: + Dictionary containing an update id to track the action: + https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status + + Raises + ------ + MeiliSearchApiError + 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 + """ + return self.add_documents_raw(str_documents, primary_key, 'csv') + + def add_documents_ndjson( + self, + str_documents: str, + primary_key: Optional[str] = None, + ) -> Dict[str, int]: + """Add string documents from a NDJSON file to the index. + + Parameters + ---------- + str_documents: + String of document from a NDJSON file. + primary_key (optional): + The primary-key used in index. Ignored if already set up. + + Returns + ------- + update: + Dictionary containing an update id to track the action: + https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status + + Raises + ------ + MeiliSearchApiError + 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 + """ + return self.add_documents_raw(str_documents, primary_key, 'jsonl') + + def add_documents_raw( + self, + str_documents: str, + primary_key: Optional[str] = None, + doc_type: Optional[str] = None, + ) -> Dict[str, int]: + """Add string documents to the index. + + Parameters + ---------- + str_documents: + String of document. + primary_key (optional): + The primary-key used in index. Ignored if already set up. + type: + The type of document. Type available: 'csv', 'json', 'jsonl' + + Returns + ------- + update: + Dictionary containing an update id to track the action: + https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status + + Raises + ------ + MeiliSearchApiError + 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 + """ + if primary_key is None: + url = f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}' + else: + primary_key = urllib.parse.urlencode({'primaryKey': primary_key}) + url = f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{primary_key}' + if doc_type == "json": + content_type = 'application/json' + if doc_type == "jsonl": + content_type = 'application/x-ndjson' + if doc_type == "csv": + content_type = 'text/csv' + return self.http.post(url, str_documents, content_type) + def update_documents( self, documents: List[Dict[str, Any]],