數(shù)據(jù)推送 Demo
更新時(shí)間:
本文將介紹通過(guò)召回引擎版Python SDK客戶(hù)端將數(shù)據(jù)實(shí)時(shí)同步到向量檢索版的實(shí)例中,支持的推送操作有:add、delete。
add 操作示例
# -*- coding: utf-8 -*-
from alibabacloud_ha3engine import models, client
from Tea.exceptions import TeaException, RetryError
Config = models.Config(
endpoint="<API域名>", # // API域名,可在實(shí)例詳情頁(yè)>API入口 查看(需要去掉http://前綴)
instance_id="<實(shí)例id>", # // 實(shí)例id,可在實(shí)例詳情頁(yè)左上角查看,例:ha-cn-i7*****605
protocol="http",
access_user_name="<用戶(hù)名>", # // 用戶(hù)名,可在實(shí)例詳情頁(yè)>API入口 查看
access_pass_word="<用戶(hù)名密碼>" # // 用戶(hù)名密碼,可在實(shí)例詳情頁(yè)>API入口 修改
)
# 初始化 引擎客戶(hù)端
ha3EngineClient = client.Client(Config)
optionsHeaders = {}
def push():
# 文檔推送的數(shù)據(jù)源配置名稱(chēng),可在實(shí)例管理>配置中心>數(shù)據(jù)源配置 查看.
dataSourceName = "<數(shù)據(jù)源名稱(chēng)>"
# 文檔推送的文檔主鍵字段.
pkField = "id"
try:
# 文檔推送外層結(jié)構(gòu), 可添加對(duì)文檔操作的結(jié)構(gòu)體.結(jié)構(gòu)內(nèi)支持 一個(gè)或多個(gè)文檔操作內(nèi)容.
documentArrayList = []
# 添加文檔
# 添加一篇文檔,如果文檔已經(jīng)存在會(huì)先刪除然后再添加。
# =====================================================
add2DocumentFields = dict()
add2Document = dict()
# 更新文檔內(nèi)容信息, keyValue成對(duì)匹配.
# field_pk 字段需與 索引表結(jié)構(gòu)中的pkField 字段配置一致.
add2DocumentFields.__setitem__("id", 1) # 主鍵id,INT單值類(lèi)型
add2DocumentFields.__setitem__("name", "搜索") # STRING單值類(lèi)型
add2DocumentFields.__setitem__("str_arr", "a\x1Db\x1Dc\x1Dd") # STRING多值類(lèi)型,默認(rèn)使用ha3分割符^] 進(jìn)行切分(其對(duì)應(yīng)utf編碼為\x1D),也可以在配置表字段時(shí)輸入自定義多值分隔符
# 將文檔內(nèi)容添如 add2Document結(jié)構(gòu).
add2Document.__setitem__("fields", add2DocumentFields)
# 新增對(duì)應(yīng)的文檔命令: add
add2Document.__setitem__("cmd", "add")
documentArrayList.append(add2Document)
pushDocumentsRequestModel = models.PushDocumentsRequestModel(optionsHeaders, documentArrayList)
# 使用默認(rèn) 運(yùn)行時(shí)參數(shù)進(jìn)行請(qǐng)求
responseModel = ha3EngineClient.push_documents(dataSourceName, pkField, pushDocumentsRequestModel)
print(responseModel.body)
print(responseModel.body)
except TeaException as e:
print(f"send request with TeaException : {e}")
except RetryError as e:
print(f"send request with Connection Exception : {e}")
if __name__ == "__main__":
push()
delete 操作示例
# -*- coding: utf-8 -*-
from alibabacloud_ha3engine import models, client
from alibabacloud_tea_util import models as util_models
from Tea.exceptions import TeaException, RetryError
Config = models.Config(
endpoint="<API域名>", # // API域名,可在實(shí)例詳情頁(yè)>API入口 查看(需要去掉http://前綴)
instance_id="<實(shí)例id>", # // 實(shí)例id,可在實(shí)例詳情頁(yè)左上角查看,例:ha-cn-i7*****605
protocol="http",
access_user_name="<用戶(hù)名>", # // 用戶(hù)名,可在實(shí)例詳情頁(yè)>API入口 查看
access_pass_word="<用戶(hù)名密碼>", # // 用戶(hù)名密碼,可在實(shí)例詳情頁(yè)>API入口 修改
)
# 初始化 引擎客戶(hù)端
ha3EngineClient = client.Client(Config)
optionsHeaders = {}
def pushDoc():
# 文檔推送的數(shù)據(jù)源配置名稱(chēng),可在實(shí)例管理>配置中心>數(shù)據(jù)源配置 查看.
tableName = "<數(shù)據(jù)源名稱(chēng)>"
# 文檔推送的文檔主鍵字段.
pkField = "id"
try:
# 文檔推送外層結(jié)構(gòu), 可添加對(duì)文檔操作的結(jié)構(gòu)體.結(jié)構(gòu)內(nèi)支持 一個(gè)或多個(gè)文檔操作內(nèi)容.
documentArrayList = []
# 刪除文檔
# 刪除一篇文檔,刪除文檔時(shí)需要指定文檔主鍵,如果索引構(gòu)建時(shí)采用多級(jí)hash方式,需要指定每級(jí)hash的主鍵。
delete2Document = dict()
delete2DocumentFields = dict()
# 更新文檔內(nèi)容信息, keyValue成對(duì)匹配.
# field_pk 字段需與 pkField 字段配置一致.
delete2DocumentFields.__setitem__("id", "1") # 主鍵pk,STRING單值類(lèi)型
# 將文檔內(nèi)容添如 delete2Document 結(jié)構(gòu).
delete2Document.__setitem__("fields", delete2DocumentFields)
# 刪除對(duì)應(yīng)的文檔命令: delete
delete2Document.__setitem__("cmd", "delete")
documentArrayList.append(delete2Document)
pushDocumentsRequestModel = models.PushDocumentsRequestModel(
optionsHeaders, documentArrayList
)
# 使用默認(rèn) 運(yùn)行時(shí)參數(shù)進(jìn)行請(qǐng)求
responseModel = ha3EngineClient.push_documents(
tableName, pkField, pushDocumentsRequestModel
)
print(responseModel)
print(responseModel)
except TeaException as e:
print(f"send request with TeaException : {e}")
except RetryError as e:
print(f"send request with Connection Exception : {e}")
if __name__ == "__main__":
pushDoc()
文檔內(nèi)容是否對(duì)您有幫助?