@opensearch-project/opensearch 2.1.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.
Files changed (83) hide show
  1. package/.eslintrc.json +4 -1
  2. package/CHANGELOG.md +125 -6
  3. package/DEVELOPER_GUIDE.md +55 -5
  4. package/MAINTAINERS.md +10 -6
  5. package/RELEASING.md +8 -1
  6. package/USER_GUIDE.md +133 -63
  7. package/api/api/bulk.js +23 -0
  8. package/api/api/cat.js +319 -0
  9. package/api/api/clear_scroll.js +14 -0
  10. package/api/api/cluster.js +237 -0
  11. package/api/api/count.js +28 -0
  12. package/api/api/create.js +26 -0
  13. package/api/api/create_pit.js +88 -0
  14. package/api/api/dangling_indices.js +45 -0
  15. package/api/api/delete.js +23 -0
  16. package/api/api/delete_all_pits.js +53 -0
  17. package/api/api/delete_by_query.js +48 -0
  18. package/api/api/delete_by_query_rethrottle.js +14 -0
  19. package/api/api/delete_pit.js +61 -0
  20. package/api/api/delete_script.js +14 -0
  21. package/api/api/exists.js +25 -0
  22. package/api/api/exists_source.js +25 -0
  23. package/api/api/explain.js +27 -0
  24. package/api/api/features.js +24 -0
  25. package/api/api/field_caps.js +19 -0
  26. package/api/api/get.js +25 -0
  27. package/api/api/get_all_pits.js +53 -0
  28. package/api/api/get_script.js +16 -0
  29. package/api/api/get_script_context.js +11 -0
  30. package/api/api/get_script_languages.js +11 -0
  31. package/api/api/get_source.js +25 -0
  32. package/api/api/index.js +29 -0
  33. package/api/api/indices.js +815 -0
  34. package/api/api/info.js +11 -0
  35. package/api/api/ingest.js +89 -0
  36. package/api/api/mget.js +24 -0
  37. package/api/api/msearch.js +22 -0
  38. package/api/api/msearch_template.js +19 -0
  39. package/api/api/nodes.js +91 -0
  40. package/api/api/ping.js +11 -0
  41. package/api/api/put_script.js +17 -0
  42. package/api/api/rank_eval.js +19 -0
  43. package/api/api/reindex.js +22 -0
  44. package/api/api/reindex_rethrottle.js +14 -0
  45. package/api/api/render_search_template.js +14 -0
  46. package/api/api/scripts_painless_execute.js +13 -0
  47. package/api/api/scroll.js +17 -0
  48. package/api/api/search.js +60 -0
  49. package/api/api/search_shards.js +19 -0
  50. package/api/api/search_template.js +27 -0
  51. package/api/api/shutdown.js +3 -0
  52. package/api/api/snapshot.js +210 -0
  53. package/api/api/tasks.js +56 -0
  54. package/api/api/terms_enum.js +1 -0
  55. package/api/api/termvectors.js +26 -0
  56. package/api/api/update.js +28 -0
  57. package/api/api/update_by_query.js +50 -0
  58. package/api/api/update_by_query_rethrottle.js +14 -0
  59. package/api/index.js +28 -0
  60. package/api/new.d.ts +64 -0
  61. package/api/opensearch_dashboards.d.ts +4 -0
  62. package/api/requestParams.d.ts +16 -0
  63. package/api/types.d.ts +53 -6
  64. package/guides/advanced_index_actions.md +91 -0
  65. package/guides/bulk.md +166 -0
  66. package/guides/document_lifecycle.md +162 -0
  67. package/guides/index_lifecycle.md +152 -0
  68. package/guides/index_template.md +228 -0
  69. package/guides/search.md +217 -0
  70. package/index.d.ts +160 -0
  71. package/jenkins/release.JenkinsFile +2 -2
  72. package/lib/Connection.js +1 -1
  73. package/lib/Serializer.js +2 -2
  74. package/lib/Transport.js +23 -1
  75. package/lib/aws/AwsSigv4Signer.js +14 -5
  76. package/lib/aws/index.d.ts +1 -0
  77. package/lib/errors.d.ts +2 -2
  78. package/lib/errors.js +2 -2
  79. package/lib/pool/BaseConnectionPool.js +18 -9
  80. package/lib/pool/CloudConnectionPool.js +2 -2
  81. package/lib/pool/ConnectionPool.js +4 -4
  82. package/lib/pool/index.d.ts +35 -6
  83. package/package.json +13 -12
