All Products
Search
Document Center

OpenSearch:Behavioral data push demo

Last Updated:Aug 21, 2026

This topic provides Go sample code for pushing behavioral data to an OpenSearch Industry Algorithm Edition application. Behavioral data—clicks, purchases, favorites, and similar user actions—feeds the recommendation engine and improves result relevance over time.

Prerequisites

Before you begin, make sure you have:

Configure environment variables

Set your AccessKey pair as environment variables so the sample code can read them at runtime. Never hard-code credentials in source files.

  • Linux and macOS: Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of your RAM user.

    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 ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET, and set their values to your AccessKey ID and AccessKey secret. Restart Windows for the variables to take effect.

Behavior fields

Every push request wraps one or more behavior documents. Each document contains a fields object with the behavior data.

Required fields

The following fields are required for all behavior types.

Field

Description

item_id

The primary key of the item returned in search results.

user_id

The unique ID of the user. For logged-in users, use the user ID. For anonymous users on PC clients, use the cookie ID.

bhv_type

The behavior type.

reach_time

The time when the server receives the data, as a UNIX timestamp accurate to the second.

trace_info

The ops_request_misc value returned in search results.

rn

The request ID returned in search results.

biz_id

The numeric ID that differentiates business lines in mobile or application clients. Associates the data with OpenSearch applications and Artificial Intelligence Recommendation (AIRec) instances.

sdk_type

Set to opensearch_sdk.

sdk_version

The version number of OpenSearch SDK for Go.

trace_id

Set to ALIBABA.

item_type

Set to item.

Conditional fields

The following fields are required only when bhv_type is like, comment, or buy.

Field

Description

bhv_detail

A description of the behavior. Format: key=value{,key=value}.

bhv_value

Numerical information about the behavior, such as stay duration or the number of items purchased.

Behavior types {#behavior-types}

Value

Description

Requires conditional fields

click

The user clicks or views an item.

No

cart

The user adds an item to the shopping cart.

No

collect

The user adds an item to favorites.

No

like

The user likes an item.

Yes

comment

The user posts a comment on an item.

Yes

buy

The user purchases an item.

Yes

Sample code

The sample code creates an SDK client, builds a behavior document, and sends it to the OpenSearch bulk push endpoint using a POST request.

// 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() {
  // Configure the client with your endpoint and credentials from environment variables.
  // endpoint: the OpenSearch API endpoint for your region.
  config := &opensearch.Config{
      Endpoint:        tea.String("<Endpoint>"),

      // Read credentials from environment variables set in the "Configure environment variables" section.
      AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
      AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }

  client, _clientErr := opensearch.NewClient(config)
  if _clientErr != nil {
      fmt.Println(_clientErr)
      return
  }

  // item_id: the primary key of the item returned in search results.
  itemId := "<item_id>"

  // trace_info: the ops_request_misc value returned in search results.
  opsRequestMisc := "<ops_request_misc>"

  // bhv_type: the behavior type. Valid values: cart, collect, like, comment, buy, click.
  bhvType := "<bhv_type>"

  // rn: the request ID returned in search results.
  requestId := "<request_id>"

  // reach_time: UNIX timestamp (seconds) when the server receives the data.
  reachTime := "<reach_time>"

  // user_id: the unique ID of the user.
  // For logged-in users, use the user ID. For anonymous PC users, use the cookie ID.
  user_id := "<user_id>"

  behavior_fields := map[string]interface{}{
      "item_id":     itemId,
      "sdk_type":    "opensearch_sdk",
      "sdk_version": "<sdk_version>",  // OpenSearch SDK for Go version number.
      "trace_id":    "ALIBABA",        // Service provider identifier.
      "trace_info":  opsRequestMisc,
      "bhv_type":    bhvType,
      "item_type":   "item",
      "rn":          requestId,
      "biz_id":      "<biz_id>",       // Numeric business ID for mobile or application clients.
      "reach_time":  reachTime,
      "user_id":     user_id,
  }

  // For like, comment, and buy behaviors, include bhv_detail and bhv_value.
  bhv_actions := []string{"like", "comment", "buy"}
  if IsContain(bhv_actions, bhvType) {
      behavior_fields["bhv_detail"] = "<bhv_detail>"  // Format: key=value{,key=value}
      behavior_fields["bhv_value"]  = "<bhv_value>"   // Numerical behavior info, e.g. purchase quantity.
  }

  behavior_document := map[string]interface{}{
      "cmd":    "ADD",
      "fields": behavior_fields,
  }

  requestBody := []interface{}{behavior_document}

  // Specify the parameters that are used to configure the request and connection pool.
  runtime := &util.RuntimeOptions{
      ConnectTimeout: tea.Int(5000),
      ReadTimeout:    tea.Int(10000),
      Autoretry:      tea.Bool(false),
      IgnoreSSL:      tea.Bool(false),
      MaxIdleConns:   tea.Int(50),
  }

  // appName: the name of the OpenSearch application (not the version name).
  // dataCollectionName: the name of the data collection in the application.
  appName := "<appName>"
  dataCollectionName := "<dataCollectionName>"

  // dataCollectionType: set to BEHAVIOR for behavioral data.
  dataCollectionType := "BEHAVIOR"

  _response, _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 _request_err != nil {
      fmt.Println(_request_err)
      return
  }

  fmt.Println(_response)
}

func IsContain(items []string, item string) bool {
   for _, eachItem := range items {
       if eachItem == item {
           return true
       }
   }
   return false
}