Demo code for querying hints

更新时间:
复制 MD 格式

Hints return popular search queries as suggestions while a user types, helping users find results faster and avoid zero-result searches. The following TypeScript sample calls the OpenSearch hints API to retrieve query suggestions for a given application.

Prerequisites

Before you begin, ensure that you have:

  • An OpenSearch Industry Algorithm Edition application

  • An Alibaba Cloud access key or a Resource Access Management (RAM) role with Security Token Service (STS) credentials

  • Node.js installed with npm

Add dependencies

Install the following packages from npm:

dependencies

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

devDependencies

typescript
ts-node

Query hints

The sample uses a GET request to /v3/openapi/apps/{appName}/actions/hint with three query parameters:

ParameterDescriptionExample value
sort_typeSorting strategy for returned hints"default"
hitMaximum number of hints to return10
user_idIdentifier for the current user, used for personalized ranking"a7a0d37c824b659f36a5b9e3b819fcdd"
import * as $Util from '@alicloud/tea-util';
import Client from "./Client";
import Config from "./Config";

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

// Endpoint for the OpenSearch API. Get this value from the OpenSearch console.
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";

// Request protocol. Default: HTTP. Valid values: HTTP, HTTPS.
config.protocol = "HTTP";

// Authentication method. Default: access_key.
// Set to "sts" to use RAM roles with temporary STS credentials.
config.type = "access_key";

// STS token — required only when config.type is "sts".
// Call the RAM AssumeRole operation to get a token.
config.securityToken = "";

// Runtime options. Timeout values are in milliseconds.
let runtime = new $Util.RuntimeOptions({
  connectTimeout: 5000,
  readTimeout: 10000,
  autoretry: false,
  ignoreSSL: false,
  maxIdleConns: 50,
});

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

// Replace <appName> with the name of your OpenSearch application.
const appName = "<appName>";

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

What's next

For the full API reference and additional query parameters, see Query top searches and hints.