Drop-down suggestion demo

更新时间:
复制 MD 格式

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-util

devDependencies

typescript
ts-node

Step 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);
ParameterDescriptionDefault
config.endpointOpenSearch API endpoint. Get this from the OpenSearch console.
config.protocolRequest protocol. Valid values: HTTP, HTTPS.HTTP
config.typeAuthentication method. Valid values: access_key, sts.access_key
config.securityTokenSTS temporary token. Required only when config.type is sts. Call the AssumeRole operation of Alibaba Cloud RAM to get an STS token.""
connectTimeoutConnection timeout in milliseconds.5000
readTimeoutRead timeout in milliseconds.10000
autoretryWhether to retry on failure.false
ignoreSSLWhether to skip SSL verification.false
maxIdleConnsMaximum 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);
}
ParameterDescriptionExample
queryThe search term entered by the user."q"
hitMaximum number of suggestions to return.10
user_idUser identifier for personalized suggestions."a7a0d37c824b659f36a5b9e3b819fcdd"

What's next

For the full list of query parameters and response fields, see Query drop-down suggestions.