Skip to content

Commit f1cfc58

Browse files
committed
Improve parameters for get_document method
1 parent ad82f85 commit f1cfc58

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

meilisearch/index.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def search(self, query: str, opt_params: Optional[Dict[str, Any]] = None) -> Dic
249249
body=body
250250
)
251251

252-
def get_document(self, document_id: str, fields: Optional[List[str]] = None) -> Dict[str, Any]:
252+
def get_document(self, document_id: str, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
253253
"""Get one document with given document identifier.
254254
255255
Parameters
@@ -267,10 +267,10 @@ def get_document(self, document_id: str, fields: Optional[List[str]] = None) ->
267267
MeiliSearchApiError
268268
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
269269
"""
270-
if fields is None:
270+
if parameters is None:
271271
parameters = {}
272-
else:
273-
parameters = {'fields': ",".join(fields)}
272+
elif 'fields' in parameters and isinstance(parameters['fields'], list):
273+
parameters['fields'] = ",".join(parameters['fields'])
274274
return self.http.get(
275275
f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}/{document_id}?{parse.urlencode(parameters)}'
276276
)
@@ -295,7 +295,7 @@ def get_documents(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dic
295295
"""
296296
if parameters is None:
297297
parameters = {}
298-
elif 'fields' in parameters and isinstance(parameters, list):
298+
elif 'fields' in parameters and isinstance(parameters['fields'], list):
299299
parameters['fields'] = ",".join(parameters['fields'])
300300
return self.http.get(
301301
f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{parse.urlencode(parameters)}'

tests/index/test_index_document_meilisearch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_get_document(index_with_documents):
5252

5353
def test_get_document_with_fields(index_with_documents):
5454
"""Tests getting one document from a populated index."""
55-
response = index_with_documents().get_document('500682', ['id', 'title'])
55+
response = index_with_documents().get_document('500682', {'fields' : ['id', 'title']})
5656
assert isinstance(response, dict)
5757
assert 'title' in response
5858
assert 'poster' not in response
@@ -81,6 +81,7 @@ def test_get_documents_offset_optional_params(index_with_documents):
8181
'fields': 'title'
8282
})
8383
assert len(response_offset_limit['results']) == 3
84+
assert 'title' in response_offset_limit['results'][0]
8485
assert response_offset_limit['results'][0]['title'] == response['results'][1]['title']
8586

8687
def test_update_documents(index_with_documents, small_movies):

0 commit comments

Comments
 (0)