Skip to content

Add pagination parameters to the get_document() method #492

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 5 commits into from
Jul 7, 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
12 changes: 10 additions & 2 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,15 @@ def search(self, query: str, opt_params: Optional[Dict[str, Any]] = None) -> Dic
body=body
)

def get_document(self, document_id: str) -> Dict[str, Any]:
def get_document(self, document_id: str, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""Get one document with given document identifier.

Parameters
----------
document_id:
Unique identifier of the document.
parameters (optional):
parameters accepted by the get document route: https://docs.meilisearch.com/reference/api/documents.html#get-one-document

Returns
-------
Expand All @@ -267,8 +269,12 @@ def get_document(self, document_id: str) -> Dict[str, Any]:
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 parameters is None:
parameters = {}
elif 'fields' in parameters and isinstance(parameters['fields'], list):
parameters['fields'] = ",".join(parameters['fields'])
return self.http.get(
f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}/{document_id}'
f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}/{document_id}?{parse.urlencode(parameters)}'
)

def get_documents(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
Expand All @@ -291,6 +297,8 @@ def get_documents(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dic
"""
if parameters is None:
parameters = {}
elif 'fields' in parameters and isinstance(parameters['fields'], list):
parameters['fields'] = ",".join(parameters['fields'])
return self.http.get(
f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{parse.urlencode(parameters)}'
)
Expand Down
7 changes: 4 additions & 3 deletions meilisearch/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ def get_tasks(config: Config, parameters: Optional[Dict[str, Any]] = None) -> Di
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
"""
http = HttpRequests(config)
if parameters is None or parameters == {}:
if parameters is None:
parameters = {}
elif 'indexUid' in parameters:
parameters['indexUid'] = ",".join(parameters['indexUid'])
for param in parameters:
if isinstance(parameters[param], list):
parameters[param] = ",".join(parameters[param])
return http.get(
f"{config.paths.task}?{parse.urlencode(parameters)}"
)
Expand Down
9 changes: 9 additions & 0 deletions tests/index/test_index_document_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def test_get_document(index_with_documents):
assert 'title' in response
assert response['title'] == 'The Highwaymen'

def test_get_document_with_fields(index_with_documents):
"""Tests getting one document from a populated index."""
response = index_with_documents().get_document('500682', {'fields' : ['id', 'title']})
assert isinstance(response, dict)
assert 'title' in response
assert 'poster' not in response
assert response['title'] == 'The Highwaymen'

def test_get_document_inexistent(empty_index):
"""Tests getting one inexistent document from a populated index."""
with pytest.raises(Exception):
Expand All @@ -73,6 +81,7 @@ def test_get_documents_offset_optional_params(index_with_documents):
'fields': 'title'
})
assert len(response_offset_limit['results']) == 3
assert 'title' in response_offset_limit['results'][0]
assert response_offset_limit['results'][0]['title'] == response['results'][1]['title']

def test_update_documents(index_with_documents, small_movies):
Expand Down
2 changes: 1 addition & 1 deletion tests/index/test_index_task_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_get_tasks_default(index_with_documents):

def test_get_tasks(empty_index, small_movies):
"""Tests getting the tasks list of a populated index."""
index = empty_index()
index = empty_index("test_task")
current_tasks = index.get_tasks()
pre_count = len(current_tasks['results'])
response = index.add_documents(small_movies)
Expand Down