Go SDK

更新时间: 2025-03-03 18:15:59

提供Go语言仅对流量API接口的封装(数据处理、搜索),其他功能请使用管控SDK。

获取依赖

go get github.com/aliyun/alibabacloud-ha3-go-sdk

Git地址:github.com/aliyun/alibabacloud-ha3-go-sdk

重要

go sdk 目前仅支持在VPC 环境访问。

数据推送 Demo

代码示例

package main

import (
    "fmt"
    util "github.com/alibabacloud-go/tea-utils/service"
    "github.com/alibabacloud-go/tea/tea"
    ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {
    
    //创建请求用客户端实例
    //Endpoint 为 要访问服务的区域实例域名.
    //AccessUserName AccessPassWord 用于构造鉴权信息.
    
    config := &ha3engine.Config{
        Endpoint:       tea.String("<Endpoint>"),
        InstanceId:     tea.String("<InstanceId>"),
        AccessUserName: tea.String("<AccessUserName>"),
        AccessPassWord: tea.String("<AccessPassWord>"),
    }
    
    // New  一个client, 用以发送请求.
    client, _clientErr := ha3engine.NewClient(config)
    
    // 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
    if _clientErr != nil {
        fmt.Println(_clientErr)
        return
    }
    runtime := &util.RuntimeOptions{
        ConnectTimeout: tea.Int(5000),
        ReadTimeout:    tea.Int(10000),
        Autoretry:      tea.Bool(false),
        IgnoreSSL:      tea.Bool(false),
        MaxIdleConns:   tea.Int(50),
        HttpProxy:      tea.String("http://116.*.*.187:8088"),
    }
    docPush(client)
    docPushWithOptions(client, runtime)
}

func docPush(client *ha3engine.Client) {
    pushDocumentsRequestModel := &ha3engine.PushDocumentsRequestModel{}
    // # dataSourceName文档推送的数据源配置名称,可在实例管理>配置中心>数据源配置 查看.
    dataSourceName := "<数据源名称>"
    keyField := "id"
    
    a := [1000]int{}
    b := [10]int{}
    for x := range a {
        array := []map[string]interface{}{
        }
        for j := range b {
            filed := map[string]interface{}{
                "fields": map[string]interface{}{
                    "id":          tea.ToString(x*100) + tea.ToString(j),
                    "fb_boolean":  tea.BoolValue(nil),
                    "fb_datetime": "2167747200000",
                    "fb_string":   "409a6b18-a10b-409e-af91-07121c45d899",
                },
                "cmd": tea.String("add"),
            }
            array = append(array, filed)
        }
        
        pushDocumentsRequestModel.SetBody(array)
        
        // 发送请求的方法调用.
        response1, _requestErr1 := client.PushDocuments(tea.String(dataSourceName), tea.String(keyField), pushDocumentsRequestModel)
        
        // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
        if _requestErr1 != nil {
            fmt.Println(_requestErr1)
            return
        }
        
        // 输出正常返回的 response 内容.
        fmt.Println(response1)
        
    }
    
}

func docPushWithOptions(client *ha3engine.Client, runtime *util.RuntimeOptions) {
    
    pushDocumentsRequestModel := &ha3engine.PushDocumentsRequestModel{}
    dataSourceName := "{InstanceId}_odps"
    keyField := "id"
    
    a := [1000]int{}
    b := [10]int{}
    for x := range a {
        array := []map[string]interface{}{
        }
        for j := range b {
            filed := map[string]interface{}{
                "fields": map[string]interface{}{
                    "id":          tea.ToString(x*100) + tea.ToString(j),
                    "fb_boolean":  tea.BoolValue(nil),
                    "fb_datetime": "2167747200000",
                    "fb_string":   "409a6b18-a10b-409e-af91-07121c45d899",
                },
                "cmd": tea.String("add"),
            }
            array = append(array, filed)
        }
        
        pushDocumentsRequestModel.SetBody(array)
        
        // 发送请求的方法调用.
        response1, _requestErr1 := client.PushDocumentsWithOptions(tea.String(dataSourceName), tea.String(keyField), pushDocumentsRequestModel, runtime)
        
        // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
        if _requestErr1 != nil {
            fmt.Println(_requestErr1)
            return
        }
        
        // 输出正常返回的 response 内容.
        fmt.Println(response1)
        
    }
}

文档搜索 Demo

代码示例

package main

import (
    "fmt"
    util "github.com/alibabacloud-go/tea-utils/service"
    "github.com/alibabacloud-go/tea/tea"
    ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {
    
    //创建请求用客户端实例
    //Endpoint 为 要访问服务的区域实例域名.
    //AccessUserName AccessPassWord 用于构造鉴权信息.
    
    config := &ha3engine.Config{
        Endpoint:       tea.String("<Endpoint>"),
        InstanceId:     tea.String("<InstanceId>"),
        AccessUserName: tea.String("<AccessUserName>"),
        AccessPassWord: tea.String("<AccessPassWord>"),
    }
    
    // New  一个client, 用以发送请求.
    client, _clientErr := ha3engine.NewClient(config)
    
    // 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
    if _clientErr != nil {
        fmt.Println(_clientErr)
        return
    }
    runtime := &util.RuntimeOptions{
        ConnectTimeout: tea.Int(5000),
        ReadTimeout:    tea.Int(10000),
        Autoretry:      tea.Bool(false),
        IgnoreSSL:      tea.Bool(false),
        MaxIdleConns:   tea.Int(50),
        HttpProxy:      tea.String("http://116.*.*.187:8088"),
    }
    
    HastringQuerySearch(client)
    HastringQuerySearchWithOptions(client, runtime)
    HaStructQuerySearch(client)
    HaStructQuerySearchWithOptions(client, runtime)
    SQLstringQuerySearch(client)
    SQLstringQuerySearchWithOptions(client, runtime)
    SQLStructQuerySearch(client)
    SQLStructQuerySearchWithOptions(client, runtime)
}

func HastringQuerySearch(client *ha3engine.Client) {
    
    searchRequestModel := &ha3engine.SearchRequestModel{}
    searchQuery := &ha3engine.SearchQuery{}
    aaa := "config=start:0,hit:10,format:json,fetch_summary_type:pk,qrs_chain:search&&query=id:'5335540507182487716'&&cluster=general"
    fmt.Println(aaa)
    searchQuery.SetQuery(aaa)
    
    searchRequestModel.SetQuery(searchQuery)
    
    response, _requestErr := client.Search(searchRequestModel)
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response)
    
}

func HastringQuerySearchWithOptions(client *ha3engine.Client, runtime *util.RuntimeOptions) {
    
    searchRequestModel := &ha3engine.SearchRequestModel{}
    searchQuery := &ha3engine.SearchQuery{}
    aaa := "config=start:0,hit:10,format:json,fetch_summary_type:pk,qrs_chain:search&&query=id:'5335540507182487716'&&cluster=general"
    fmt.Println(aaa)
    searchQuery.SetQuery(aaa)
    
    searchRequestModel.SetQuery(searchQuery)
    
    response, _requestErr := client.SearchWithOptions(searchRequestModel, runtime)
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response)
    
}

