Skip to content

Commit 18e202b

Browse files
authored
Merge pull request #1492 from sanders41/get-documents-ids
Add ability to get documents by ids
2 parents a0d030a + f64ba3d commit 18e202b

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

meilisearch_python_sdk/index.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,7 @@ async def get_document(
13721372
async def get_documents(
13731373
self,
13741374
*,
1375+
ids: list[str] | None = None,
13751376
offset: int = 0,
13761377
limit: int = 20,
13771378
fields: list[str] | None = None,
@@ -1382,6 +1383,7 @@ async def get_documents(
13821383
"""Get a batch documents from the index.
13831384
13841385
Args:
1386+
ids: Array of document primary keys to retrieve. Defults to None (Gets all documents).
13851387
offset: Number of documents to skip. Defaults to 0.
13861388
limit: Maximum number of documents returnedd. Defaults to 20.
13871389
fields: Document attributes to show. If this value is None then all
@@ -1418,7 +1420,7 @@ async def get_documents(
14181420
if retrieve_vectors:
14191421
parameters["retrieveVectors"] = "true"
14201422

1421-
if not filter:
1423+
if not filter and not ids:
14221424
if fields:
14231425
parameters["fields"] = ",".join(fields)
14241426

@@ -1432,6 +1434,9 @@ async def get_documents(
14321434

14331435
parameters["filter"] = filter
14341436

1437+
if ids:
1438+
parameters["ids"] = ids
1439+
14351440
response = await self._http_requests.post(f"{self._documents_url}/fetch", body=parameters)
14361441

14371442
return DocumentsInfo(**response.json())
@@ -5640,6 +5645,7 @@ def get_document(
56405645
def get_documents(
56415646
self,
56425647
*,
5648+
ids: list[str] | None = None,
56435649
offset: int = 0,
56445650
limit: int = 20,
56455651
fields: list[str] | None = None,
@@ -5650,6 +5656,7 @@ def get_documents(
56505656
"""Get a batch documents from the index.
56515657
56525658
Args:
5659+
ids: Array of document primary keys to retrieve. Defults to None (Gets all documents).
56535660
offset: Number of documents to skip. Defaults to 0.
56545661
limit: Maximum number of documents returnedd. Defaults to 20.
56555662
fields: Document attributes to show. If this value is None then all
@@ -5686,7 +5693,7 @@ def get_documents(
56865693
if retrieve_vectors:
56875694
parameters["retrieveVectors"] = "true"
56885695

5689-
if not filter:
5696+
if not filter and not ids:
56905697
if fields:
56915698
parameters["fields"] = ",".join(fields)
56925699

@@ -5700,6 +5707,9 @@ def get_documents(
57005707

57015708
parameters["filter"] = filter
57025709

5710+
if ids:
5711+
parameters["ids"] = ids
5712+
57035713
response = self._http_requests.post(f"{self._documents_url}/fetch", body=parameters)
57045714

57055715
return DocumentsInfo(**response.json())

tests/test_async_documents.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,18 @@ async def test_get_documents_filter(async_index_with_documents):
748748
assert next(iter(genres)) == "action"
749749

750750

751+
async def test_get_documents_ids(async_index_with_documents):
752+
index = await async_index_with_documents()
753+
documents = await index.get_documents()
754+
assert len(documents.results) > 2
755+
ids = [documents.results[0]["id"], documents.results[1]["id"]]
756+
response = await index.get_documents(ids=ids)
757+
assert len(response.results) == 2
758+
retrieved_ids = [result["id"] for result in response.results]
759+
assert ids[0] in retrieved_ids
760+
assert ids[1] in retrieved_ids
761+
762+
751763
async def test_get_documents_filter_with_fields(async_index_with_documents):
752764
index = await async_index_with_documents()
753765
response = await index.update_filterable_attributes(["genre"])

tests/test_documents.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,18 @@ def test_get_documents_filter(index_with_documents):
668668
assert next(iter(genres)) == "action"
669669

670670

671+
def test_get_documents_ids(index_with_documents):
672+
index = index_with_documents()
673+
documents = index.get_documents()
674+
assert len(documents.results) > 2
675+
ids = [documents.results[0]["id"], documents.results[1]["id"]]
676+
response = index.get_documents(ids=ids)
677+
assert len(response.results) == 2
678+
retrieved_ids = [result["id"] for result in response.results]
679+
assert ids[0] in retrieved_ids
680+
assert ids[1] in retrieved_ids
681+
682+
671683
def test_get_documents_with_vectors(index_with_documents):
672684
index = index_with_documents()
673685
response = index.update_filterable_attributes(["genre"])

0 commit comments

Comments
 (0)