Skip to content

Commit e04cb2c

Browse files
authored
Try #560:
2 parents 6373bc6 + 3709743 commit e04cb2c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

meilisearch/index.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,65 @@ def update_documents(
534534
url = self._build_url(primary_key)
535535
response = self.http.put(url, documents)
536536
return TaskInfo(**response)
537+
538+
def update_documents_ndjson(
539+
self,
540+
str_documents: str,
541+
primary_key: str | None = None,
542+
) -> TaskInfo:
543+
"""Update documents 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+
"""Add string documents to 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.post(url, str_documents, content_type)
595+
return TaskInfo(**response)
537596

538597
def update_documents_in_batches(
539598
self,

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)