@opensearch-project/opensearch 3.0.0-beta.4 → 3.0.0-beta.5
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/lib/OpenSearchAPI.d.ts +1403 -0
- package/lib/OpenSearchAPI.js +105 -0
- package/package.json +5 -1
- package/.dockerignore +0 -5
- package/.eslintignore +0 -2
- package/.eslintrc.json +0 -34
- package/.prettierrc.json +0 -5
- package/.whitesource +0 -15
- package/ADMINS.md +0 -16
- package/COMPATIBILITY.md +0 -13
- package/DEVELOPER_GUIDE.md +0 -127
- package/MAINTAINERS.md +0 -23
- package/Makefile +0 -17
- package/RELEASING.md +0 -41
- package/SECURITY.md +0 -3
- package/USER_GUIDE.md +0 -316
- package/codecov.yml +0 -14
- package/guides/advanced_index_actions.md +0 -92
- package/guides/bulk.md +0 -126
- package/guides/document_lifecycle.md +0 -162
- package/guides/index_lifecycle.md +0 -142
- package/guides/index_template.md +0 -228
- package/guides/json.md +0 -53
- package/guides/msearch.md +0 -56
- package/guides/search.md +0 -203
- package/jenkins/release.JenkinsFile +0 -11
- package/samples/README.md +0 -17
- package/samples/bulk.js +0 -91
- package/samples/json.js +0 -75
- package/samples/msearch.js +0 -131
- package/samples/rollups.js +0 -139
- package/samples/search.js +0 -155
- package/samples/transforms.ts +0 -146
package/USER_GUIDE.md
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
# User Guide
|
|
2
|
-
|
|
3
|
-
- [User Guide](#user-guide)
|
|
4
|
-
- [Initializing a Client](#initializing-a-client)
|
|
5
|
-
- [Authenticate with Amazon OpenSearch Service](#authenticate-with-amazon-opensearch-service)
|
|
6
|
-
- [Using AWS V2 SDK](#using-aws-v2-sdk)
|
|
7
|
-
- [Using AWS V3 SDK](#using-aws-v3-sdk)
|
|
8
|
-
- [Enable Handling of Long Numerals](#enable-handling-of-long-numerals)
|
|
9
|
-
- [Create an Index](#create-an-index)
|
|
10
|
-
- [Add a Document to the Index](#add-a-document-to-the-index)
|
|
11
|
-
- [Search for the Document](#search-for-the-document)
|
|
12
|
-
- [Multi-Search (msearch)](#multi-search-msearch)
|
|
13
|
-
- [Delete the document](#delete-the-document)
|
|
14
|
-
- [Delete the index](#delete-the-index)
|
|
15
|
-
- [Create a Point in Time](#create-a-point-in-time)
|
|
16
|
-
- [Get all PITs](#get-all-pits)
|
|
17
|
-
- [Delete a Point in Time](#delete-a-point-in-time)
|
|
18
|
-
- [Delete all PITs](#delete-all-pits)
|
|
19
|
-
- [Empty all Pool Connections](#empty-all-pool-connections)
|
|
20
|
-
|
|
21
|
-
## Initializing a Client
|
|
22
|
-
|
|
23
|
-
```javascript
|
|
24
|
-
'use strict';
|
|
25
|
-
|
|
26
|
-
var host = 'localhost';
|
|
27
|
-
var protocol = 'https';
|
|
28
|
-
var port = 9200;
|
|
29
|
-
var auth = 'admin:strongPassword@999'; // For testing only. Don't store credentials in code.
|
|
30
|
-
var ca_certs_path = '/full/path/to/root-ca.pem';
|
|
31
|
-
|
|
32
|
-
// Optional client certificates if you don't want to use HTTP basic authentication.
|
|
33
|
-
// var client_cert_path = '/full/path/to/client.pem'
|
|
34
|
-
// var client_key_path = '/full/path/to/client-key.pem'
|
|
35
|
-
|
|
36
|
-
// Create a client with SSL/TLS enabled.
|
|
37
|
-
var { Client } = require('@opensearch-project/opensearch');
|
|
38
|
-
var fs = require('fs');
|
|
39
|
-
var client = new Client({
|
|
40
|
-
node: protocol + '://' + auth + '@' + host + ':' + port,
|
|
41
|
-
ssl: {
|
|
42
|
-
ca: fs.readFileSync(ca_certs_path),
|
|
43
|
-
// You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
|
|
44
|
-
// cert: fs.readFileSync(client_cert_path),
|
|
45
|
-
// key: fs.readFileSync(client_key_path)
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Authenticate with Amazon OpenSearch Service
|
|
51
|
-
|
|
52
|
-
#### Using AWS V2 SDK
|
|
53
|
-
|
|
54
|
-
```javascript
|
|
55
|
-
const AWS = require('aws-sdk'); // V2 SDK.
|
|
56
|
-
const { Client } = require('@opensearch-project/opensearch');
|
|
57
|
-
const { AwsSigv4Signer } = require('@opensearch-project/opensearch/aws');
|
|
58
|
-
|
|
59
|
-
const client = new Client({
|
|
60
|
-
...AwsSigv4Signer({
|
|
61
|
-
region: 'us-west-2',
|
|
62
|
-
service: 'es', // 'aoss' for OpenSearch Serverless
|
|
63
|
-
// Must return a Promise that resolve to an AWS.Credentials object.
|
|
64
|
-
// This function is used to acquire the credentials when the client start and
|
|
65
|
-
// when the credentials are expired.
|
|
66
|
-
// The Client will refresh the Credentials only when they are expired.
|
|
67
|
-
// With AWS SDK V2, Credentials.refreshPromise is used when available to refresh the credentials.
|
|
68
|
-
|
|
69
|
-
// Example with AWS SDK V2:
|
|
70
|
-
getCredentials: () =>
|
|
71
|
-
new Promise((resolve, reject) => {
|
|
72
|
-
// Any other method to acquire a new Credentials object can be used.
|
|
73
|
-
// If you use profiles, make sure to pass role assumer, or use default provider fromNodeProviderChain
|
|
74
|
-
AWS.config.getCredentials((err, credentials) => {
|
|
75
|
-
if (err) {
|
|
76
|
-
reject(err);
|
|
77
|
-
} else {
|
|
78
|
-
resolve(credentials);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}),
|
|
82
|
-
}),
|
|
83
|
-
node: 'https://search-xxx.region.es.amazonaws.com', // OpenSearch domain URL
|
|
84
|
-
// node: "https://xxx.region.aoss.amazonaws.com" for OpenSearch Serverless
|
|
85
|
-
});
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
#### Using AWS V3 SDK
|
|
89
|
-
|
|
90
|
-
```javascript
|
|
91
|
-
const { defaultProvider } = require('@aws-sdk/credential-provider-node'); // V3 SDK.
|
|
92
|
-
const { Client } = require('@opensearch-project/opensearch');
|
|
93
|
-
const { AwsSigv4Signer } = require('@opensearch-project/opensearch/aws-v3'); // use aws-v3 import path if you are using aws-sdk v3
|
|
94
|
-
// Unlike the import path in the v2 example above that lazy loads both aws-sdk v3 credential providers & entire aws-sdk v2 if available
|
|
95
|
-
// This will only lazy load the aws-sdk v3 credential providers
|
|
96
|
-
|
|
97
|
-
const client = new Client({
|
|
98
|
-
...AwsSigv4Signer({
|
|
99
|
-
region: 'us-east-1',
|
|
100
|
-
service: 'es', // 'aoss' for OpenSearch Serverless
|
|
101
|
-
// Must return a Promise that resolve to an AWS.Credentials object.
|
|
102
|
-
// This function is used to acquire the credentials when the client start and
|
|
103
|
-
// when the credentials are expired.
|
|
104
|
-
// The Client will treat the Credentials as expired if within
|
|
105
|
-
// `requestTimeout` ms of expiration (default is 30000 ms).
|
|
106
|
-
|
|
107
|
-
// Example with AWS SDK V3:
|
|
108
|
-
getCredentials: () => {
|
|
109
|
-
// Any other method to acquire a new Credentials object can be used.
|
|
110
|
-
const credentialsProvider = defaultProvider();
|
|
111
|
-
return credentialsProvider();
|
|
112
|
-
},
|
|
113
|
-
}),
|
|
114
|
-
requestTimeout: 60000, // Also used for refreshing credentials in advance
|
|
115
|
-
node: 'https://search-xxx.region.es.amazonaws.com', // OpenSearch domain URL
|
|
116
|
-
// node: "https://xxx.region.aoss.amazonaws.com" for OpenSearch Serverless
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### Enable Handling of Long Numerals
|
|
121
|
-
|
|
122
|
-
JavaScript can safely work with integers from -(2<sup>53</sup> - 1) to 2<sup>53</sup> - 1. However,
|
|
123
|
-
serialized JSON texts from other languages can potentially have numeric values beyond that range and the native
|
|
124
|
-
serialization and deserialization methods of JavaScript's JSON, incapable of parsing them with precision; these
|
|
125
|
-
values get rounded to fit the IEEE-754 representation.
|
|
126
|
-
|
|
127
|
-
The `Client` can be configured to appropriately deserialize long numerals as `BigInt` values and vice versa:
|
|
128
|
-
|
|
129
|
-
```javascript
|
|
130
|
-
const client = new Client({
|
|
131
|
-
enableLongNumeralSupport: true,
|
|
132
|
-
});
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
## Create an Index
|
|
136
|
-
|
|
137
|
-
```javascript
|
|
138
|
-
console.log('Creating index:');
|
|
139
|
-
|
|
140
|
-
var index_name = 'books';
|
|
141
|
-
var settings = {
|
|
142
|
-
settings: {
|
|
143
|
-
index: {
|
|
144
|
-
number_of_shards: 4,
|
|
145
|
-
number_of_replicas: 3,
|
|
146
|
-
},
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
var response = await client.indices.create({
|
|
151
|
-
index: index_name,
|
|
152
|
-
body: settings,
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
console.log(response.body);
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
## Add a Document to the Index
|
|
159
|
-
|
|
160
|
-
```javascript
|
|
161
|
-
console.log('Adding document:');
|
|
162
|
-
|
|
163
|
-
var document = {
|
|
164
|
-
title: 'The Outsider',
|
|
165
|
-
author: 'Stephen King',
|
|
166
|
-
year: '2018',
|
|
167
|
-
genre: 'Crime fiction',
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
var id = '1';
|
|
171
|
-
|
|
172
|
-
var response = await client.index({
|
|
173
|
-
id: id,
|
|
174
|
-
index: index_name,
|
|
175
|
-
body: document,
|
|
176
|
-
refresh: true,
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
console.log(response.body);
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
## Search for the Document
|
|
183
|
-
|
|
184
|
-
```javascript
|
|
185
|
-
console.log('Search results:');
|
|
186
|
-
|
|
187
|
-
var query = {
|
|
188
|
-
query: {
|
|
189
|
-
match: {
|
|
190
|
-
title: {
|
|
191
|
-
query: 'The Outsider',
|
|
192
|
-
},
|
|
193
|
-
},
|
|
194
|
-
},
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
var response = await client.search({
|
|
198
|
-
index: index_name,
|
|
199
|
-
body: query,
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
console.log(response.body.hits);
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
## Multi-Search (msearch)
|
|
206
|
-
|
|
207
|
-
```javascript
|
|
208
|
-
const queries = [
|
|
209
|
-
{},
|
|
210
|
-
{ query: { match: { author: 'Stephen King' } } },
|
|
211
|
-
{},
|
|
212
|
-
{ query: { match: { title: 'The Outsider' } } },
|
|
213
|
-
];
|
|
214
|
-
|
|
215
|
-
const multiSearchResponse = await client.msearch({
|
|
216
|
-
index: index_name,
|
|
217
|
-
body: queries,
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
multiSearchResponse.body.responses.map((res) =>
|
|
221
|
-
res.hits.hits.map((movie) => {
|
|
222
|
-
console.log(movie._source);
|
|
223
|
-
})
|
|
224
|
-
);
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
Explore `msearch` further with a detailed guide in [msearch.md](guides/msearch.md) and find sample code in [msearch.js](samples/msearch.js).
|
|
228
|
-
|
|
229
|
-
## Delete the document
|
|
230
|
-
|
|
231
|
-
```javascript
|
|
232
|
-
console.log('Deleting document:');
|
|
233
|
-
|
|
234
|
-
var response = await client.delete({
|
|
235
|
-
index: index_name,
|
|
236
|
-
id: id,
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
console.log(response.body);
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
## Delete the index
|
|
243
|
-
|
|
244
|
-
```javascript
|
|
245
|
-
console.log('Deleting index:');
|
|
246
|
-
|
|
247
|
-
var response = await client.indices.delete({
|
|
248
|
-
index: index_name,
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
console.log(response.body);
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
## Create a Point in Time
|
|
255
|
-
|
|
256
|
-
```javascript
|
|
257
|
-
console.log('Creating a PIT:');
|
|
258
|
-
|
|
259
|
-
var response = await client.createPit({
|
|
260
|
-
index: 'books*',
|
|
261
|
-
keep_alive: '100m',
|
|
262
|
-
expand_wildcards: 'all',
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
console.log(response.body);
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
## Get all PITs
|
|
269
|
-
|
|
270
|
-
```javascript
|
|
271
|
-
console.log('Getting all PITs:');
|
|
272
|
-
|
|
273
|
-
var response = await client.getAllPits();
|
|
274
|
-
|
|
275
|
-
console.log(response.body);
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
## Delete a Point in Time
|
|
279
|
-
|
|
280
|
-
```javascript
|
|
281
|
-
console.log('Deleting a PIT:');
|
|
282
|
-
|
|
283
|
-
var response = await client.deletePit({
|
|
284
|
-
body: {
|
|
285
|
-
pit_id: [
|
|
286
|
-
'o463QQEPbXktaW5kZXgtMDAwMDAxFkhGN09fMVlPUkVPLXh6MUExZ1hpaEEAFjBGbmVEZHdGU1EtaFhhUFc4ZkR5cWcAAAAAAAAAAAEWaXBPNVJtZEhTZDZXTWFFR05waXdWZwEWSEY3T18xWU9SRU8teHoxQTFnWGloQQAA',
|
|
287
|
-
],
|
|
288
|
-
},
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
console.log(response.body);
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
## Delete all PITs
|
|
295
|
-
|
|
296
|
-
```javascript
|
|
297
|
-
console.log('Deleting all PITs:');
|
|
298
|
-
|
|
299
|
-
var response = await client.deleteAllPits();
|
|
300
|
-
|
|
301
|
-
console.log(response.body);
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
## Empty all Pool Connections
|
|
305
|
-
|
|
306
|
-
```javascript
|
|
307
|
-
var pool = new ConnectionPool({ Connection });
|
|
308
|
-
pool.addConnection('http://localhost:9200/');
|
|
309
|
-
pool.addConnection('http://localhost:9201/');
|
|
310
|
-
|
|
311
|
-
pool.empty();
|
|
312
|
-
// OR
|
|
313
|
-
pool.empty(() => {
|
|
314
|
-
// Do something after emptying the pool
|
|
315
|
-
});
|
|
316
|
-
```
|
package/codecov.yml
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
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
|
-
|
|
10
|
-
const client = new Client({
|
|
11
|
-
node: 'http://localhost:9200',
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
client.indices.create({index: 'movies'})
|
|
15
|
-
```
|
|
16
|
-
## API Actions
|
|
17
|
-
### Clear index cache
|
|
18
|
-
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:
|
|
19
|
-
|
|
20
|
-
```javascript
|
|
21
|
-
client.indices.clear_cache({index: 'movies'})
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
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:
|
|
25
|
-
|
|
26
|
-
```javascript
|
|
27
|
-
client.indices.clear_cache({index: 'movies', query: true})
|
|
28
|
-
client.indices.clear_cache({index: 'movies', fielddata: true, request: true})
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Flush index
|
|
32
|
-
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:
|
|
33
|
-
|
|
34
|
-
```javascript
|
|
35
|
-
client.indices.flush({index: 'movies'})
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
### Refresh index
|
|
39
|
-
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:
|
|
40
|
-
|
|
41
|
-
```javascript
|
|
42
|
-
client.indices.refresh({index: 'movies'})
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### Open/Close index
|
|
46
|
-
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:
|
|
47
|
-
|
|
48
|
-
```javascript
|
|
49
|
-
client.indices.close({index: 'movies'})
|
|
50
|
-
client.indices.open({index: 'movies'})
|
|
51
|
-
```
|
|
52
|
-
### Force merge index
|
|
53
|
-
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:
|
|
54
|
-
|
|
55
|
-
```javascript
|
|
56
|
-
client.indices.forcemerge({index: 'movies'})
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
### Clone index
|
|
60
|
-
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:
|
|
61
|
-
|
|
62
|
-
```javascript
|
|
63
|
-
client.indices.add_block({index: 'movies', block: 'write'})
|
|
64
|
-
client.indices.clone({index: 'movies', target: 'movies_clone'})
|
|
65
|
-
client.indices.put_settings({index: 'movies', body: { index: { blocks: { write: false } } } })
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### Split index
|
|
69
|
-
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:
|
|
70
|
-
|
|
71
|
-
```javascript
|
|
72
|
-
client.indices.create({
|
|
73
|
-
index: 'books',
|
|
74
|
-
body: { settings: {
|
|
75
|
-
index: { number_of_shards: 5,
|
|
76
|
-
number_of_routing_shards: 30,
|
|
77
|
-
blocks: { write: true } } } } })
|
|
78
|
-
|
|
79
|
-
client.indices.split({
|
|
80
|
-
index: 'books',
|
|
81
|
-
target: 'bigger_books',
|
|
82
|
-
body: { settings: { index: { number_of_shards: 10 } } } })
|
|
83
|
-
|
|
84
|
-
client.indices.put_settings({index: 'books', body: { index: { blocks: { write: false } } } })
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## Cleanup
|
|
88
|
-
|
|
89
|
-
Let's delete all the indices we created in this guide:
|
|
90
|
-
```javascript
|
|
91
|
-
client.indices.delete({index: ['movies', 'books', 'movies_clone', 'bigger_books']});
|
|
92
|
-
```
|
package/guides/bulk.md
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
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
|
-
if ((await client.indices.exists({ index: movies })).body)
|
|
23
|
-
await client.indices.delete({ index: movies });
|
|
24
|
-
if ((await client.indices.exists({ index: books })).body)
|
|
25
|
-
await client.indices.delete({ index: books });
|
|
26
|
-
|
|
27
|
-
await client.indices.create({ index: movies });
|
|
28
|
-
await client.indices.create({ index: books });
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
## Bulk API
|
|
33
|
-
|
|
34
|
-
The `bulk` API action allows you to perform multiple 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
|
-
|
|
38
|
-
The following code create 3 documents in the `movies` index and one in the `books` index using the `bulk` API action:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
```javascript
|
|
42
|
-
client
|
|
43
|
-
.bulk({
|
|
44
|
-
index: movies, // the default index
|
|
45
|
-
body: [
|
|
46
|
-
{ create: {} },
|
|
47
|
-
{ title: 'Beauty and the Beast', year: 2030, director: 'Ella' },
|
|
48
|
-
{ index : {} },
|
|
49
|
-
{ title: 'Snow White', year: 2031, director: 'Jake' },
|
|
50
|
-
{ create: {} },
|
|
51
|
-
{ title: 'Cinderella', year: 2032, director: 'Clyde' },
|
|
52
|
-
{ index: { _index: books } },
|
|
53
|
-
{ title: 'The Lion King', year: 1994, author: 'John' },
|
|
54
|
-
],
|
|
55
|
-
})
|
|
56
|
-
.then((response) => {
|
|
57
|
-
console.log(response.body.items);
|
|
58
|
-
});
|
|
59
|
-
```
|
|
60
|
-
As you can see, each `create` operation comprises 2 objects: the first object contain the directive for the operation, and the second object is the document to be indexed. Notice that the first 3 operations do not specify the index name, so the `movies` index is used by default.
|
|
61
|
-
|
|
62
|
-
### Mixing Operations
|
|
63
|
-
|
|
64
|
-
Take a look at the code block below:
|
|
65
|
-
|
|
66
|
-
```javascript
|
|
67
|
-
client
|
|
68
|
-
.bulk({
|
|
69
|
-
index: movies, // The default index
|
|
70
|
-
body: [
|
|
71
|
-
{ create: { _id: 1 } },
|
|
72
|
-
{ title: 'Beauty and the Beast 1', year: 2050 },
|
|
73
|
-
|
|
74
|
-
{ delete: { _id: 1 } },
|
|
75
|
-
|
|
76
|
-
{ create: { _id: 2 } },
|
|
77
|
-
{ title: 'Beauty and the Beast 2', year: 2051 },
|
|
78
|
-
|
|
79
|
-
{ create: {} },
|
|
80
|
-
{ title: 'Beauty and the Beast 2', year: 2051 },
|
|
81
|
-
|
|
82
|
-
{ create: { _index: books } },
|
|
83
|
-
{ title: '2012', year: 2012 },
|
|
84
|
-
],
|
|
85
|
-
})
|
|
86
|
-
.then((response) => {
|
|
87
|
-
console.log(response.body.items);
|
|
88
|
-
});
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
This single `bulk` request contains 5 operations:
|
|
92
|
-
- Creates a document with the ID `1` in the `movies` index.
|
|
93
|
-
- Deletes the document with the ID `1` in the `movies` index.
|
|
94
|
-
- Creates a document with the ID `2` in the `movies` index.
|
|
95
|
-
- Creates a document in the `movies` index (since `_id` is not specified, a new ID is generated automatically).
|
|
96
|
-
- Creates a document in the `books` index (since `movies` is the default index, you have to specify `books` for `_index`).
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
## Bulk Helper
|
|
100
|
-
|
|
101
|
-
The OpenSearch JavaScript client provides a `bulk` helper that simplifies the process of creating a bulk request for operations of the same type. The following code block demonstrates how to use the `bulk` helper to create one document in the `movies` index for each item in the `docs` array:
|
|
102
|
-
|
|
103
|
-
```javascript
|
|
104
|
-
const docs = [
|
|
105
|
-
{ title: 'Beauty and the Beast 1', year: 2050 },
|
|
106
|
-
{ title: 'Beauty and the Beast 2', year: 2051 },
|
|
107
|
-
];
|
|
108
|
-
|
|
109
|
-
client.helpers
|
|
110
|
-
.bulk({
|
|
111
|
-
datasource: docs,
|
|
112
|
-
onDocument(_) {
|
|
113
|
-
return { index: { _index: movies } };
|
|
114
|
-
},
|
|
115
|
-
})
|
|
116
|
-
.then((result) => {
|
|
117
|
-
console.log(result);
|
|
118
|
-
});
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
## Cleanup
|
|
122
|
-
To clean up the resources created in this guide, delete the `movies` and `books` indices:
|
|
123
|
-
|
|
124
|
-
```javascript
|
|
125
|
-
client.indices.delete({ index: [movies, books] });
|
|
126
|
-
```
|