文档

获取Doc

更新时间:

本文介绍如何通过Java SDK,根据id或id列表获取Collection中已存在的Doc。

说明

如果指定id不存在,则该id对应的Doc为空。

前提条件

接口定义

// class DashVectorCollection

// 同步接口
public Response<Map<String, Doc>> fetch(FetchDocRequest fetchDocRequest);

// 异步接口
public ListenableFuture<Response<Map<String, Doc>>> fetchAsync(FetchDocRequest fetchDocRequest);

使用示例

说明
  1. 需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。

  1. 本示例需要参考新建Collection-使用示例提前创建好名称为quickstart的Collection,并参考插入Doc提前插入部分数据。

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");

        // 构建 FetchDocRequest
        FetchDocRequest request = FetchDocRequest.builder()
            .id("1")
            .build();

        // 发送获取Doc请求
        Response<Map<String, Doc>> response = collection.fetch(request);

        System.out.println(response);
        // example output:
        // {
        //     "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
        //         }
        //     }
        // }
    }
}

入参描述

通过FetchDocRequestBuilder构造FetchDocRequest对象,其可用方法如下表所示:

方法

必填

默认值

描述

ids(List<String> ids)

-

文档主键列表

id(String id)

-

partition(String partition)

default

分区名称

build()

-

-

构造FetchDocRequest对象

出参描述

说明

返回结果为Response<Map<String, Doc>>对象,Response<Map<String, Doc>>对象中可获取本次操作结果信息,如下表所示。

方法

类型

描述

示例

getCode()

int

返回值,参考返回状态码说明

0

getMessage()

String

返回消息

success

getRequestId()

String

请求唯一id

19215409-ea66-4db9-8764-26ce2eb5bb99

getOutput()

Map<String, Doc>

key为主键,value为对应Doc的Map

{
  "1":{
    "id":"1",
    "vector":{"value":[0.1,0.2,0.3,0.4]},
    "fields":{"name":"zhangsan"},
    "score":0
  }
}

isSuccess()

Boolean

判断请求是否成功

true

  • 本页导读 (0)
文档反馈