You need to enable JavaScript to run this app.
文档中心
向量数据库VikingDB

向量数据库VikingDB

复制全文
检索(Search)
随机检索-SearchByRandom
复制全文
随机检索-SearchByRandom

概述

随机检索是一种在未指定查询内容的情况下,从数据集中随机返回若干条记录的检索方式。随机检索同样支持过滤和对检索结果的后处理,可用于对比召回效果、数据过滤等场景。

请求接口

请求向量数据库 VikingDB 的 OpenAPI 接口时,可以使用API Key(推荐)或者 AK、SK 构造签名进行鉴权。请参见数据面API调用流程,复制调用示例并填入必要信息

URI

/api/vikingdb/data/search/random

统一资源标识符

方法

POST

客户端对向量数据库服务器请求的操作类型

请求头

Content-Type: application/json

请求消息类型

Authorization: ***

鉴权

请求体参数

无特殊参数。更多信息请参见检索公共参数

请求响应示例
  • 请求参数
req_path = "/api/vikingdb/data/search/random"
req_body = {
    "collection_name": "test_coll",
    "index_name": "idx_1",
    "limit": 2
}
  • 响应参数
{
    "code": "Success",
    "message": "The API call was executed successfully.",
    "request_id": "02175438839168500000000000000000000ffff0a003ee4fc3499",
    "result": {
        "data": [
            {
                "id": "uid_001",
                "fields": {
                    "f_good_id": "uid_001", "f_price": 999000
                },
                "score": 1.000000,
                "ann_score": 1.000000
            },
            {
                "id": "uid_002",
                "fields": {
                    "f_good_id": "uid_002", "f_price": 309000
                },
                "score": 0.88232,
                "ann_score": 0.88232,
            }
        ],
        "total_return_count": 2
    }
}

请求模版
"""
pip3 install volcengine
"""

from volcengine.base.Request import Request
import requests, json


class ClientForDataApi:
    def __init__(self, api_key=None, host=None):
        """
        初始化ClientForDataApi类
        :param api_key: VikingDB的API Key
        :param host: VikingDB的API Host
        """
        if not api_key or not host:
            raise ValueError("api_key and host are required")
        self.api_key = api_key
        self.host = host

    def prepare_request(self, method, path, params=None, data=None):
        r = Request()
        r.set_shema("https")
        r.set_method(method)
        r.set_connection_timeout(10)
        r.set_socket_timeout(10)
        mheaders = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Host": self.host,
            "Authorization": f"Bearer {self.api_key}",
        }
        r.set_headers(mheaders)
        if params:
            r.set_query(params)
        r.set_host(self.host)
        r.set_path(path)
        if data is not None:
            r.set_body(json.dumps(data))
        return r

    def do_req(self, req_method, req_path, req_params, req_body):
        req = self.prepare_request(
            method=req_method, path=req_path, params=req_params, data=req_body
        )
        return requests.request(
            method=req.method,
            url="http://{}{}".format(self.host, req.path),
            headers=req.headers,
            data=req.body,
            timeout=10000,
        )


if __name__ == "__main__":
    import os
    client = ClientForDataApi(
        api_key=os.environ.get("VIKINGDB_API_KEY", "***"), # 从环境变量中获取API Key,若未设置则使用默认值
        host="api-vikingdb.vikingdb.cn-beijing.volces.com",  # 替换为您所在的域名
    )
    req_method = "POST"
    req_params = None
    req_path = "/api/vikingdb/data/search/random"
    req_body = {
        "collection_name": "test_coll",
        "index_name": "idx_1",
        "limit": 2,
    }
    result = client.do_req(
        req_method=req_method, req_path=req_path, req_params=req_params, req_body=req_body
    )
    print("req http status code: ", result.status_code)
    print("req result: \n", result.text)
最近更新时间:2026.03.11 16:52:48
这个页面对您有帮助吗?
有用
有用
无用
无用