Skip to content

Commit a3ad163

Browse files
authored
Merge pull request #1283 from Kiraprint/add-params2get_document_route
Add new params for get_document route
2 parents dfac386 + 9081207 commit a3ad163

File tree

3 files changed

+101
-6
lines changed

3 files changed

+101
-6
lines changed

meilisearch_python_sdk/index.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,12 +1288,22 @@ async def search_similar_documents(
12881288

12891289
return SimilarSearchResults[self.hits_type](**response.json()) # type: ignore[name-defined]
12901290

1291-
async def get_document(self, document_id: str) -> JsonDict:
1291+
async def get_document(
1292+
self,
1293+
document_id: str,
1294+
*,
1295+
fields: list[str] | None = None,
1296+
retrieve_vectors: bool = False,
1297+
) -> JsonDict:
12921298
"""Get one document with given document identifier.
12931299
12941300
Args:
12951301
document_id: Unique identifier of the document.
1296-
1302+
fields: Document attributes to show. If this value is None then all
1303+
attributes are retrieved. Defaults to None.
1304+
retrieve_vectors: If set to True the embedding vectors will be returned with the document.
1305+
Defaults to False. Note: This parameter can only be
1306+
used with Meilisearch >= v1.13.0
12971307
Returns:
12981308
The document information
12991309
@@ -1307,7 +1317,16 @@ async def get_document(self, document_id: str) -> JsonDict:
13071317
>>> index = client.index("movies")
13081318
>>> document = await index.get_document("1234")
13091319
"""
1310-
response = await self._http_requests.get(f"{self._documents_url}/{document_id}")
1320+
parameters: JsonDict = {}
1321+
1322+
if fields:
1323+
parameters["fields"] = ",".join(fields)
1324+
if retrieve_vectors:
1325+
parameters["retrieveVectors"] = "true"
1326+
1327+
url = _build_encoded_url(f"{self._documents_url}/{document_id}", parameters)
1328+
1329+
response = await self._http_requests.get(url)
13111330

13121331
return response.json()
13131332

@@ -1318,6 +1337,7 @@ async def get_documents(
13181337
limit: int = 20,
13191338
fields: list[str] | None = None,
13201339
filter: Filter | None = None,
1340+
retrieve_vectors: bool = False,
13211341
) -> DocumentsInfo:
13221342
"""Get a batch documents from the index.
13231343
@@ -1328,6 +1348,9 @@ async def get_documents(
13281348
attributes are retrieved. Defaults to None.
13291349
filter: Filter value information. Defaults to None. Note: This parameter can only be
13301350
used with Meilisearch >= v1.2.0
1351+
retrieve_vectors: If set to True the vectors will be returned with each document.
1352+
Defaults to False. Note: This parameter can only be
1353+
used with Meilisearch >= v1.13.0
13311354
13321355
Returns:
13331356
Documents info.
@@ -1348,6 +1371,9 @@ async def get_documents(
13481371
"limit": limit,
13491372
}
13501373

1374+
if retrieve_vectors:
1375+
parameters["retrieveVectors"] = "true"
1376+
13511377
if not filter:
13521378
if fields:
13531379
parameters["fields"] = ",".join(fields)
@@ -5470,12 +5496,22 @@ def search_similar_documents(
54705496

54715497
return SimilarSearchResults[self.hits_type](**response.json()) # type: ignore[name-defined]
54725498

5473-
def get_document(self, document_id: str) -> JsonDict:
5499+
def get_document(
5500+
self,
5501+
document_id: str,
5502+
*,
5503+
fields: list[str] | None = None,
5504+
retrieve_vectors: bool = False,
5505+
) -> JsonDict:
54745506
"""Get one document with given document identifier.
54755507
54765508
Args:
54775509
document_id: Unique identifier of the document.
5478-
5510+
fields: Document attributes to show. If this value is None then all
5511+
attributes are retrieved. Defaults to None.
5512+
retrieve_vectors: If set to True the embedding vectors will be returned with the document.
5513+
Defaults to False. Note: This parameter can only be
5514+
used with Meilisearch >= v1.13.0
54795515
Returns:
54805516
The document information
54815517
@@ -5489,8 +5525,16 @@ def get_document(self, document_id: str) -> JsonDict:
54895525
>>> index = client.index("movies")
54905526
>>> document = index.get_document("1234")
54915527
"""
5492-
response = self._http_requests.get(f"{self._documents_url}/{document_id}")
5528+
parameters: JsonDict = {}
5529+
5530+
if fields:
5531+
parameters["fields"] = ",".join(fields)
5532+
if retrieve_vectors:
5533+
parameters["retrieveVectors"] = "true"
54935534

5535+
url = _build_encoded_url(f"{self._documents_url}/{document_id}", parameters)
5536+
5537+
response = self._http_requests.get(url)
54945538
return response.json()
54955539

54965540
def get_documents(
@@ -5500,6 +5544,7 @@ def get_documents(
55005544
limit: int = 20,
55015545
fields: list[str] | None = None,
55025546
filter: Filter | None = None,
5547+
retrieve_vectors: bool = False,
55035548
) -> DocumentsInfo:
55045549
"""Get a batch documents from the index.
55055550
@@ -5510,6 +5555,9 @@ def get_documents(
55105555
attributes are retrieved. Defaults to None.
55115556
filter: Filter value information. Defaults to None. Note: This parameter can only be
55125557
used with Meilisearch >= v1.2.0
5558+
retrieve_vectors: If set to True the vectors will be returned with each document.
5559+
Defaults to False. Note: This parameter can only be
5560+
used with Meilisearch >= v1.13.0
55135561
55145562
Returns:
55155563
Documents info.
@@ -5530,6 +5578,9 @@ def get_documents(
55305578
"limit": limit,
55315579
}
55325580

5581+
if retrieve_vectors:
5582+
parameters["retrieveVectors"] = "true"
5583+
55335584
if not filter:
55345585
if fields:
55355586
parameters["fields"] = ",".join(fields)

tests/test_async_documents.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,20 @@ async def test_get_document_inexistent(async_empty_index):
706706
await index.get_document("123")
707707

708708

709+
async def test_get_document_with_fields(async_index_with_documents):
710+
index = await async_index_with_documents()
711+
response = await index.get_document("500682", fields=["title", "overview"])
712+
assert len(response.keys()) == 2
713+
assert "title" in response.keys()
714+
assert "overview" in response.keys()
715+
716+
717+
async def test_get_document_with_vectors(async_index_with_documents):
718+
index = await async_index_with_documents()
719+
response = await index.get_document("500682", retrieve_vectors=True)
720+
assert "_vectors" in response.keys()
721+
722+
709723
async def test_get_documents_populated(async_index_with_documents):
710724
index = await async_index_with_documents()
711725
response = await index.get_documents()
@@ -744,6 +758,14 @@ async def test_get_documents_filter_with_fields(async_index_with_documents):
744758
assert next(iter(genres)) == "action"
745759

746760

761+
async def test_get_documents_with_vectors(async_index_with_documents):
762+
index = await async_index_with_documents()
763+
response = await index.update_filterable_attributes(["genre"])
764+
await async_wait_for_task(index.http_client, response.task_uid)
765+
response = await index.get_documents(retrieve_vectors=True)
766+
assert all("_vectors" in x.keys() for x in response.results)
767+
768+
747769
@pytest.mark.parametrize("compress", (True, False))
748770
async def test_update_documents(compress, async_index_with_documents, small_movies):
749771
index = await async_index_with_documents()

tests/test_documents.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,20 @@ def test_get_document_inexistent(empty_index):
626626
index.get_document("123")
627627

628628

629+
def test_get_document_with_fields(index_with_documents):
630+
index = index_with_documents()
631+
response = index.get_document("500682", fields=["title", "overview"])
632+
assert len(response.keys()) == 2
633+
assert "title" in response.keys()
634+
assert "overview" in response.keys()
635+
636+
637+
def test_get_document_with_vectors(index_with_documents):
638+
index = index_with_documents()
639+
response = index.get_document("500682", retrieve_vectors=True)
640+
assert "_vectors" in response.keys()
641+
642+
629643
def test_get_documents_populated(index_with_documents):
630644
index = index_with_documents()
631645
response = index.get_documents()
@@ -652,6 +666,14 @@ def test_get_documents_filter(index_with_documents):
652666
assert next(iter(genres)) == "action"
653667

654668

669+
def test_get_documents_with_vectors(index_with_documents):
670+
index = index_with_documents()
671+
response = index.update_filterable_attributes(["genre"])
672+
wait_for_task(index.http_client, response.task_uid)
673+
response = index.get_documents(retrieve_vectors=True)
674+
assert all("_vectors" in x.keys() for x in response.results)
675+
676+
655677
def test_get_documents_filter_with_fields(index_with_documents):
656678
index = index_with_documents()
657679
response = index.update_filterable_attributes(["genre"])

0 commit comments

Comments
 (0)