All Products
Search
Document Center

OpenSearch:Demo code for querying top searches

Last Updated:Feb 20, 2023

Download packages: https://www.npmjs.com/

Add dependencies

  • dependencies

@alicloud/credentials 
@alicloud/opensearch-util 
@alicloud/tea-typescript 
@alicloud/tea-util
  • devDependencies

typescript
ts-node

Sample code

import * as $Util from '@alicloud/tea-util';
import Client from "./Client";
import Config from "./Config";

// Create a Config instance.
let config  = new Config();

// Specify the endpoint of the OpenSearch API. You can obtain the endpoint from the OpenSearch console.
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";

// Specify the request protocol. Default value: HTTP. Valid values: HTTPS and HTTP.
config.protocol = "HTTP";

// Specify the authentication method. Default value: access_key. A value of sts indicates authentication based on Resource Access Management (RAM) and Security Token Service (STS).
// Valid values: sts and access_key.
config.type = "access_key";

// If you use authentication based on RAM and STS, you must specify the SecurityToken parameter. You can call the AssumeRole operation of Alibaba Cloud RAM to obtain an STS token.
config.securityToken = "";

// Specify your AccessKey pair.
config.accessKeyId ="<accessKeyId>";
config.accessKeySecret ="<accessKeySecret>";

// Specify runtime parameters. The values of the parameters that specify time are in units of milliseconds.
let runtime = new $Util.RuntimeOptions({
  connectTimeout: 5000,
  readTimeout: 10000,
  autoretry: false,
  ignoreSSL: false,
  maxIdleConns: 50,
});

// Create an OpenSearch client instance.
let client = new Client(config);

// Specify the name or version information of the application to which data is to be pushed.
const appName = "<appName>";


// --------------- Query top searches or hints ---------------
let hot_algoQuery = {
    "sort_type": "default",
    "hit": 10,
    "user_id": "a7a0d37c824b659f36a5b9e3b819fcdd"
  };
try {
  let pathname = `/v3/openapi/apps/${appName}/actions/hot`;
  let  result=  client._request("GET", pathname, hot_algoQuery, null, null, runtime);
  result.then(function (result)  {
    console.log(result);
  });
}
catch (e) {
  console.log(e);
}
Note

For more information, see Query top searches and hints.