Fetch a doc

更新时间:
复制 MD 格式

Retrieve one or more documents from a collection by primary key using the DashVector Java SDK. If a specified ID does not exist, the corresponding output is empty.

Prerequisites

Before you begin, make sure that you have:

API definition

The DashVectorCollection class provides both synchronous and asynchronous methods for fetching documents:

// Synchronous
public Response<Map<String, Doc>> fetch(FetchDocRequest fetchDocRequest);

// Asynchronous
public ListenableFuture<Response<Map<String, Doc>>> fetchAsync(FetchDocRequest fetchDocRequest);

Request parameters

Use FetchDocRequestBuilder to construct a FetchDocRequest object.

Method

Required

Default value

Description

ids(List<String> ids)

Yes

-

The primary keys of the documents to fetch.

id(String id)

No

-

A single document primary key.

partition(String partition)

No

default

The partition to fetch from.

build()

-

-

Builds the FetchDocRequest object.

Example

This example fetches a single document by ID from the quickstart collection.

Replace the placeholders with your actual values:

Placeholder Description
<YOUR_API_KEY> Your DashVector API key
<YOUR_CLUSTER_ENDPOINT> Your cluster endpoint
Note

This example requires a collection named quickstart with existing documents. See Create a collection and Insert documents.

import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.DashVectorCollection;
import com.aliyun.dashvector.common.DashVectorException;
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.FetchDocRequest;
import com.aliyun.dashvector.models.responses.Response;

import java.util.Map;

public class Main {
    public static void main(String[] args) throws DashVectorException {
        // Initialize the client
        DashVectorClient client = new DashVectorClient("<YOUR_API_KEY>", "<YOUR_CLUSTER_ENDPOINT>");
        DashVectorCollection collection = client.get("quickstart");

        // Build a fetch request for document ID "1"
        FetchDocRequest request = FetchDocRequest.builder()
            .id("1")
            .build();

        // Fetch the document
        Response<Map<String, Doc>> response = collection.fetch(request);

        System.out.println(response);
    }
}

Sample response:

{
    "code": 0,
    "message": "Success",
    "requestId": "489c5cda-3ffc-4171-b6e0-1837b932962b",
    "output": {
        "1": {
            "id": "1",
            "vector": {"value": [0.1, 0.2, 0.3, 0.4]},
            "fields": {
                "name": "zhangsan",
                "age": 20,
                "weight": 100.0,
                "anykey1": "String",
                "anykey2": 1,
                "anykey3": true,
                "anykey4": 3.1415926
            },
            "score": 0
        }
    }
}

Response parameters

The fetch method returns a Response<Map<String, Doc>> object.

Method

Type

Description

Example

getCode()

int

The status code. For more information, see Status codes.

0

getMessage()

String

The response message.

success

getRequestId()

String

The unique request ID.

19215409-ea66-4db9-8764-26ce2eb5bb99

getOutput()

Map<String, Doc>

A map of primary keys to Doc objects.

See the sample response above.

getUsage()

RequestUsage

For a doc fetch request to a collection in a pay-as-you-go serverless instance, this method returns the number of read request units (RUs) consumed if the request is successful.

isSuccess()

Boolean

Whether the operation succeeded.

true