Fetch docs
This topic describes how to use the Python software development kit (SDK) to fetch existing docs in a collection by an ID or a list of IDs.
If a specified ID does not exist, the corresponding doc 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 latest version of the SDK is installed. For more information, see Install the DashVector SDK.
API definition
collection.fetch(
ids: Union[str, List[str]],
partition: Optional[str] = None,
async_req: bool = False
) -> DashVectorResponseExamples
Replace `YOUR_API_KEY` with your API key and `YOUR_CLUSTER_ENDPOINT` with your cluster endpoint to ensure that the code runs correctly.
For this example, you must first create a collection named
quickstartas described in Create a Collection - Example and insert data as described in Insert a Doc.
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 if the fetch operation is successful.
if docs:
print('fetch success')
# Check if the fetched doc exists. If the specified ID does not exist, the output is empty.
if doc_id in docs:
doc = docs[doc_id]
print(doc.id)
print(doc.vector)
print(doc.fields)
# Traverse the returned results.
for id in docs:
print(docs[id])
# Batch fetch.
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 a list of primary keys. |
partition (Optional) | Optional[str] | None | The partition name. |
async_req (Optional) | bool | False | Specifies whether the operation is asynchronous. |
Response parameters
The operation returns a `DashVectorResponse` object. This object contains the results of the operation, as described in the following table.
Field | Type | Description | Example |
code | int | The return value. 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] | A dictionary where each key is a primary key and the corresponding value is the Doc object. | |
usage | For a doc fetch request on a collection in a Serverless (pay-as-you-go) instance, this field returns the number of read request units consumed by the successful operation. |