Skip to content

Commit 4ebee15

Browse files
bors[bot]Azanul
andauthored
Merge #560
560: New ndjson handlers r=alallema a=Azanul # Pull Request ## Related issue Fixes #347 ## What does this PR do? - Adds handler for updating documents with raw string - Adds handler for updating documents with ndjson string. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Azanul <[email protected]> Co-authored-by: Azanul Haque <[email protected]>
2 parents 6373bc6 + 56e2c6b commit 4ebee15

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

meilisearch/_httprequests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def patch(
7878
def put(
7979
self,
8080
path: str,
81-
body: dict[str, Any] | list[dict[str, Any]] | list[str] | None = None,
81+
body: dict[str, Any] | list[dict[str, Any]] | list[str] | str | None = None,
8282
content_type: str | None = 'application/json',
8383
) -> Any:
8484
return self.send_request(requests.put, path, body, content_type)

meilisearch/index.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,65 @@ def update_documents(
535535
response = self.http.put(url, documents)
536536
return TaskInfo(**response)
537537

538+
def update_documents_ndjson(
539+
self,
540+
str_documents: str,
541+
primary_key: str | None = None,
542+
) -> TaskInfo:
543+
"""Update documents as a ndjson string in the index.
544+
545+
Parameters
546+
----------
547+
str_documents:
548+
String of document from a NDJSON file.
549+
primary_key (optional):
550+
The primary-key used in index. Ignored if already set up
551+
552+
Returns
553+
-------
554+
task_info:
555+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
556+
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
557+
558+
Raises
559+
------
560+
MeiliSearchApiError
561+
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
562+
"""
563+
return self.update_documents_raw(str_documents, primary_key, 'application/x-ndjson')
564+
565+
def update_documents_raw(
566+
self,
567+
str_documents: str,
568+
primary_key: str | None = None,
569+
content_type: str | None = None,
570+
) -> TaskInfo:
571+
"""Update documents as a string in the index.
572+
573+
Parameters
574+
----------
575+
str_documents:
576+
String of document.
577+
primary_key (optional):
578+
The primary-key used in index. Ignored if already set up.
579+
type:
580+
The type of document. Type available: 'csv', 'json', 'jsonl'
581+
582+
Returns
583+
-------
584+
task_info:
585+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
586+
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
587+
588+
Raises
589+
------
590+
MeiliSearchApiError
591+
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
592+
"""
593+
url = self._build_url(primary_key)
594+
response = self.http.put(url, str_documents, content_type)
595+
return TaskInfo(**response)
596+
538597
def update_documents_in_batches(
539598
self,
540599
documents: list[dict[str, Any]],

tests/index/test_index_document_meilisearch.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,13 @@ def test_add_documents_ndjson(empty_index, songs_ndjson):
193193
task = index.wait_for_task(response.task_uid)
194194
assert task.status == 'succeeded'
195195
assert index.get_primary_key() == 'id'
196+
197+
def test_update_documents_ndjson(index_with_documents, songs_ndjson):
198+
"""Tests updating a single document with ndjson string."""
199+
index = index_with_documents()
200+
response = index.update_documents_ndjson(songs_ndjson)
201+
assert isinstance(response, TaskInfo)
202+
assert response.task_uid != None
203+
task = index.wait_for_task(response.task_uid)
204+
assert task.status == 'succeeded'
205+
assert index.get_primary_key() == 'id'

0 commit comments

Comments
 (0)