Skip to content

New json handlers #562

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 18, 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
28 changes: 28 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,33 @@ def update_documents_ndjson(
"""
return self.update_documents_raw(str_documents, primary_key, 'application/x-ndjson')

def update_documents_json(
self,
str_documents: str,
primary_key: str | None = None,
) -> TaskInfo:
"""Update documents as a json string in 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
-------
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/json')

def update_documents_raw(
self,
str_documents: str,
Expand Down Expand Up @@ -1478,3 +1505,4 @@ def _build_url(
return f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}'
primary_key = parse.urlencode({'primaryKey': primary_key})
return f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{primary_key}'

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 @@ -184,6 +184,16 @@ def test_add_documents_json(empty_index, small_movies_json_file):
assert task.status == 'succeeded'
assert index.get_primary_key() == 'id'

def test_update_documents_json(index_with_documents, small_movies_json_file):
"""Tests updating a single document with json string."""
index = index_with_documents()
response = index.update_documents_json(small_movies_json_file)
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'

def test_add_documents_ndjson(empty_index, songs_ndjson):
"""Tests adding new documents to a clean index."""
index = empty_index()
Expand Down