func HaStructQuerySearch(client *ha3engine.Client) {
    
    searchRequestModel := &ha3engine.SearchRequestModel{}
    searchQuery := &ha3engine.SearchQuery{}
    
    haquery := &ha3engine.HaQuery{}
    haquery.SetQuery("id:'5335540507182487716'")
    haquery.SetCluster("general")
    haQueryconfigClause := &ha3engine.HaQueryconfigClause{}
    haQueryconfigClause.SetFormat("json")
    haQueryconfigClause.SetHit("10")
    haQueryconfigClause.SetStart("0")
    CustomConfig := map[string]*string{
        "fetch_summary_type": tea.String("pk"),
        "qrs_chain":          tea.String("search"),
    }
    
    haQueryconfigClause.SetCustomConfig(CustomConfig)
    haquery.SetConfig(haQueryconfigClause)
    
    result, _err := client.BuildHaSearchQuery(haquery)
    if _err != nil {
        fmt.Println(_err)
        return
    }
    fmt.Println(tea.StringValue(result))
    searchQuery.SetQuery(tea.StringValue(result))
    
    searchRequestModel.SetQuery(searchQuery)
    
    response, _requestErr := client.Search(searchRequestModel)
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response)
    
}

func HaStructQuerySearchWithOptions(client *ha3engine.Client, runtime *util.RuntimeOptions) {
    
    searchRequestModel := &ha3engine.SearchRequestModel{}
    searchQuery := &ha3engine.SearchQuery{}
    
    haquery := &ha3engine.HaQuery{}
    haquery.SetQuery("id:'5335540507182487716'")
    haquery.SetCluster("general")
    haQueryconfigClause := &ha3engine.HaQueryconfigClause{}
    haQueryconfigClause.SetFormat("json")
    haQueryconfigClause.SetHit("10")
    haQueryconfigClause.SetStart("0")
    CustomConfig := map[string]*string{
        "fetch_summary_type": tea.String("pk"),
        "qrs_chain":          tea.String("search"),
    }
    
    haQueryconfigClause.SetCustomConfig(CustomConfig)
    haquery.SetConfig(haQueryconfigClause)
    
    result, _err := client.BuildHaSearchQuery(haquery)
    if _err != nil {
        fmt.Println(_err)
        return
    }
    fmt.Println(tea.StringValue(result))
    searchQuery.SetQuery(tea.StringValue(result))
    
    searchRequestModel.SetQuery(searchQuery)
    
    response, _requestErr := client.SearchWithOptions(searchRequestModel, runtime)
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response)
    
}

