This topic describes how to obtain one or more documents in a collection based on the document ID or ID list by using the SDK for Python.
If a specified ID does not exist, the corresponding output is empty.
Prerequisites
A cluster is created. For more information, see Create a cluster.
An API key is obtained. For more information, see Manage API keys.
The SDK of the latest version is installed. For more information, see Install DashVector SDK.
API definition
collection.fetch(
ids: Union[str, List[str]],
partition: Optional[str] = None,
async_req: bool = False
) -> DashVectorResponse
Example
You need to replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with the endpoint of your cluster in the sample code for the code to run properly.
You need to create a collection named
quickstart
in advance. For more information, see the "Example" section of the Create a collection topic. You also need to insert some documents in advance. For more information, see Insert documents.
import dashvector
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
collection = client.get(name='quickstart')
doc_id = '1'
docs = collection.fetch(doc_id)
# Check whether the fetch method is successfully called.
if docs:
print('fetch success')
# Check whether the ID of the document to be obtained exists. If not, the corresponding output is empty.
if doc_id in docs:
doc = docs[doc_id]
print(doc.id)
print(doc.vector)
print(doc.fields)
# Traverse all documents and return results.
for id in docs:
print(docs[id])
# Obtain multiple documents at a time.
docs = collection.fetch(['1','2'])
Request parameters
Parameter | Type | Default value | Description |
ids | Union[Union[str, int], List[Union[str, int]]] | - | The primary key or primary key list. |
partition | Optional[str] | None | Optional. The name of the partition. |
async_req | bool | False | Optional. Specifies whether to enable the asynchronous mode. |
Response parameters
A DashVectorResponse object is returned, which contains the operation result, as described in the following table.
Parameter | Type | Description | Example |
code | int | The returned status code. For more information, see Status codes. | 0 |
message | str | The returned message. | success |
request_id | str | The unique ID of the request. | 19215409-ea66-4db9-8764-26ce2eb5bb99 |
output | Dict[str, Doc] | The dictionary with the key set to the primary key and the value set to a Doc object. |