This page provides TypeScript sample code for querying drop-down suggestions using the OpenSearch Industry Algorithm Edition SDK. Download packages from npmjs.com.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch application with a configured drop-down suggestion algorithm
Your OpenSearch endpoint (available in the OpenSearch console)
Your AccessKey ID and AccessKey secret, or a Security Token Service (STS) token if you use Resource Access Management (RAM)-based authentication
Step 1: Install dependencies
Add the following packages to your project:
dependencies
@alicloud/credentials
@alicloud/opensearch-util
@alicloud/tea-typescript
@alicloud/tea-utildevDependencies
typescript
ts-nodeStep 2: Configure the client
Create a Config instance and set your connection parameters.
All timeout values are in milliseconds.
import * as $Util from '@alicloud/tea-util';
import Client from "./Client";
import Config from "./Config";
import construct = Reflect.construct;
const config = new Config();
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";
config.protocol = "HTTP";
config.type = "access_key";
config.securityToken = "";
const runtime = new $Util.RuntimeOptions({
connectTimeout: 5000,
readTimeout: 10000,
autoretry: false,
ignoreSSL: false,
maxIdleConns: 50,
});
const client = new Client(config);| Parameter | Description | Default |
|---|---|---|
config.endpoint | OpenSearch API endpoint. Get this from the OpenSearch console. | — |
config.protocol | Request protocol. Valid values: HTTP, HTTPS. | HTTP |
config.type | Authentication method. Valid values: access_key, sts. | access_key |
config.securityToken | STS temporary token. Required only when config.type is sts. Call the AssumeRole operation of Alibaba Cloud RAM to get an STS token. | "" |
connectTimeout | Connection timeout in milliseconds. | 5000 |
readTimeout | Read timeout in milliseconds. | 10000 |
autoretry | Whether to retry on failure. | false |
ignoreSSL | Whether to skip SSL verification. | false |
maxIdleConns | Maximum number of idle connections. | 50 |
Step 3: Query drop-down suggestions
Call the suggest search endpoint with your application name and algorithm name.
const appName = "YOUR_APP_NAME";
const algoName = "YOUR_ALGO_NAME";
const suggest_algoQuery = {
"query": "q",
"hit": 10,
"user_id": "a7a0d37c824b659f36a5b9e3b819fcdd"
};
try {
const pathname = `/v3/openapi/apps/${appName}/suggest/${algoName}/search`;
const result = client._request("GET", pathname, suggest_algoQuery, null, null, runtime);
result.then(function (result) {
console.log(result);
});
} catch (e) {
console.log(e);
}| Parameter | Description | Example |
|---|---|---|
query | The search term entered by the user. | "q" |
hit | Maximum number of suggestions to return. | 10 |
user_id | User identifier for personalized suggestions. | "a7a0d37c824b659f36a5b9e3b819fcdd" |
What's next
For the full list of query parameters and response fields, see Query drop-down suggestions.