@opensearch-project/opensearch 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,8 +1,81 @@
1
1
  # CHANGELOG
2
+
2
3
  Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
3
4
 
4
5
  ## [Unreleased]
6
+
5
7
  ### Added
8
+ ### Dependencies
9
+ ### Changed
10
+ ### Deprecated
11
+ ### Removed
12
+ ### Fixed
13
+ ### Security
14
+
15
+ ## [2.2.1]
16
+
17
+ ### Added
18
+
19
+ - Add release details to releasing.md ([319](https://github.com/opensearch-project/opensearch-js/pull/319))
20
+ - Added Amazon OpenSearch Serverless in user_guide ([#372](https://github.com/opensearch-project/opensearch-js/issues/372))
21
+ - Add guidelines on installing yarn, dependencies; instructions on running ESLint in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435))
22
+ - Added pull_request_template ([440](https://github.com/opensearch-project/opensearch-js/pull/440))
23
+ - Added guide for Search ([#473](https://github.com/opensearch-project/opensearch-js/pull/489))
24
+
25
+ ### Dependencies
26
+
27
+ - Bumps `aws4` from 1.11.0 to 1.12.0
28
+ - Bumps `minimist` from 1.2.6 to 1.2.8
29
+ - Bumps `tsd` from 0.24.1 to 0.27.0
30
+ - Bumps `dezalgo` from 1.0.3 to 1.0.4
31
+ - Bumps `simple-statistics` from 7.7.0 to 7.8.3
32
+ - Bumps `eslint` from 8.30.0 to 8.38.0
33
+ - Downgrades `@types/node` from 18.11.19 to 17.0.45
34
+ - Bumps `ora` from 5.4.1 to 6.1.2
35
+ - Bumps `@aws-sdk/types` from 3.190.0 to 3.257.0
36
+ - Bumps `secure-json-parse` from 2.4.0 to 2.7.0
37
+ - Bumps `simple-git` from 3.15.1 to 3.17.0
38
+ - Bumps `deepmerge` from 4.2.2 to 4.3.1
39
+ - Bumps `tap` from 16.3.0 to 16.3.4
40
+ - Bumps `rimraf` from 4.1.1 to 5.0.0
41
+ - Bumps `@types/node` from 17.0.45 to 18.15.11
42
+ - Bumps `caniuse-lite` from 1.0.30001249 to 1.0.30001469
43
+ - Bumps `eslint-config-prettier` from 8.6.0 to 8.8.0
44
+ - Bumps `split2` from 4.1.0 to 4.2.0
45
+ - Bumps `prettier` from 2.8.4 to 2.8.7
46
+ - Bumps `jsdoc` from 4.0.0 to 4.0.2
47
+ - Bumps `@babel/eslint-parser` from 7.19.1 to 7.21.3
48
+
49
+ ### Changed
50
+
51
+ - Remove test artifacts from gh_pages workflow ([#335](https://github.com/opensearch-project/opensearch-js/issues/335))
52
+ - Make fields in `BulkOperation` optional to match OpenSearch Bulk API requirements ([#378](https://github.com/opensearch-project/opensearch-js/pull/378))
53
+ - Remove guidance on using npm and switch completely to yarn in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435))
54
+ - Change coverage, compatability, integration, integration with unreleased Open Search, node ci, bundler tests not to run on documentation change ([441](https://github.com/opensearch-project/opensearch-js/pull/441))
55
+ - Change the Windows yarn installation troubleshoot steps ([455](https://github.com/opensearch-project/opensearch-js/issues/455))
56
+ - Make `callback` arg in `BaseConnectionPool`, `CloudConnectionPool` and `ConnectionPool` optional ([#451](https://github.com/opensearch-project/opensearch-js/pull/451))
57
+
58
+ ### Deprecated
59
+
60
+ - Remove deprecation warnings in bulk.test.js ([#434](https://github.com/opensearch-project/opensearch-js/issues/434))
61
+
62
+ ### Removed
63
+
64
+ - Remove waitCluster in Integration Tests ([#422](https://github.com/opensearch-project/opensearch-js/issues/422))
65
+
66
+ ### Fixed
67
+
68
+ - Added missing types for AwsSigv4SignerOptions.service ([#377](https://github.com/opensearch-project/opensearch-js/pull/377))
69
+ - Fixed deprecated folder mapping "./" in the "exports" field module resolution ([#416](https://github.com/opensearch-project/opensearch-js/pull/416))
70
+
71
+ ### Security
72
+
73
+ - [CVE-2022-25912] Bumps simple-git from 3.4.0 to 3.15.0 ([#341](https://github.com/opensearch-project/opensearch-js/pull/341))
74
+
75
+ ## [2.2.0]
76
+
77
+ ### Added
78
+
6
79
  - Add release details to releasing.md ([319](https://github.com/opensearch-project/opensearch-js/pull/319))
7
80
  - Allow overriding the aws service identifier in AwsSigv4Signer ([333](https://github.com/opensearch-project/opensearch-js/pull/333))
8
81
  - Added skip-changelog label ([339](https://github.com/opensearch-project/opensearch-js/pull/339))
@@ -11,44 +84,60 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
11
84
  - Documented all API methods ([#335](https://github.com/opensearch-project/opensearch-js/issues/335))
12
85
  - Added point in time APIs ([#348](https://github.com/opensearch-project/opensearch-js/pull/348))
13
86
  - Added support for Amazon OpenSearch Serverless ([#356](https://github.com/opensearch-project/opensearch-js/issues/356))
87
+ - Added required `array` argument when invoking `SerializationError` ([#419](https://github.com/opensearch-project/opensearch-js/pull/419))
88
+ - Removed unnecessary `data` argument when invoking `OpenSearchClientError` ([#421](https://github.com/opensearch-project/opensearch-js/pull/421))
89
+ - Fixed typos in `ConnectionPool` ([#427](https://github.com/opensearch-project/opensearch-js/pull/427))
90
+ - Added the solution for the possible error during yarn installation on Windows OS ([#435](https://github.com/opensearch-project/opensearch-js/issues/435))
91
+ - Added missing `createConnection` method type definition in `BaseConnectionPool` )([#490](https://github.com/opensearch-project/opensearch-js/pull/490))
14
92
 
15
93
  ### Dependencies
94
+
16
95
  - Bumps `xmlbuilder2` from 2.4.1 to 3.0.2
17
96
  - Bumps `minimatch` from 3.0.4 to 3.1.2
18
- - Bumps `eslint` from 8.30.0 to 8.32.0
19
97
  - Bumps `eslint` from 7.32.0 to 8.32.0
20
98
  - Replaced `babel-eslint` with `@babel/eslint-parser`
21
99
  - Bumps `eslint-plugin-prettier` from 4.0.0 to 4.2.1
22
- - Bumps `minimist` from 1.2.6 to 1.2.7
23
- - Bumps `@aws-sdk/types` from 3.190.0 to 3.226.0
24
100
  - Bumps `json5` from 2.2.0 to 2.2.3
25
101
  - Bumps `split2` from 3.2.2 to 4.1.0
26
- - Bumps `@types/node` from 15.14.7 to 18.11.18
27
- - Bumps `prettier` from 2.7.1 to 2.8.3
102
+ - Bumps `@types/node` from 15.14.7 to 18.11.19
103
+ - Bumps `prettier` from 2.7.1 to 2.8.4
28
104
  - Bumps `hpagent` from 0.1.2 to 1.2.0
29
105
  - Bumps `eslint-config-prettier` from 8.5.0 to 8.6.0
30
106
  - Bumps `rimraf` from 3.0.2 to 4.1.1
31
107
 
32
108
  ### Dependencies
109
+
33
110
  ### Changed
111
+
34
112
  - Remove test artifacts from gh_pages workflow ([#335](https://github.com/opensearch-project/opensearch-js/issues/335))
113
+ - Make `TimeoutError` and `RequestAbortedError` classes' `meta` argument optional ([#478](https://github.com/opensearch-project/opensearch-js/pull/478/))
114
+
35
115
  ### Deprecated
116
+
36
117
  ### Removed
118
+
37
119
  ### Fixed
120
+
38
121
  ### Security
122
+
39
123
  - [CVE-2022-25912] Bumps simple-git from 3.4.0 to 3.15.0 ([#341](https://github.com/opensearch-project/opensearch-js/pull/341))
40
124
 
41
- ## [2.1]
125
+ ## [2.1.0]
126
+
42
127
  ### Added
128
+
43
129
  - Github workflow for changelog verification ([#306](https://github.com/opensearch-project/opensearch-js/pull/306))
44
130
  - Add GitHub and Jenkins release workflow ([#317](https://github.com/opensearch-project/opensearch-js/pull/317))
45
131
 
46
132
  ### Dependencies
133
+
47
134
  - Bumps `tsd` from 0.22.0 to 0.24.1
48
135
  - Bumps `semver` from 7.3.7 to 7.3.8
49
136
 
50
137
  ### Fixed
138
+
51
139
  - Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291))
52
140
 
53
- [2.1]: https://github.com/opensearch-project/opensearch-js/releases/tag/2.1.0
54
- [Unreleased]: https://github.com/opensearch-project/opensearch-js/compare/2.1...HEAD
141
+ [2.1.0]: https://github.com/opensearch-project/opensearch-js/releases/tag/2.1.0
142
+ [2.2.0]: https://github.com/opensearch-project/opensearch-js/releases/tag/2.2.0
143
+ [Unreleased]: https://github.com/opensearch-project/opensearch-js/compare/2.2.0...HEAD
@@ -3,7 +3,10 @@
3
3
  - [Git Clone OpenSearch Node.js Client Repository](#git-clone-opensearch-nodejs-client-repository)
4
4
  - [Install Prerequisites](#install-prerequisites)
5
5
  - [Node.js](#nodejs)
6
+ - [Yarn](#yarn)
6
7
  - [Docker](#docker)
8
+ - [Installing Dependencies](#installing-dependencies)
9
+ - [Using ESLint](#using-eslint)
7
10
  - [Unit Testing](#unit-testing)
8
11
  - [Integration Testing](#integration-testing)
9
12
  - [Execute integration tests from your terminal](#execute-integration-tests-from-your-terminal)
@@ -25,30 +28,77 @@ e.g. `git clone https://github.com/[your username]/opensearch-js.git`.
25
28
 
26
29
  The minimum supported version of Node.js is v10.
27
30
 
31
+ #### Yarn
32
+
33
+ This project uses yarn as it's package manager. You can use npm to install yarn:
34
+
35
+ ```
36
+ npm install --global yarn
37
+ ```
38
+
28
39
  #### Docker
29
40
 
30
41
  [Docker](https://docs.docker.com/install/) is required for building some OpenSearch artifacts and executing integration tests.
31
42
 
43
+ ### Installing Dependencies
44
+
45
+ To install all the dependencies needed for this project, run
46
+
47
+ ```
48
+ yarn install
49
+ ```
50
+ There might be an issue on Windows OS:
51
+ ```
52
+ yarn. ps1 cannot be loaded because running scripts is disabled on this system
53
+ ```
54
+ If this occurs, open Windows PowerShell and run:
55
+ ```
56
+ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
57
+ ```
58
+ Agree to the changes.
59
+ Now you can run `yarn install`
60
+ If the problem persists, you can try to open Windows PowerShell as an administrator and run:
61
+ ```
62
+ Set-ExecutionPolicy -ExecutionPolicy Unrestricted
63
+ ```
64
+
65
+ ### Using ESLint
66
+
67
+ To do a ESLint check on the project, run:
68
+
69
+ ```
70
+ yarn run lint
71
+ ```
72
+
73
+ To let ESLint to fix errors in your code automatically, run:
74
+
75
+ ```
76
+ yarn run lint:fix
77
+ ```
78
+
79
+ This will instruct ESLint to try to fix as many issues as possible. The fixes are made to the actual files themselves and only the remaining unfixed issues are output.
80
+
81
+ Not all problems are fixable using this option, and some of them you will need to fix manually.
82
+
32
83
  ### Unit Testing
33
84
 
34
85
  Go to your terminal and run:
35
86
 
36
87
  ```
37
88
  cd folder-path
38
- npm run test:unit
89
+ yarn run test:unit
39
90
  ```
40
91
 
41
- To run a specific unit test, you can use the following jest command. `npm test` will not work.
92
+ To run a specific unit test, you can use the following jest command. `yarn test` will not work.
42
93
 
43
94
  ```
44
95
  cd folder-path/to/test
45
96
  jest TestName
46
97
  ```
47
98
 
48
- If you don't have jest, you can install it via npm or yarn
99
+ If you don't have jest, you can install it via yarn
49
100
 
50
101
  ```
51
- npm i -g jest-cli
52
102
  yarn global add jest-cli
53
103
  ```
54
104
 
@@ -69,7 +119,7 @@ If you have not installed docker-compose, you can install it from this [link](ht
69
119
 
70
120
  2. Run all integration tests.
71
121
  ```
72
- npm run test:integration:helpers
122
+ yarn run test:integration:helpers
73
123
  ```
74
124
  3. Stop and clean containers.
75
125
  ```
package/MAINTAINERS.md CHANGED
@@ -5,12 +5,18 @@ This document contains a list of maintainers in this repo. See [opensearch-proje
5
5
  ## Current Maintainers
6
6
 
7
7
  | Maintainer | GitHub ID | Affiliation |
8
- | --------------------- | --------------------------------------------- | ----------- |
8
+ |-----------------------|-----------------------------------------------|-------------|
9
9
  | Anan Zhuang | [ananzh](https://github.com/ananzh) | Amazon |
10
- | Bishoy Boktor | [boktorbb](https://github.com/boktorbb) | Amazon |
11
10
  | Kawika (Rocky) Avilla | [kavilla](https://github.com/kavilla) | Amazon |
12
- | Mihir Soni | [mihirsoni](https://github.com/mihirsoni) | Amazon |
13
11
  | Sean Neumann | [seanneumann](https://github.com/seanneumann) | Amazon |
14
12
  | Daniel Doubrovkine | [dblock](https://github.com/dblock) | Amazon |
15
13
  | Vacha Shah | [VachaShah](https://github.com/VachaShah) | Amazon |
16
14
  | Theo Truong | [nhtruong](https://github.com/nhtruong) | Amazon |
15
+ | Harsha Vamsi Kalluri | [harshavamsi](https://github.com/harshavamsi) | Amazon |
16
+
17
+ ## Emeritus
18
+
19
+ | Maintainer | GitHub ID | Affiliation |
20
+ |---------------|-------------------------------------------|-------------|
21
+ | Bishoy Boktor | [boktorbb](https://github.com/boktorbb) | Amazon |
22
+ | Mihir Soni | [mihirsoni](https://github.com/mihirsoni) | Amazon |
package/RELEASING.md CHANGED
@@ -34,7 +34,8 @@ Repositories create consistent release labels, such as `v1.0.0`, `v1.1.0` and `v
34
34
  The release process is standard across repositories in this org and is run by a release manager volunteering from amongst [maintainers](MAINTAINERS.md).
35
35
 
36
36
  1. Create a tag, e.g. v2.1.0, and push it to the GitHub repo.
37
- 1. The [release-drafter.yml](.github/workflows/release-drafter.yml) will be automatically kicked off and a draft release will be created.
37
+ 1. The [release-drafter.yml](.github/workflows/release-drafter.yml) will be automatically kicked off and is responsible for drafting a new release on GitHub.
38
+ 1. Before creating a draft release, this workflow creates a GitHub issue asking for approval from the [maintainers](MAINTAINERS.md). See sample [issue](https://github.com/gaiksaya/opensearch-js/issues/1). The maintainers need to approve in order to continue the workflow run.
38
39
  1. This draft release triggers the [jenkins release workflow](https://build.ci.opensearch.org/job/opensearch-js-release/) as a result of which opensearch-js client is released on [npmjs](https://www.npmjs.com/package/@opensearch-project/opensearch).
39
40
  1. Once the above release workflow is successful, the drafted release on GitHub is published automatically.
40
- 1. Increment "version" in package.json to the next patch release, e.g. v2.1.1. See [example](https://github.com/opensearch-project/opensearch-js/pull/318)
41
+ 1. Increment "version" in package.json to the next patch release, e.g. v2.1.1. See [example](https://github.com/opensearch-project/opensearch-js/pull/318)
package/USER_GUIDE.md CHANGED
@@ -10,6 +10,7 @@
10
10
  - [Search for the Document](#search-for-the-document)
11
11
  - [Delete the document](#delete-the-document)
12
12
  - [Delete the index](#delete-the-index)
13
+ - [Empty all Pool Connections](#empty-all-pool-connections)
13
14
 
14
15
  ## Initializing a Client
15
16
 
@@ -51,7 +52,8 @@ const { AwsSigv4Signer } = require('@opensearch-project/opensearch/aws');
51
52
 
52
53
  const client = new Client({
53
54
  ...AwsSigv4Signer({
54
- region: 'us-east-1',
55
+ region: 'us-west-2',
56
+ service: 'es', // 'aoss' for OpenSearch Serverless
55
57
  // Must return a Promise that resolve to an AWS.Credentials object.
56
58
  // This function is used to acquire the credentials when the client start and
57
59
  // when the credentials are expired.
@@ -72,6 +74,7 @@ const client = new Client({
72
74
  }),
73
75
  }),
74
76
  node: 'https://search-xxx.region.es.amazonaws.com', // OpenSearch domain URL
77
+ // node: "https://xxx.region.aoss.amazonaws.com" for OpenSearch Serverless
75
78
  });
76
79
  ```
77
80
 
@@ -85,6 +88,7 @@ const { AwsSigv4Signer } = require('@opensearch-project/opensearch/aws');
85
88
  const client = new Client({
86
89
  ...AwsSigv4Signer({
87
90
  region: 'us-east-1',
91
+ service: 'es', // 'aoss' for OpenSearch Serverless
88
92
  // Must return a Promise that resolve to an AWS.Credentials object.
89
93
  // This function is used to acquire the credentials when the client start and
90
94
  // when the credentials are expired.
@@ -99,6 +103,7 @@ const client = new Client({
99
103
  },
100
104
  }),
101
105
  node: 'https://search-xxx.region.es.amazonaws.com', // OpenSearch domain URL
106
+ // node: "https://xxx.region.aoss.amazonaws.com" for OpenSearch Serverless
102
107
  });
103
108
  ```
104
109
 
@@ -245,4 +250,18 @@ console.log('Deleting all PITs:');
245
250
  var response = await client.deleteAllPits();
246
251
 
247
252
  console.log(response.body);
248
- ```
253
+ ```
254
+
255
+ ## Empty all Pool Connections
256
+
257
+ ```javascript
258
+ var pool = new ConnectionPool({ Connection });
259
+ pool.addConnection('http://localhost:9200/');
260
+ pool.addConnection('http://localhost:9201/');
261
+
262
+ pool.empty();
263
+ // OR
264
+ pool.empty(() => {
265
+ // Do something after emptying the pool
266
+ });
267
+ ```
package/api/types.d.ts CHANGED
@@ -40,12 +40,12 @@ export interface BulkIndexOperation extends BulkOperation { }
40
40
  export interface BulkIndexResponseItem extends BulkResponseItemBase { }
41
41
 
42
42
  export interface BulkOperation {
43
- _id: Id;
44
- _index: IndexName;
45
- retry_on_conflict: integer;
46
- routing: Routing;
47
- version: VersionNumber;
48
- version_type: VersionType;
43
+ _id?: Id;
44
+ _index?: IndexName;
45
+ retry_on_conflict?: integer;
46
+ routing?: Routing;
47
+ version?: VersionNumber;
48
+ version_type?: VersionType;
49
49
  }
50
50
 
51
51
  export interface BulkOperationContainer {
@@ -0,0 +1,91 @@
1
+ # Advanced Index Actions
2
+ In this guide, we will look at some advanced index actions that are not covered in the [Index Lifecycle](index_lifecycle.md) guide.
3
+
4
+
5
+ ## Setup
6
+ Let's create a client instance, and an index named `movies`:
7
+ ```javascript
8
+ const { Client } = require('@opensearch-project/opensearch');
9
+ const client = new Client({
10
+ node: 'https://admin:admin@localhost:9200',
11
+ ssl: { rejectUnauthorized: false }
12
+ });
13
+ client.indices.create({index: 'movies'})
14
+ ```
15
+ ## API Actions
16
+ ### Clear index cache
17
+ You can clear the cache of an index or indices by using the `indices.clear_cache` API action. The following example clears the cache of the `movies` index:
18
+
19
+ ```javascript
20
+ client.indices.clear_cache({index: 'movies'})
21
+ ```
22
+
23
+ By default, the `indices.clear_cache` API action clears all types of cache. To clear specific types of cache pass the the `query`, `fielddata`, or `request` parameter to the API action:
24
+
25
+ ```javascript
26
+ client.indices.clear_cache({index: 'movies', query: true})
27
+ client.indices.clear_cache({index: 'movies', fielddata: true, request: true})
28
+ ```
29
+
30
+ ### Flush index
31
+ Sometimes you might want to flush an index or indices to make sure that all data in the transaction log is persisted to the index. To flush an index or indices use the `indices.flush` API action. The following example flushes the `movies` index:
32
+
33
+ ```javascript
34
+ client.indices.flush({index: 'movies'})
35
+ ```
36
+
37
+ ### Refresh index
38
+ You can refresh an index or indices to make sure that all changes are available for search. To refresh an index or indices use the `indices.refresh` API action:
39
+
40
+ ```javascript
41
+ client.indices.refresh({index: 'movies'})
42
+ ```
43
+
44
+ ### Open/Close index
45
+ You can close an index to prevent read and write operations on the index. A closed index does not have to maintain certain data structures that an opened index require, reducing the memory and disk space required by the index. The following example closes and reopens the `movies` index:
46
+
47
+ ```javascript
48
+ client.indices.close({index: 'movies'})
49
+ client.indices.open({index: 'movies'})
50
+ ```
51
+ ### Force merge index
52
+ You can force merge an index or indices to reduce the number of segments in the index. This can be useful if you have a large number of small segments in the index. Merging segments reduces the memory footprint of the index. Do note that this action is resource intensive and it is only recommended for read-only indices. The following example force merges the `movies` index:
53
+
54
+ ```javascript
55
+ client.indices.forcemerge({index: 'movies'})
56
+ ```
57
+
58
+ ### Clone index
59
+ You can clone an index to create a new index with the same mappings, data, and MOST of the settings. The source index must be in read-only state for cloning. The following example blocks write operations from `movies` index, clones the said index to create a new index named `movies_clone`, then re-enables write:
60
+
61
+ ```javascript
62
+ client.indices.add_block({index: 'movies', block: 'write'})
63
+ client.indices.clone({index: 'movies', target: 'movies_clone'})
64
+ client.indices.put_settings({index: 'movies', body: { index: { blocks: { write: false } } } })
65
+ ```
66
+
67
+ ### Split index
68
+ You can split an index into another index with more primary shards. The source index must be in read-only state for splitting. The following example create the read-only `books` index with 30 routing shards and 5 shards (which is divisible by 30), splits index into `bigger_books` with 10 shards (which is also divisible by 30), then re-enables write:
69
+
70
+ ```javascript
71
+ client.indices.create({
72
+ index: 'books',
73
+ body: { settings: {
74
+ index: { number_of_shards: 5,
75
+ number_of_routing_shards: 30,
76
+ blocks: { write: true } } } } })
77
+
78
+ client.indices.split({
79
+ index: 'books',
80
+ target: 'bigger_books',
81
+ body: { settings: { index: { number_of_shards: 10 } } } })
82
+
83
+ client.indices.put_settings({index: 'books', body: { index: { blocks: { write: false } } } })
84
+ ```
85
+
86
+ ## Cleanup
87
+
88
+ Let's delete all the indices we created in this guide:
89
+ ```javascript
90
+ client.indices.delete({index: ['movies', 'books', 'movies_clone', 'bigger_books']});
91
+ ```
package/guides/bulk.md ADDED
@@ -0,0 +1,166 @@
1
+ # Bulk
2
+
3
+ In this guide, you'll learn how to use the OpenSearch JavaScript Client API to perform bulk operations. You'll learn how to index, update, and delete multiple documents in a single request.
4
+
5
+ ## Setup
6
+ First, create a client instance with the following code:
7
+
8
+ ```javascript
9
+ const { Client } = require('@opensearch-project/opensearch');
10
+
11
+ const client = new Client({
12
+ node: 'http://localhost:9200',
13
+ });
14
+ ```
15
+
16
+ Next, create an index named `movies` and another named `books` with the default settings:
17
+
18
+ ```javascript
19
+ const movies = 'movies';
20
+ const books = 'books';
21
+
22
+ client.indices.create({ index: movies }).then(() => {
23
+ console.log(`Index ${movies} created`);
24
+ });
25
+
26
+ client.indices.create({ index: books }).then(() => {
27
+ console.log(`Index ${books} created`);
28
+ });
29
+ ```
30
+
31
+
32
+ ## Bulk API
33
+
34
+ The `bulk` API action allows you to perform document operations in a single request. The body of the request is an array of objects that contains the bulk operations and the target documents to index, create, update, or delete.
35
+
36
+ ### Indexing multiple documents
37
+ The following code creates two documents in the `movies` index and one document in the `books` index:
38
+
39
+ ```javascript
40
+ client.bulk({
41
+ body: [
42
+ { index: { _index: movies, _id: 1 } },
43
+ { title: 'Beauty and the Beast', year: 1991 },
44
+ { index: { _index: movies, _id: 2 } },
45
+ { title: 'Beauty and the Beast - Live Action', year: 2017 },
46
+ { index: { _index: books, _id: 1 } },
47
+ { title: 'The Lion King', year: 1994 }
48
+ ]
49
+ }).then((response) => {
50
+ console.log(response);
51
+ });
52
+ ```
53
+ As you can see, each bulk operation is comprised of two objects. The first object contains the operation type and the target document's `_index` and `_id`. The second object contains the document's data. As a result, the body of the request above contains six objects for three index actions.
54
+
55
+ Alternatively, the `bulk` method can accept an array of hashes where each hash represents a single operation. The following code is equivalent to the previous example:
56
+
57
+ ```javascript
58
+ client.bulk({
59
+ body: [
60
+ { index: { _index: movies, _id: 1, data: { title: 'Beauty and the Beast', year: 1991 } } },
61
+ { index: { _index: movies, _id: 2, data: { title: 'Beauty and the Beast - Live Action', year: 2017 } } },
62
+ { index: { _index: books, _id: 1, data: { title: 'The Lion King', year: 1994 } } }
63
+ ]
64
+ }).then((response) => {
65
+ console.log(response);
66
+ });
67
+ ```
68
+
69
+ We will use this format for the rest of the examples in this guide.
70
+
71
+ ### Creating multiple documents
72
+
73
+ Similarly, instead of calling the `create` method for each document, you can use the `bulk` API to create multiple documents in a single request. The following code creates three documents in the `movies` index and one in the `books` index:
74
+
75
+ ```javascript
76
+ client.bulk({
77
+ index: movies,
78
+ body: [
79
+ { create: { data: { title: 'Beauty and the Beast 2', year: 2030 } } },
80
+ { create: { data: { title: 'Beauty and the Beast 3', year: 2031 } } },
81
+ { create: { data: { title: 'Beauty and the Beast 4', year: 2049 } } },
82
+ { create: { _index: books, data: { title: 'The Lion King 2', year: 1998 } } }
83
+ ]
84
+ }).then((response) => {
85
+ console.log(response);
86
+ });
87
+ ```
88
+ Note that we specified only the `_index` for the last document in the request body. This is because the `bulk` method accepts an `index` parameter that specifies the default `_index` for all bulk operations in the request body. Moreover, we omit the `_id` for each document and let OpenSearch generate them for us in this example, just like we can with the `create` method.
89
+
90
+ ### Updating multiple documents
91
+ ```javascript
92
+ client.bulk({
93
+ index: movies,
94
+ body: [
95
+ { update: { _id: 1, data: { doc: { year: 1992 } } } },
96
+ { update: { _id: 2, data: { doc: { year: 2018 } } } }
97
+ ]
98
+ }).then((response) => {
99
+ console.log(response);
100
+ });
101
+ ```
102
+ Note that the updated data is specified in the `doc` field of the `data` object.
103
+
104
+
105
+ ### Deleting multiple documents
106
+ ```javascript
107
+ client.bulk({
108
+ index: movies,
109
+ body: [
110
+ { delete: { _id: 1 } },
111
+ { delete: { _id: 2 } }
112
+ ]
113
+ }).then((response) => {
114
+ console.log(response);
115
+ });
116
+ ```
117
+
118
+ ### Mix and match operations
119
+ You can mix and match the different operations in a single request. The following code creates two documents, updates one document, and deletes another document:
120
+
121
+ ```javascript
122
+ client.bulk({
123
+ index: movies,
124
+ body: [
125
+ { create: { data: { title: 'Beauty and the Beast 5', year: 2050 } } },
126
+ { create: { data: { title: 'Beauty and the Beast 6', year: 2051 } } },
127
+ { update: { _id: 3, data: { doc: { year: 2052 } } } },
128
+ { delete: { _id: 4 } }
129
+ ]
130
+ }).then((response) => {
131
+ console.log(response);
132
+ });
133
+ ```
134
+
135
+ ### Handling errors
136
+ The `bulk` API returns an array of responses for each operation in the request body. Each response contains a `status` field that indicates whether the operation was successful or not. If the operation was successful, the `status` field is set to a `2xx` code. Otherwise, the response contains an error message in the `error` field.
137
+
138
+ The following code shows how to look for errors in the response:
139
+
140
+ ```javascript
141
+ client.bulk({
142
+ index: movies,
143
+ body: [
144
+ { create: { _id: 1, data: { title: 'Beauty and the Beast', year: 1991 } } },
145
+ { create: { _id: 2, data: { title: 'Beauty and the Beast 2', year: 2030 } } },
146
+ { create: { _id: 1, data: { title: 'Beauty and the Beast 3', year: 2031 } } }, // document already exists error
147
+ { create: { _id: 2, data: { title: 'Beauty and the Beast 4', year: 2049 } } } // document already exists error
148
+ ]
149
+ }).then((response) => {
150
+ response.body.forEach((item) => {
151
+ const createStatus = item.create && item.create.status;
152
+ if (createStatus && !createStatus.toString().match(/2\d{2}/)) {
153
+ console.log(item.create.error.reason);
154
+ }
155
+ });
156
+ }).catch((error) => {
157
+ console.error(error);
158
+ });
159
+ ```
160
+
161
+ ## Cleanup
162
+ To clean up the resources created in this guide, delete the `movies` and `books` indices:
163
+
164
+ ```javascript
165
+ client.indices.delete({ index: [movies, books] });
166
+ ```