Skip to content

New ndjson handlers #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def patch(
def put(
self,
path: str,
body: dict[str, Any] | list[dict[str, Any]] | list[str] | None = None,
body: dict[str, Any] | list[dict[str, Any]] | list[str] | str | None = None,
content_type: str | None = 'application/json',
) -> Any:
return self.send_request(requests.put, path, body, content_type)
Expand Down
59 changes: 59 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,65 @@ def update_documents(
response = self.http.put(url, documents)
return TaskInfo(**response)

def update_documents_ndjson(
self,
str_documents: str,
primary_key: str | None = None,
) -> TaskInfo:
"""Update documents as a ndjson string in 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
-------
task_info:
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task

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.update_documents_raw(str_documents, primary_key, 'application/x-ndjson')

def update_documents_raw(
self,
str_documents: str,
primary_key: str | None = None,
content_type: str | None = None,
) -> TaskInfo:
"""Update documents as a string in 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
-------
task_info:
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task

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
"""
url = self._build_url(primary_key)
response = self.http.put(url, str_documents, content_type)
return TaskInfo(**response)

def update_documents_in_batches(
self,
documents: list[dict[str, Any]],
Expand Down
10 changes: 10 additions & 0 deletions tests/index/test_index_document_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,13 @@ def test_add_documents_ndjson(empty_index, songs_ndjson):
task = index.wait_for_task(response.task_uid)
assert task.status == 'succeeded'
assert index.get_primary_key() == 'id'

def test_update_documents_ndjson(index_with_documents, songs_ndjson):
"""Tests updating a single document with ndjson string."""
index = index_with_documents()
response = index.update_documents_ndjson(songs_ndjson)
assert isinstance(response, TaskInfo)
assert response.task_uid != None
task = index.wait_for_task(response.task_uid)
assert task.status == 'succeeded'
assert index.get_primary_key() == 'id'