SearchByKeywords 用于在指定数据集索引中按关键词检索数据,返回与关键词相关的数据及相关性分数。
新接入关键词检索时,建议优先使用 bm25 模式。
如果请求中不指定 mode,系统会根据数据集配置自动选择模式:
请求向量数据库 VikingDB 的 OpenAPI 接口时,可以使用API Key(推荐)或者 AK、SK 构造签名进行鉴权。请参见数据面API调用流程,复制调用示例并填入必要信息。
| URI | /api/vikingdb/data/search/keywords | 统一资源标识符 |
|---|---|---|
| 方法 | POST | 客户端对向量数据库服务器请求的操作类型 |
| 请求头 | Content-Type: application/json | 请求消息类型 |
| Authorization: *** | 鉴权 |
仅列出本接口特有的参数。更多信息请参见检索公共参数。
| 参数名 | 必选 | 类型 | 备注 |
|---|---|---|---|
mode | 否 | string | 检索模式。可选值:
|
keywords | 是(二选一) | list | 关键词列表。 |
query | string | 检索语句,使用analyzer分析。 | |
| case_sensitive | 否 | bool | 是否大小写敏感。默认 false。仅对 semantic_then_match 模式生效。 |
fields | 否 | list | 只对 bm25 模式生效,用于指定检索哪些全文检索字段。 |
fields_weights | 否 | float | 只针对bm25搜索时生效。 |
bm25_k1 | 否 | map | 只针对bm25搜索时生效。 |
bm25_b | 否 | float | 只针对bm25搜索时生效。 |
highlight | 否 | map | 高亮信息配置,适用于需要在检索结果页标出关键词命中位置的场景。 |
见检索公共参数。
以下示例适用于已配置 FullText 的数据集。新接入关键词检索时,建议使用该模式。
req_path = "/api/vikingdb/data/search/keywords" req_body = { "collection_name": "test_coll_with_fulltext", "index_name": "idx_1", "keywords": ["机器学习", "检索", "排序"], "mode": "bm25", "fields": ["f_text"], "output_fields": ["f_text"], "limit": 5 }
{ "code": "Success", "message": "The API call was executed successfully.", "request_id": "02175438839168500000000000000000000ffff0a003ee4fc3499", "result": { "data": [ { "id": "uid_001", "fields": { "f_text": "机器学习检索" }, "score": 3.605649671017216 } ], "total_return_count": 1 } }
以下示例在 bm25 模式下开启高亮信息返回。
req_path = "/api/vikingdb/data/search/keywords" req_body = { "collection_name": "test_coll_with_fulltext", "index_name": "idx_1", "keywords": ["机器学习", "检索", "排序"], "mode": "bm25", "fields": ["f_text"], "output_fields": ["f_text"], "limit": 5, "highlight": { "enable": True } }
{ "code": "Success", "message": "The API call was executed successfully.", "request_id": "02175438839168500000000000000000000ffff0a003ee4fc3499", "result": { "data": [ { "id": "uid_001", "fields": { "f_text": "机器学习检索" }, "highlight": { "f_text": { "机器": [0], "学习": [2], "检索": [4] } }, "score": 3.605649671017216 } ], "total_return_count": 1 } }
""" 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="https://{}{}".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/keywords" req_body = { "collection_name": "test_coll_with_vectorize", "index_name": "idx_1", "keywords": ["火山", "向量", "检索", "亿"], "output_fields": [ "f_text" ], "limit": 5, } 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)
本接口的业务错误码、触发条件和处理建议统一参见向量库 V2 错误码。排查时请同时记录响应中的 request_id,以便定位具体请求。