Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.
For information about how to create an AccessKey pair, see Create an AccessKey pair.
If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.
We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.
Linux and macOS
Run the following commands. Replace
<access_key_id>and<access_key_secret>with the AccessKey ID and AccessKey secret of the RAM user that you use.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>Windows
Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.
Restart Windows for the AccessKey pair to take effect.
Code sample
// This file is auto-generated, don't edit it. Thanks.
package main
import (
"fmt"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
opensearch "main/client"
)
func main() {
// Create a client instance for requests.
// The Endpoint is the domain name of the region for the service you want to access.
// The AccessKey ID and AccessKey secret are used for authentication.
config := &opensearch.Config{
Endpoint: tea.String("<Endpoint>"),
// User identity information.
// Read the AccessKey ID and AccessKey secret from environment variables.
// Before you run the code sample, you must configure the environment variables. For more information, see the "Configure environment variables" section in this document.
// Replace with your AccessKey ID.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
// Create a client to send requests.
client, _clientErr := opensearch.NewClient(config)
// If an error occurs when you create the client, the system returns _client_err and prints the error message.
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
// The item_id is the primary key returned in the search results. This is the primary key ID.
itemId := "<item_id>"
// The ops_request_misc is the ops_request_misc information returned in the search request.
opsRequestMisc :="<ops_request_misc>"
// The bhv_type is the feature information of the behavioral event. The following values are supported:
// cart (add to shopping cart)
// collect (add to favorites)
// like (give a like)
// comment (post a comment)
// buy (make a purchase)
// click (click or view)
bhvType := "<bhv_type>"
// The requestId is the request_id information returned in the search request.
requestId :="<request_id>"
// The time when the data arrives at the server. Format: UNIX timestamp. Unit: seconds.
reachTime :="<reach_time>"
// The ID that uniquely identifies a user on the client application.
//* This is usually the ID of the logged-on user.
//* For a PC client, if the user is not logged on, you can set this to the cookie ID.
user_id:="<user_id>"
behavior_fields :=map[string]interface{}{
"item_id": itemId,
"sdk_type": "opensearch_sdk",
"sdk_version": "<sdk_version>", // The version number of the OpenSearch SDK that you are using.
"trace_id": "ALIBABA", // Used to identify which service provider is called.
"trace_info": opsRequestMisc,
"bhv_type": bhvType,
"item_type": "item",
"rn": requestId,
"biz_id":"<biz_id>", // A numeric ID used by a mobile phone or client application to distinguish services. You can use this field to associate with an application in OpenSearch or an instance in AIRec.
"reach_time": reachTime,
"user_id": user_id,
}
bhv_actions := []string{"like", "comment", "buy"}
if IsContain(bhv_actions,bhvType){ // If the behavior type is included in bhv_actions, you must provide more behavior information.
// The description of the behavior. Format: key=value{,key=value}
behavior_fields["bhv_detail"] ="<bhv_detail>"
// The value of the behavior, such as the duration of stay or the number of items purchased.
behavior_fields["bhv_value"] ="<bhv_value>"
}
behavior_document := map[string]interface{}{
"cmd": "ADD",
"fields": behavior_fields,
}
// Assemble the documents to be pushed into an array. The array contains a single document action.
requestBody := []interface{}{behavior_document}
// The configuration parameters for sending the request. These are used for request configuration and connection pool configuration.
runtime := &util.RuntimeOptions{
ConnectTimeout: tea.Int(5000),
ReadTimeout: tea.Int(10000),
Autoretry: tea.Bool(false),
IgnoreSSL: tea.Bool(false),
MaxIdleConns: tea.Int(50),
}
// The push API requires the app name and data collection name.
// appName is the name of the app. You cannot use a version to push data.
appName := "<appName>"
dataCollectionName := "<dataCollectionName>"
// The value of dataCollectionType is 'BEHAVIOR'. This is the default value.
dataCollectionType := "BEHAVIOR"
// Call the method to send the request.
_reponse, _request_err := client.Request(
tea.String("POST"),
tea.String("/v3/openapi/app-groups/"+appName+"/data-collections/"+dataCollectionName+"/data-collection-type/"+dataCollectionType+"/actions/bulk"),
nil,
nil,
requestBody,
runtime)
// If an error occurs when you send the request, the system returns _request_err and prints the error message.
if _request_err != nil {
fmt.Println(_request_err)
return
}
// Print the content of the normal response.
fmt.Println(_reponse)
}
func IsContain(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}