Basic search

更新时间:
复制 MD 格式

The OpenSearch TypeScript SDK lets you query indexed documents with a single HTTP call. This guide walks you through installing the SDK, configuring the client, and sending your first search request.

Prerequisites

Before you begin, ensure that you have:

  • Node.js 14 or later installed

  • An OpenSearch application created in the OpenSearch console

  • An AccessKey ID and AccessKey Secret — get yours from the Security Management page

  • Data indexed in your OpenSearch application

Install dependencies

Add the following packages to your package.json:

{
  "dependencies": {
    "@alicloud/credentials": "*",
    "@alicloud/opensearch-util": "*",
    "@alicloud/tea-typescript": "*",
    "@alicloud/tea-util": "*"
  },
  "devDependencies": {
    "typescript": "*",
    "ts-node": "*"
  }
}

Install them from npmjs.com:

npm install

Run a search

1. Configure the client

Create a Config instance and set the connection parameters:

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

const config = new Config();

// Endpoint for your application — find this in the OpenSearch console
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";

// Protocol: HTTP (default) or HTTPS
config.protocol = "HTTP";

// Authentication method: access_key (default) or sts
config.type = "access_key";

// Required only when using RAM and STS authentication.
// Call the RAM AssumeRole operation to get a security token.
config.securityToken = "";
Note

To find your endpoint, open the OpenSearch console, select your application, and copy the endpoint from the application details page.

2. Set runtime options

Configure connection and retry behavior. All timeout values are in milliseconds.

const runtime = new $Util.RuntimeOptions({
  connectTimeout: 5000,   // Connection timeout
  readTimeout: 10000,     // Read timeout
  autoretry: false,       // Auto-retry on failure
  ignoreSSL: false,       // SSL verification
  maxIdleConns: 50,       // Maximum idle connections
});

3. Build the search request

Specify your application name and construct the query parameters:

const client = new Client(config);

// Your OpenSearch application name
const appName = "<your-app-name>";

const searchParams = {
  // query string: sets pagination, format, and search conditions
  // start:0 — starting offset; hit:10 — number of results; format:fulljson — response format
  "query": "config=start:0,hit:10,format:fulljson&&query=(default:'Search' AND default:'OpenSearch') OR (default:'search engine' AND default:'policy')",
  "first_rank_name": "<your-first-rank-name>",    // Coarse ranking configuration name
  "second_rank_name": "<your-second-rank-name>",  // Fine ranking configuration name
  "fetch_fields": "<field1,field2>",              // Fields to return in results
};

Replace the placeholders with your actual values:

PlaceholderDescription
<your-app-name>Your OpenSearch application name
<your-first-rank-name>Name of your first-rank (coarse ranking) configuration
<your-second-rank-name>Name of your second-rank (fine ranking) configuration
<field1,field2>Comma-separated list of fields to return
Note

For query syntax details — including pagination parameters, format options, and search condition operators — see Search processing.

4. Send the request and handle the response

try {
  const pathname = `/v3/openapi/apps/${appName}/search`;
  const result = client._request("GET", pathname, searchParams, null, null, runtime);
  result.then(function (response) {
    console.log(response);
  });
} catch (e) {
  console.log(e);
}

What's next