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 Java.
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
// The DashVectorCollection class.
// The method working in synchronous mode.
public Response<Map<String, Doc>> fetch(FetchDocRequest fetchDocRequest);
// The method working in asynchronous mode.
public ListenableFuture<Response<Map<String, Doc>>> fetchAsync(FetchDocRequest fetchDocRequest);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
quickstartin 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 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 {
DashVectorClient client = new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");
DashVectorCollection collection = client.get("quickstart");
// Build a FetchDocRequest object.
FetchDocRequest request = FetchDocRequest.builder()
.id("1")
.build();
// Send the document obtaining request.
Response<Map<String, Doc>> response = collection.fetch(request);
System.out.println(response);
// Output example:
// {
// "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
// }
// }
// }
}
}
Request parameters
You can use a FetchDocRequestBuilder instance to build a FetchDocRequest object. The following table describes the methods that can be called by the instance.
Method | Required | Default value | Description |
ids(List<String> ids) | Yes | - | Sets the primary key or a list of primary keys of documents. |
id(String id) | No | - | |
partition(String partition) | No | default | Sets the name of the partition. |
build() | - | - | Builds a |
Response parameters
A Response<Map<String, Doc>> object is returned, which contains the operation result, as described in the following table.
Method | Type | Description | Example |
getCode() | int | The returned status code. For more information, see Status codes. | 0 |
getMessage() | String | The returned message. | success |
getRequestId() | String | The unique ID of the request. | 19215409-ea66-4db9-8764-26ce2eb5bb99 |
getOutput() | Map<String, Doc> | The map with the key set to the primary key and the value set to the Doc object. | |
isSuccess() | Boolean | Specifies whether the operation is successful. | true |