func SQLstringQuerySearch(client *ha3engine.Client) {
    
    searchRequestModel := &ha3engine.SearchRequestModel{}
    searchQuery := &ha3engine.SearchQuery{}
    
    searchQuery.SetSql("select * from odps")
    searchRequestModel.SetQuery(searchQuery)
    
    // 发送请求的方法调用.
    response, _requestErr := client.Search(searchRequestModel)
    
    //
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response)
    
}

func SQLstringQuerySearchWithOptions(client *ha3engine.Client, runtime *util.RuntimeOptions) {
    
    searchRequestModel := &ha3engine.SearchRequestModel{}
    searchQuery := &ha3engine.SearchQuery{}
    
    searchQuery.SetSql("select * from odps")
    searchRequestModel.SetQuery(searchQuery)
    
    // 发送请求的方法调用.
    response, _requestErr := client.SearchWithOptions(searchRequestModel, runtime)
    
    //
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response)
    
}

func SQLStructQuerySearch(client *ha3engine.Client) {
    
    searchQuery1 := &ha3engine.SearchQuery{}
    sqlquery := &ha3engine.SQLQuery{}
    sqlquery.SetQuery("select * from odps")
    kvPair := map[string]*string{
        "format": tea.String("json"),
    }
    
    sqlquery.SetKvpairs(kvPair)
    
    result, _err := client.BuildSQLSearchQuery(sqlquery)
    if _err != nil {
        fmt.Println(_err)
        return
    }
    searchQuery1.SetSql(tea.StringValue(result))
    
    searchRequestModel1 := &ha3engine.SearchRequestModel{}
    searchRequestModel1.SetQuery(searchQuery1)
    
    // 发送请求的方法调用.
    response1, _requestErr1 := client.Search(searchRequestModel1)
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr1 != nil {
        fmt.Println(_requestErr1)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response1)
    
}

func SQLStructQuerySearchWithOptions(client *ha3engine.Client, runtime *util.RuntimeOptions) {
    
    searchQuery1 := &ha3engine.SearchQuery{}
    sqlquery := &ha3engine.SQLQuery{}
    sqlquery.SetQuery("select * from odps")
    kvPair := map[string]*string{
        "format": tea.String("json"),
    }
    
    sqlquery.SetKvpairs(kvPair)
    
    result, _err := client.BuildSQLSearchQuery(sqlquery)
    if _err != nil {
        fmt.Println(_err)
        return
    }
    searchQuery1.SetSql(tea.StringValue(result))
    
    searchRequestModel1 := &ha3engine.SearchRequestModel{}
    searchRequestModel1.SetQuery(searchQuery1)
    
    // 发送请求的方法调用.
    response1, _requestErr1 := client.SearchWithOptions(searchRequestModel1, runtime)
    
    // 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
    if _requestErr1 != nil {
        fmt.Println(_requestErr1)
        return
    }
    
    // 输出正常返回的 response 内容.
    fmt.Println(response1)
    
}

注意事项

上一篇: restful api文档搜索Demo 下一篇: 数据结构
阿里云首页 智能开放搜索 OpenSearch 相关技术圈