@@ -0,0 +1,217 @@
1
+ # Search
2
+ OpenSearch provides a powerful search API that allows you to search for documents in an index. The search API supports a number of parameters that allow you to customize the search operation. In this guide, we will explore the search API and its parameters.
3
+ # Setup
4
+ Let's start by creating an index and adding some documents to it:
5
+ ```javascript
6
+ var host = "localhost";
7
+ var protocol = "https";
8
+ var port = 9200;
9
+ var auth = "admin:admin"; // For testing only. Don't store credentials in code.
10
+ var ca_certs_path = "/full/path/to/root-ca.pem";
11
+
12
+ // Optional client certificates if you don't want to use HTTP basic authentication.
13
+ // var client_cert_path = '/full/path/to/client.pem'
14
+ // var client_key_path = '/full/path/to/client-key.pem'
15
+
16
+ // Create a client with SSL/TLS enabled.
17
+ var { Client } = require("@opensearch-project/opensearch");
18
+ var fs = require("fs");
19
+ var client = new Client({
20
+ node: protocol + "://" + auth + "@" + host + ":" + port,
21
+ ssl: {
22
+ ca: fs.readFileSync(ca_certs_path),
23
+ // You can turn off certificate verification (rejectUnauthorized: false) if you're using
24
+ // self-signed certificates with a hostname mismatch.
25
+ // cert: fs.readFileSync(client_cert_path),
26
+ // key: fs.readFileSync(client_key_path)
27
+ },
28
+ });
29
+
30
+ await client.indices.create({index: 'movies'});
31
+
32
+ for(let i = 0; i < 10; i++) {
33
+ await client.index({
34
+ index: 'movies',
35
+ id: i,
36
+ body: {
37
+ title: `The Dark Knight ${i}`,
38
+ director: 'Christopher Nolan',
39
+ year: 2008 + i
40
+ }
41
+ });
42
+ }
43
+
44
+ await client.index({
45
+ index: 'movies',
46
+ body: {
47
+ title: 'The Godfather',
48
+ director: 'Francis Ford Coppola',
49
+ year: 1972
50
+ }
51
+ });
52
+
53
+ await client.index({
54
+ index: 'movies',
55
+ body: {
56
+ title: 'The Shawshank Redemption',
57
+ director: 'Frank Darabont',
58
+ year: 1994
59
+ }
60
+ });
61
+
62
+ await client.index({index: 'movies', refresh: true});
63
+ ```
64
+ # Search API
65
+ ### Basic Search
66
+ The search API allows you to search for documents in an index. The following example searches for ALL documents in the movies index:
67
+
68
+ ```javascript
69
+ const response = await client.search({
70
+ index: 'movies'
71
+ });
72
+ ```
73
+ You can also search for documents that match a specific query. The following example searches for documents that match the query `dark knight`:
74
+ ```javascript
75
+ const response = await client.search({
76
+ index: 'movies',
77
+ body: {
78
+ query: {
79
+ match: {
80
+ title: 'dark knight'
81
+ }
82
+ }
83
+ }
84
+ });
85
+ console.log(response.body.hits.hits);
86
+ ```
87
+
88
+ OpenSearch query DSL allows you to specify complex queries. Check out the [OpenSearch query DSL documentation](https://opensearch.org/docs/latest/query-dsl/) for more information.
89
+
90
+ ### Basic Pagination
91
+ The search API allows you to paginate through the search results. The following example searches for documents that match the query `dark knight`, sorted by `year` in ascending order, and returns the first 2 results after skipping the first 5 results:
92
+ ```javascript
93
+ search_body = {
94
+ query: {
95
+ match: {
96
+ title: 'dark knight'
97
+ }
98
+ },
99
+ sort: [
100
+ {
101
+ year: {
102
+ order: 'asc'
103
+ }
104
+ }
105
+ ]
106
+ }
107
+
108
+ await client.search({
109
+ index: 'movies',
110
+ size: 2,
111
+ from: 5,
112
+ body: search_body
113
+ });
114
+ ```
115
+
116
+ With sorting, you can also use the `search_after` parameter to paginate through the search results. Let's say you have already displayed the first page of results, and you want to display the next page. You can use the `search_after` parameter to paginate through the search results. The following example will demonstrate how to get the first 3 pages of results using the search query of the previous example:
117
+ ```javascript
118
+ const page_1 = await client.search({
119
+ index: 'movies',
120
+ size: 2,
121
+ body: search_body
122
+ });
123
+
124
+ const page_2 = await client.search({
125
+ index: 'movies',
126
+ size: 2,
127
+ body: {
128
+ ...search_body,
129
+ search_after: page_1[page_1.length - 1].sort
130
+ }
131
+ });
132
+
133
+ const page_3 = await client.search({
134
+ index: 'movies',
135
+ size: 2,
136
+ body: {
137
+ ...search_body,
138
+ search_after: page_2[page_2.length - 1].sort
139
+ }
140
+ })
141
+ ```
142
+ ### Pagination with scroll
143
+ When retrieving large amounts of non-real-time data, you can use the `scroll` parameter to paginate through the search results.
144
+ ```javascript
145
+ const page_1 = await client.search({
146
+ index: 'movies',
147
+ scroll: '1m',
148
+ size: 2,
149
+ body: search_body
150
+ });
151
+
152
+ const page_2 = await client.scroll({
153
+ scroll_id: page_1._scroll_id,
154
+ scroll: '1m'
155
+ });
156
+
157
+ const page_3 = await client.scroll({
158
+ scroll_id: page_2._scroll_id,
159
+ scroll: '1m'
160
+ });
161
+ ```
162
+ ### Pagination with point in time
163
+ The scroll example above has one weakness: if the index is updated while you are scrolling through the results, they will be paginated inconsistently. To avoid this, you should use the "Point in Time" feature. The following example demonstrates how to use the `point_in_time` and `pit_id` parameters to paginate through the search results:
164
+ ```javascript
165
+ /** Create a point in time */
166
+ const pit = client.create_pit({
167
+ index: 'movies',
168
+ keep_alive: '1m'
169
+ });
170
+
171
+ /** Include pit info in the search body */
172
+ const pit_search_body = {
173
+ ..search_body,
174
+ pit: {
175
+ id: pit.pit_id,
176
+ keep_alive: '1m'
177
+ }
178
+ }
179
+
180
+ /** Get the first 3 pages of results */
181
+ const page_1 = await client.search({
182
+ size: 2,
183
+ body: pit_search_body
184
+ });
185
+ console.log(page_1.body.hits.hits);
186
+
187
+ const page_2 = await client.search({
188
+ size: 2,
189
+ body: {
190
+ ...pit_search_body,
191
+ search_after: page_1[page_1.length - 1].sort
192
+ }
193
+ });
194
+ console.log(page_2.body.hits.hits);
195
+
196
+ const page_3 = await client.search({
197
+ size: 2,
198
+ body: {
199
+ ...pit_search_body,
200
+ search_after: page2[page_2.length-1].sort
201
+ }
202
+ });
203
+ console.log(page_3.body.hits.hits);
204
+
205
+ /** Print out the titles of the first 3 pages of results */
206
+ console.log(page_1.map(hit => hit._source.title));
207
+ console.log(page_2.map(hit => hit._source.title));
208
+ console.log(page_3.map(hit => hit._source.title));
209
+
210
+ /** Delete the point in time */
211
+ client.delete_pit({body: {pit_id: pit.pit_id}});
212
+ ```
213
+ Note that a point-in-time is associated with an index or a set of index. So, when performing a search with a point-in-time, you DO NOT specify the index in the search.
214
+ # Cleanup
215
+ ```javascript
216
+ await client.indices.delete({index: 'movies'});
217
+ ```
package/index.d.ts CHANGED
@@ -1228,6 +1228,38 @@ declare class Client {
1228
1228
  options: TransportRequestOptions,
1229
1229
  callback: callbackFn<TResponse, TContext>
1230
1230
  ): TransportRequestCallback;
1231
+ create_pit<TResponse = Record<string, any>, TContext = Context>(
1232
+ params?: RequestParams.CreatePit,
1233
+ options?: TransportRequestOptions
1234
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1235
+ create_pit<TResponse = Record<string, any>, TContext = Context>(
1236
+ callback: callbackFn<TResponse, TContext>
1237
+ ): TransportRequestCallback;
1238
+ create_pit<TResponse = Record<string, any>, TContext = Context>(
1239
+ params: RequestParams.CreatePit,
1240
+ callback: callbackFn<TResponse, TContext>
1241
+ ): TransportRequestCallback;
1242
+ create_pit<TResponse = Record<string, any>, TContext = Context>(
1243
+ params: RequestParams.CreatePit,
1244
+ options: TransportRequestOptions,
1245
+ callback: callbackFn<TResponse, TContext>
1246
+ ): TransportRequestCallback;
1247
+ createPit<TResponse = Record<string, any>, TContext = Context>(
1248
+ params?: RequestParams.CreatePit,
1249
+ options?: TransportRequestOptions
1250
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1251
+ createPit<TResponse = Record<string, any>, TContext = Context>(
1252
+ callback: callbackFn<TResponse, TContext>
1253
+ ): TransportRequestCallback;
1254
+ createPit<TResponse = Record<string, any>, TContext = Context>(
1255
+ params: RequestParams.CreatePit,
1256
+ callback: callbackFn<TResponse, TContext>
1257
+ ): TransportRequestCallback;
1258
+ createPit<TResponse = Record<string, any>, TContext = Context>(
1259
+ params: RequestParams.CreatePit,
1260
+ options: TransportRequestOptions,
1261
+ callback: callbackFn<TResponse, TContext>
1262
+ ): TransportRequestCallback;
1231
1263
  dangling_indices: {
1232
1264
  delete_dangling_index<TResponse = Record<string, any>, TContext = Context>(
1233
1265
  params?: RequestParams.DanglingIndicesDeleteDanglingIndex,
@@ -1440,6 +1472,38 @@ declare class Client {
1440
1472
  options: TransportRequestOptions,
1441
1473
  callback: callbackFn<TResponse, TContext>
1442
1474
  ): TransportRequestCallback;
1475
+ delete_all_pits<TResponse = Record<string, any>, TContext = Context>(
1476
+ params?: RequestParams.DeleteAllPits,
1477
+ options?: TransportRequestOptions
1478
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1479
+ delete_all_pits<TResponse = Record<string, any>, TContext = Context>(
1480
+ callback: callbackFn<TResponse, TContext>
1481
+ ): TransportRequestCallback;
1482
+ delete_all_pits<TResponse = Record<string, any>, TContext = Context>(
1483
+ params: RequestParams.DeleteAllPits,
1484
+ callback: callbackFn<TResponse, TContext>
1485
+ ): TransportRequestCallback;
1486
+ delete_all_pits<TResponse = Record<string, any>, TContext = Context>(
1487
+ params: RequestParams.DeleteAllPits,
1488
+ options: TransportRequestOptions,
1489
+ callback: callbackFn<TResponse, TContext>
1490
+ ): TransportRequestCallback;
1491
+ deleteAllPits<TResponse = Record<string, any>, TContext = Context>(
1492
+ params?: RequestParams.DeleteAllPits,
1493
+ options?: TransportRequestOptions
1494
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1495
+ deleteAllPits<TResponse = Record<string, any>, TContext = Context>(
1496
+ callback: callbackFn<TResponse, TContext>
1497
+ ): TransportRequestCallback;
1498
+ deleteAllPits<TResponse = Record<string, any>, TContext = Context>(
1499
+ params: RequestParams.DeleteAllPits,
1500
+ callback: callbackFn<TResponse, TContext>
1501
+ ): TransportRequestCallback;
1502
+ deleteAllPits<TResponse = Record<string, any>, TContext = Context>(
1503
+ params: RequestParams.DeleteAllPits,
1504
+ options: TransportRequestOptions,
1505
+ callback: callbackFn<TResponse, TContext>
1506
+ ): TransportRequestCallback;
1443
1507
  delete_by_query<
1444
1508
  TResponse = Record<string, any>,
1445
1509
  TRequestBody extends RequestBody = Record<string, any>,
@@ -1532,6 +1596,70 @@ declare class Client {
1532
1596
  options: TransportRequestOptions,
1533
1597
  callback: callbackFn<TResponse, TContext>
1534
1598
  ): TransportRequestCallback;
1599
+ delete_pit<
1600
+ TResponse = Record<string, any>,
1601
+ TRequestBody extends RequestBody = Record<string, any>,
1602
+ TContext = Context
1603
+ >(
1604
+ params?: RequestParams.DeletePit<TRequestBody>,
1605
+ options?: TransportRequestOptions
1606
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1607
+ delete_pit<
1608
+ TResponse = Record<string, any>,
1609
+ TRequestBody extends RequestBody = Record<string, any>,
1610
+ TContext = Context
1611
+ >(
1612
+ callback: callbackFn<TResponse, TContext>
1613
+ ): TransportRequestCallback;
1614
+ delete_pit<
1615
+ TResponse = Record<string, any>,
1616
+ TRequestBody extends RequestBody = Record<string, any>,
1617
+ TContext = Context
1618
+ >(
1619
+ params: RequestParams.DeletePit<TRequestBody>,
1620
+ callback: callbackFn<TResponse, TContext>
1621
+ ): TransportRequestCallback;
1622
+ delete_pit<
1623
+ TResponse = Record<string, any>,
1624
+ TRequestBody extends RequestBody = Record<string, any>,
1625
+ TContext = Context
1626
+ >(
1627
+ params: RequestParams.DeletePit<TRequestBody>,
1628
+ options: TransportRequestOptions,
1629
+ callback: callbackFn<TResponse, TContext>
1630
+ ): TransportRequestCallback;
1631
+ deletePit<
1632
+ TResponse = Record<string, any>,
1633
+ TRequestBody extends RequestBody = Record<string, any>,
1634
+ TContext = Context
1635
+ >(
1636
+ params?: RequestParams.DeletePit<TRequestBody>,
1637
+ options?: TransportRequestOptions
1638
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1639
+ deletePit<
1640
+ TResponse = Record<string, any>,
1641
+ TRequestBody extends RequestBody = Record<string, any>,
1642
+ TContext = Context
1643
+ >(
1644
+ callback: callbackFn<TResponse, TContext>
1645
+ ): TransportRequestCallback;
1646
+ deletePit<
1647
+ TResponse = Record<string, any>,
1648
+ TRequestBody extends RequestBody = Record<string, any>,
1649
+ TContext = Context
1650
+ >(
1651
+ params: RequestParams.DeletePit<TRequestBody>,
1652
+ callback: callbackFn<TResponse, TContext>
1653
+ ): TransportRequestCallback;
1654
+ deletePit<
1655
+ TResponse = Record<string, any>,
1656
+ TRequestBody extends RequestBody = Record<string, any>,
1657
+ TContext = Context
1658
+ >(
1659
+ params: RequestParams.DeletePit<TRequestBody>,
1660
+ options: TransportRequestOptions,
1661
+ callback: callbackFn<TResponse, TContext>
1662
+ ): TransportRequestCallback;
1535
1663
  delete_script<TResponse = Record<string, any>, TContext = Context>(
1536
1664
  params?: RequestParams.DeleteScript,
1537
1665
  options?: TransportRequestOptions
@@ -1784,6 +1912,38 @@ declare class Client {
1784
1912
  options: TransportRequestOptions,
1785
1913
  callback: callbackFn<TResponse, TContext>
1786
1914
  ): TransportRequestCallback;
1915
+ get_all_pits<TResponse = Record<string, any>, TContext = Context>(
1916
+ params?: RequestParams.GetAllPits,
1917
+ options?: TransportRequestOptions
1918
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1919
+ get_all_pits<TResponse = Record<string, any>, TContext = Context>(
1920
+ callback: callbackFn<TResponse, TContext>
1921
+ ): TransportRequestCallback;
1922
+ get_all_pits<TResponse = Record<string, any>, TContext = Context>(
1923
+ params: RequestParams.GetAllPits,
1924
+ callback: callbackFn<TResponse, TContext>
1925
+ ): TransportRequestCallback;
1926
+ get_all_pits<TResponse = Record<string, any>, TContext = Context>(
1927
+ params: RequestParams.GetAllPits,
1928
+ options: TransportRequestOptions,
1929
+ callback: callbackFn<TResponse, TContext>
1930
+ ): TransportRequestCallback;
1931
+ getAllPits<TResponse = Record<string, any>, TContext = Context>(
1932
+ params?: RequestParams.GetAllPits,
1933
+ options?: TransportRequestOptions
1934
+ ): TransportRequestPromise<ApiResponse<TResponse, TContext>>;
1935
+ getAllPits<TResponse = Record<string, any>, TContext = Context>(
1936
+ callback: callbackFn<TResponse, TContext>
1937
+ ): TransportRequestCallback;
1938
+ getAllPits<TResponse = Record<string, any>, TContext = Context>(
1939
+ params: RequestParams.GetAllPits,
1940
+ callback: callbackFn<TResponse, TContext>
1941
+ ): TransportRequestCallback;
1942
+ getAllPits<TResponse = Record<string, any>, TContext = Context>(
1943
+ params: RequestParams.GetAllPits,
1944
+ options: TransportRequestOptions,
1945
+ callback: callbackFn<TResponse, TContext>
1946
+ ): TransportRequestCallback;
1787
1947
  get_script<TResponse = Record<string, any>, TContext = Context>(
1788
1948
  params?: RequestParams.GetScript,
1789
1949
  options?: TransportRequestOptions
@@ -1,4 +1,4 @@
1
- lib = library(identifier: 'jenkins@1.1.0', retriever: modernSCM([
1
+ lib = library(identifier: 'jenkins@1.1.1', retriever: modernSCM([
2
2
  $class: 'GitSCMSource',
3
3
  remote: 'https://github.com/opensearch-project/opensearch-build-libraries.git',
4
4
  ]))
@@ -6,6 +6,6 @@ lib = library(identifier: 'jenkins@1.1.0', retriever: modernSCM([
6
6
  standardReleasePipelineWithGenericTrigger(
7
7
  tokenIdCredential: 'jenkins-opensearch-js-generic-webhook-token',
8
8
  causeString: 'A tag was cut on opensearch-project/opensearch-js repository causing this workflow to run',
9
- publishRelease: true){
9
+ publishRelease: true) {
10
10
  publishToNpm(repository: 'https://github.com/opensearch-project/opensearch-js', tag: "$tag")
11
11
  }
package/lib/Connection.js CHANGED
@@ -134,7 +134,7 @@ class Connection {
134
134
  request.once('error', () => {}); // we need to catch the request aborted error
135
135
  debug('Request aborted', params);
136
136
  this._openRequests--;
137
- callback(new RequestAbortedError(), null);
137
+ callback(new RequestAbortedError('Request aborted'), null);
138
138
  };
139
139
 
140
140
  request.on('response', onResponse);
package/lib/Serializer.js CHANGED
@@ -69,7 +69,7 @@ class Serializer {
69
69
  ndserialize(array) {
70
70
  debug('ndserialize', array);
71
71
  if (Array.isArray(array) === false) {
72
- throw new SerializationError('The argument provided is not an array');
72
+ throw new SerializationError('The argument provided is not an array', array);
73
73
  }
74
74
  let ndjson = '';
75
75
  for (let i = 0, len = array.length; i < len; i++) {
@@ -90,7 +90,7 @@ class Serializer {
90
90
  const keys = Object.keys(object);
91
91
  for (let i = 0, len = keys.length; i < len; i++) {
92
92
  const key = keys[i];
93
- // OpenSearch will complain for keys without a value
93
+ // OpenSearch will complain about keys without a value
94
94
  if (object[key] === undefined) {
95
95
  delete object[key];
96
96
  } else if (Array.isArray(object[key]) === true) {
package/lib/Transport.js CHANGED
@@ -56,6 +56,7 @@ const HEAP_SIZE_LIMIT = require('v8').getHeapStatistics().heap_size_limit;
56
56
  const kCompatibleCheck = Symbol('compatible check');
57
57
  const kApiVersioning = Symbol('api versioning');
58
58
 
59
+ /** Default Transport Layer */
59
60
  class Transport {
60
61
  constructor(opts) {
61
62
  if (typeof opts.compression === 'string' && opts.compression !== 'gzip') {
@@ -110,6 +111,27 @@ class Transport {
110
111
  }
111
112
  }
112
113
 
114
+ /**
115
+ * @param {Object} params
116
+ * @param {string} params.method - HTTP Method (e.g. HEAD, GET, POST...)
117
+ * @param {string} params.path - Relative URL path
118
+ * @param {Object | string} [params.body] - Body of a standard request.
119
+ * @param {Object[] | string} [params.bulkBody] - Body of a bulk request.
120
+ * @param {Object[] | string} [params.querystring] - Querystring params.
121
+ *
122
+ * @param {Object} options
123
+ * @param {number} [options.id] - Request ID
124
+ * @param {Object} [options.context] - Object used for observability
125
+ * @param {number} [options.maxRetries] - Max number of retries
126
+ * @param {false | 'gzip'} [options.compression] - Request body compression, if any
127
+ * @param {boolean} [options.asStream] - Whether to emit the response as stream
128
+ * @param {number[]} [options.ignore] - Response's Error Status Codes to ignore
129
+ * @param {Object} [options.headers] - Request headers
130
+ * @param {Object | string} [options.querystring] - Request's query string
131
+ * @param {number} [options.requestTimeout] - Max request timeout in milliseconds
132
+ *
133
+ * @param {function} callback - Callback that handles errors and response
134
+ */
113
135
  request(params, options, callback) {
114
136
  options = options || {};
115
137
  if (typeof options === 'function') {
@@ -386,7 +408,7 @@ class Transport {
386
408
  // if the statusCode is 502/3/4 we should run our retry strategy
387
409
  // and mark the connection as dead
388
410
  this.connectionPool.markDead(meta.connection);
389
- // retry logic (we shoukd not retry on "429 - Too Many Requests")
411
+ // retry logic (we should not retry on "429 - Too Many Requests")
390
412
  if (meta.attempts < maxRetries && result.statusCode !== 429) {
391
413
  meta.attempts++;
392
414
  debug(`Retrying request, there are still ${maxRetries - meta.attempts} attempts`, params);
@@ -13,6 +13,7 @@ const Connection = require('../Connection');
13
13
  const Transport = require('../Transport');
14
14
  const aws4 = require('aws4');
15
15
  const AwsSigv4SignerError = require('./errors');
16
+ const crypto = require('crypto');
16
17
 
17
18
  const getAwsSDKCredentialsProvider = async () => {
18
19
  // First try V3
@@ -58,23 +59,31 @@ const awsDefaultCredentialsProvider = () =>
58
59
  });
59
60
  });
60
61
 
61
- function AwsSigv4Signer(opts) {
62
+ function AwsSigv4Signer(opts = {}) {
62
63
  const credentialsState = {
63
64
  credentials: null,
64
65
  };
65
- if (opts && (!opts.region || opts.region === null || opts.region === '')) {
66
+ if (!opts.region) {
66
67
  throw new AwsSigv4SignerError('Region cannot be empty');
67
68
  }
68
- if (opts && typeof opts.getCredentials !== 'function') {
69
+ if (!opts.service) {
70
+ opts.service = 'es';
71
+ }
72
+ if (typeof opts.getCredentials !== 'function') {
69
73
  opts.getCredentials = awsDefaultCredentialsProvider;
70
74
  }
71
75
 
72
76
  function buildSignedRequestObject(request = {}) {
73
- request.service = 'es';
77
+ request.service = opts.service;
74
78
  request.region = opts.region;
75
79
  request.headers = request.headers || {};
76
80
  request.headers['host'] = request.hostname;
77
- return aws4.sign(request, credentialsState.credentials);
81
+ const signed = aws4.sign(request, credentialsState.credentials);
82
+ signed.headers['x-amz-content-sha256'] = crypto
83
+ .createHash('sha256')
84
+ .update(request.body || '', 'utf8')
85
+ .digest('hex');
86
+ return signed;
78
87
  }
79
88
 
80
89
  class AwsSigv4SignerConnection extends Connection {
@@ -19,6 +19,7 @@ import { OpenSearchClientError } from '../errors';
19
19
  interface AwsSigv4SignerOptions {
20
20
  getCredentials?: () => Promise<Credentials>;
21
21
  region: string;
22
+ service?: 'es' | 'aoss';
22
23
  }
23
24
 
24
25
  interface AwsSigv4SignerResponse {
package/lib/errors.d.ts CHANGED
@@ -51,7 +51,7 @@ export declare class ConnectionError<
51
51
  name: string;
52
52
  message: string;
53
53
  meta: ApiResponse<TResponse, TContext>;
54
- constructor(message: string, meta: ApiResponse);
54
+ constructor(message: string, meta?: ApiResponse);
55
55
  }
56
56
 
57
57
  export declare class NoLivingConnectionsError<
@@ -104,7 +104,7 @@ export declare class RequestAbortedError<
104
104
  name: string;
105
105
  message: string;
106
106
  meta: ApiResponse<TResponse, TContext>;
107
- constructor(message: string, meta: ApiResponse);
107
+ constructor(message: string, meta?: ApiResponse);
108
108
  }
109
109
 
110
110
  export declare class NotCompatibleError<
package/lib/errors.js CHANGED
@@ -70,7 +70,7 @@ class NoLivingConnectionsError extends OpenSearchClientError {
70
70
 
71
71
  class SerializationError extends OpenSearchClientError {
72
72
  constructor(message, data) {
73
- super(message, data);
73
+ super(message);
74
74
  Error.captureStackTrace(this, SerializationError);
75
75
  this.name = 'SerializationError';
76
76
  this.message = message || 'Serialization Error';
@@ -80,7 +80,7 @@ class SerializationError extends OpenSearchClientError {
80
80
 
81
81
  class DeserializationError extends OpenSearchClientError {
82
82
  constructor(message, data) {
83
- super(message, data);
83
+ super(message);
84
84
  Error.captureStackTrace(this, DeserializationError);
85
85
  this.name = 'DeserializationError';
86
86
  this.message = message || 'Deserialization Error';
@@ -32,6 +32,7 @@
32
32
  const { URL } = require('url');
33
33
  const debug = require('debug')('opensearch');
34
34
  const Connection = require('../Connection');
35
+ const { ConfigurationError } = require('../errors');
35
36
  const noop = () => {};
36
37
 
37
38
  class BaseConnectionPool {
@@ -64,6 +65,9 @@ class BaseConnectionPool {
64
65
  * Creates a new connection instance.
65
66
  */
66
67
  createConnection(opts) {
68
+ if (opts instanceof Connection) {
69
+ throw new ConfigurationError('The argument provided is already a Connection instance.');
70
+ }
67
71
  if (typeof opts === 'string') {
68
72
  opts = this.urlToHost(opts);
69
73
  }
@@ -102,18 +106,25 @@ class BaseConnectionPool {
102
106
  */
103
107
  addConnection(opts) {
104
108
  if (Array.isArray(opts)) {
105
- return opts.forEach((o) => this.addConnection(o));
109
+ opts.forEach((o) => this.addConnection(o));
110
+ return;
106
111
  }
107
-
108
112
  if (typeof opts === 'string') {
109
113
  opts = this.urlToHost(opts);
110
114
  }
111
115
 
112
- const connectionById = this.connections.find((c) => c.id === opts.id);
113
- const connectionByUrl = this.connections.find((c) => c.id === opts.url.href);
116
+ const connectionId = opts.id;
117
+ const connectionUrl = opts.url.href;
118
+
119
+ if (connectionId || connectionUrl) {
120
+ const connectionById = this.connections.find((c) => c.id === connectionId);
121
+ const connectionByUrl = this.connections.find((c) => c.id === connectionUrl);
114
122
 
115
- if (connectionById || connectionByUrl) {
116
- throw new Error(`Connection with id '${opts.id || opts.url.href}' is already present`);
123
+ if (connectionById || connectionByUrl) {
124
+ throw new ConfigurationError(
125
+ `Connection with id '${connectionId || connectionUrl}' is already present`
126
+ );
127
+ }
117
128
  }
118
129
 
119
130
  this.update([...this.connections, opts]);
@@ -133,10 +144,8 @@ class BaseConnectionPool {
133
144
 
134
145
  /**
135
146
  * Empties the connection pool.
136
- *
137
- * @returns {ConnectionPool}
138
147
  */
139
- empty(callback) {
148
+ empty(callback = noop) {
140
149
  debug('Emptying the connection pool');
141
150
  let openConnections = this.size;
142
151
  this.connections.forEach((connection) => {
@@ -30,6 +30,7 @@
30
30
  'use strict';
31
31
 
32
32
  const BaseConnectionPool = require('./BaseConnectionPool');
33
+ const noop = () => {};
33
34
 
34
35
  class CloudConnectionPool extends BaseConnectionPool {
35
36
  constructor(opts) {
@@ -49,9 +50,8 @@ class CloudConnectionPool extends BaseConnectionPool {
49
50
  /**
50
51
  * Empties the connection pool.
51
52
  *
52
- * @returns {ConnectionPool}
53
53
  */
54
- empty(callback) {
54
+ empty(callback = noop) {
55
55
  super.empty(() => {
56
56
  this.cloudConnection = null;
57
57
  callback();