Push behavioral data to OpenSearch to enable personalized search rankings and recommendation model training. Each event type — click, buy, cart, and others — feeds specific OpenSearch capabilities. Sending the right event for each user action determines how well these features perform.
This page provides TypeScript sample code for the push. All packages are available on npm.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch application with a data collection configured
An AccessKey ID and AccessKey secret (for
access_keyauthentication), or an STS token (for Resource Access Management (RAM) and Security Token Service (STS) authentication)The endpoint of your OpenSearch application (available in the OpenSearch console)
Install dependencies
Add the following to your package.json:
dependencies
@alicloud/credentials
@alicloud/opensearch-util
@alicloud/tea-typescript
@alicloud/tea-utildevDependencies
typescript
ts-nodeBehavior event types
Use the bhv_type field to specify the user action being recorded. Choose the value that matches the event you want to capture:
| Event type | User action |
|---|---|
click | Clicks or views an item |
cart | Adds an item to the shopping cart |
buy | Purchases an item |
collect | Adds an item to favorites |
like | Likes an item |
comment | Posts a comment on an item |
Sample code
The following sample shows a complete flow: initialize the client, construct a behavior event, and push it to OpenSearch using the bulk API.
import * as $Util from '@alicloud/tea-util';
import Client from "./Client";
import Config from "./Config";
// Initialize the client
let config = new Config();
// Set the endpoint. Get the endpoint from the OpenSearch console.
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";
// Set the request protocol. Valid values: HTTP, HTTPS. Default: HTTP.
config.protocol = "HTTP";
// Set the authentication type. Valid values: access_key, sts. Default: access_key.
// Use sts for RAM and STS authentication.
config.type = "access_key";
// Set your AccessKey pair.
config.accessKeyId = "<your-access-key-id>";
config.accessKeySecret = "<your-access-key-secret>";
// Set runtime options. Time values are in milliseconds.
let runtime = new $Util.RuntimeOptions({
connectTimeout: 5000,
readTimeout: 10000,
autoretry: false,
ignoreSSL: false,
maxIdleConns: 50,
});
let client = new Client(config);
// Specify the application name and data collection name.
const appName = "<your-app-name>";
const collectionName = "<your-collection-name>";
// --------------- Construct the behavior event ---------------
// item_id: the primary key of the item returned in search results.
const item_id = "358713";
// ops_request_misc: the request miscellaneous field returned in search results.
const ops_request_misc = "%7B%22request%5Fid%22%3A%22161777635816780357273903%22%2C%22scm%22%3A%2220140713.130149759..%22%7D";
// bhv_type: the event type. See the behavior event types table above.
const bhv_type = "click";
// request_id: the request ID returned in search results.
const request_id = "161777635816780357273903";
// reach_time: the UNIX timestamp (accurate to the second) when the data is received by the server.
const reach_time = "1709708439";
// user_id: the unique ID of the user.
// For logged-in users, use the account ID. For anonymous users on PC, use the cookie ID.
const user_id = "a7a0d37c824b659f36a5b9e3b819fcdd";
let behavior_fields = {
"item_id": item_id,
"sdk_type": "opensearch_sdk",
"sdk_version": "<sdk-version>", // OpenSearch SDK for TypeScript version number
"trace_id": "ALIBABA", // Service provider identifier
"trace_info": ops_request_misc,
"bhv_type": bhv_type,
"item_type": "item",
"rn": request_id,
"biz_id": "<biz-id>", // Numerical ID for differentiating business lines across mobile apps or clients. Can be linked to OpenSearch applications and Artificial Intelligence Recommendation (AIRec) instances.
"reach_time": reach_time,
"user_id": user_id,
};
// --------------- Push the behavior event ---------------
let behavior_documents = [{ "cmd": "add", "fields": behavior_fields }];
try {
let pathname = `/v3/openapi/app-groups/${appName}/data-collections/${collectionName}/data-collection-type/BEHAVIOR/actions/bulk`;
let result = client._request("POST", pathname, null, null, behavior_documents, runtime);
result.then(function (result) {
console.log(result);
});
} catch (e) {
console.log(e);
}Replace the placeholders before running the code:
| Placeholder | Description | Example |
|---|---|---|
<your-access-key-id> | AccessKey ID | LTAI5tXxx |
<your-access-key-secret> | AccessKey secret | xXxXxXx |
<your-app-name> | Name or version of the OpenSearch application | my-search-app |
<your-collection-name> | Name of the data collection in the application | behavior |
<sdk-version> | Version number of OpenSearch SDK for TypeScript | 1.0.0 |
<biz-id> | Numerical business identifier | 1001 |
Using STS authentication
If your application uses RAM and STS authentication instead of an AccessKey pair, set config.type to sts and provide the STS token:
config.type = "sts";
config.securityToken = "<your-sts-token>"; // Obtain via the AssumeRole operation of RAM
config.accessKeyId = "<your-sts-access-key-id>";
config.accessKeySecret = "<your-sts-access-key-secret>";What's next
Push collected data — Learn about the full data push process and field requirements.