All Products
Search
Document Center

OpenSearch:Demo code for querying drop-down suggestions

Last Updated:Jul 11, 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";
import construct = Reflect.construct;

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

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

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

// The authentication method. Default value: access_key. A value of sts specifies 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 = "";

// The 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);

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


// --------------- Query drop-down suggestions ---------------

const suggest_algoQuery = {
    "query": "q",
    "hit": 10,
    "user_id": "a7a0d37c824b659f36a5b9e3b819fcdd"
  };

// The name of the drop-down suggestion.
const algoName= "algoName";

try {
  let pathname = `/v3/openapi/apps/${appName}/suggest/${algoName}/search`;
  let  result=  client._request("GET", pathname, suggest_algoQuery, null, null, runtime);
  result.then(function (result)  {
    console.log(result);
  });
}
catch (e) {
  console.log(e);
}
Note

For more information, see Query drop-down suggestions.