@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
package/api/index.js CHANGED
@@ -35,10 +35,13 @@ const clearScrollApi = require('./api/clear_scroll');
35
35
  const ClusterApi = require('./api/cluster');
36
36
  const countApi = require('./api/count');
37
37
  const createApi = require('./api/create');
38
+ const createPitApi = require('./api/create_pit');
38
39
  const DanglingIndicesApi = require('./api/dangling_indices');
39
40
  const deleteApi = require('./api/delete');
41
+ const deleteAllPitsApi = require('./api/delete_all_pits');
40
42
  const deleteByQueryApi = require('./api/delete_by_query');
41
43
  const deleteByQueryRethrottleApi = require('./api/delete_by_query_rethrottle');
44
+ const deletePitApi = require('./api/delete_pit');
42
45
  const deleteScriptApi = require('./api/delete_script');
43
46
  const existsApi = require('./api/exists');
44
47
  const existsSourceApi = require('./api/exists_source');
@@ -46,6 +49,7 @@ const explainApi = require('./api/explain');
46
49
  const FeaturesApi = require('./api/features');
47
50
  const fieldCapsApi = require('./api/field_caps');
48
51
  const getApi = require('./api/get');
52
+ const getAllPitsApi = require('./api/get_all_pits');
49
53
  const getScriptApi = require('./api/get_script');
50
54
  const getScriptContextApi = require('./api/get_script_context');
51
55
  const getScriptLanguagesApi = require('./api/get_script_languages');
@@ -109,15 +113,19 @@ OpenSearchAPI.prototype.bulk = bulkApi;
109
113
  OpenSearchAPI.prototype.clearScroll = clearScrollApi;
110
114
  OpenSearchAPI.prototype.count = countApi;
111
115
  OpenSearchAPI.prototype.create = createApi;
116
+ OpenSearchAPI.prototype.createPit = createPitApi;
112
117
  OpenSearchAPI.prototype.delete = deleteApi;
118
+ OpenSearchAPI.prototype.deleteAllPits = deleteAllPitsApi;
113
119
  OpenSearchAPI.prototype.deleteByQuery = deleteByQueryApi;
114
120
  OpenSearchAPI.prototype.deleteByQueryRethrottle = deleteByQueryRethrottleApi;
121
+ OpenSearchAPI.prototype.deletePit = deletePitApi;
115
122
  OpenSearchAPI.prototype.deleteScript = deleteScriptApi;
116
123
  OpenSearchAPI.prototype.exists = existsApi;
117
124
  OpenSearchAPI.prototype.existsSource = existsSourceApi;
118
125
  OpenSearchAPI.prototype.explain = explainApi;
119
126
  OpenSearchAPI.prototype.fieldCaps = fieldCapsApi;
120
127
  OpenSearchAPI.prototype.get = getApi;
128
+ OpenSearchAPI.prototype.getAllPits = getAllPitsApi;
121
129
  OpenSearchAPI.prototype.getScript = getScriptApi;
122
130
  OpenSearchAPI.prototype.getScriptContext = getScriptContextApi;
123
131
  OpenSearchAPI.prototype.getScriptLanguages = getScriptLanguagesApi;
@@ -167,6 +175,11 @@ Object.defineProperties(OpenSearchAPI.prototype, {
167
175
  return this[kCluster];
168
176
  },
169
177
  },
178
+ create_pit: {
179
+ get() {
180
+ return this.createPit;
181
+ },
182
+ },
170
183
  danglingIndices: {
171
184
  get() {
172
185
  if (this[kDanglingIndices] === null) {
@@ -180,6 +193,11 @@ Object.defineProperties(OpenSearchAPI.prototype, {
180
193
  return this.danglingIndices;
181
194
  },
182
195
  },
196
+ delete_all_pits: {
197
+ get() {
198
+ return this.deleteAllPits;
199
+ },
200
+ },
183
201
  delete_by_query: {
184
202
  get() {
185
203
  return this.deleteByQuery;
@@ -190,6 +208,11 @@ Object.defineProperties(OpenSearchAPI.prototype, {
190
208
  return this.deleteByQueryRethrottle;
191
209
  },
192
210
  },
211
+ delete_pit: {
212
+ get() {
213
+ return this.deletePit;
214
+ },
215
+ },
193
216
  delete_script: {
194
217
  get() {
195
218
  return this.deleteScript;
@@ -213,6 +236,11 @@ Object.defineProperties(OpenSearchAPI.prototype, {
213
236
  return this.fieldCaps;
214
237
  },
215
238
  },
239
+ get_all_pits: {
240
+ get() {
241
+ return this.getAllPits;
242
+ },
243
+ },
216
244
  get_script: {
217
245
  get() {
218
246
  return this.getScript;
package/api/new.d.ts CHANGED
@@ -487,6 +487,22 @@ declare class Client {
487
487
  options: TransportRequestOptions,
488
488
  callback: callbackFn<T.CreateResponse, TContext>
489
489
  ): TransportRequestCallback;
490
+ createPit<TContext = unknown>(
491
+ params?: T.PointInTimeCreateRequest,
492
+ options?: TransportRequestOptions
493
+ ): TransportRequestPromise<ApiResponse<T.PointInTimeCreateResponse, TContext>>;
494
+ createPit<TContext = unknown>(
495
+ callback: callbackFn<T.PointInTimeCreateResponse, TContext>
496
+ ): TransportRequestCallback;
497
+ createPit<TContext = unknown>(
498
+ params: T.PointInTimeCreateRequest,
499
+ callback: callbackFn<T.PointInTimeCreateResponse, TContext>
500
+ ): TransportRequestCallback;
501
+ createPit<TContext = unknown>(
502
+ params: T.PointInTimeCreateRequest,
503
+ options: TransportRequestOptions,
504
+ callback: callbackFn<T.PointInTimeCreateResponse, TContext>
505
+ ): TransportRequestCallback;
490
506
  danglingIndices: {
491
507
  deleteDanglingIndex<TContext = unknown>(
492
508
  params?: TODO,
@@ -550,6 +566,22 @@ declare class Client {
550
566
  options: TransportRequestOptions,
551
567
  callback: callbackFn<T.DeleteResponse, TContext>
552
568
  ): TransportRequestCallback;
569
+ deleteAllPits<TContext = unknown>(
570
+ params?: T.PointInTimeDeleteAllRequest,
571
+ options?: TransportRequestOptions
572
+ ): TransportRequestPromise<ApiResponse<T.PointInTimeDeleteAllResponse, TContext>>;
573
+ deleteAllPits<TContext = unknown>(
574
+ callback: callbackFn<T.PointInTimeDeleteAllResponse, TContext>
575
+ ): TransportRequestCallback;
576
+ deleteAllPits<TContext = unknown>(
577
+ params: T.PointInTimeDeleteAllRequest,
578
+ callback: callbackFn<T.PointInTimeDeleteAllResponse, TContext>
579
+ ): TransportRequestCallback;
580
+ deleteAllPits<TContext = unknown>(
581
+ params: T.PointInTimeDeleteAllRequest,
582
+ options: TransportRequestOptions,
583
+ callback: callbackFn<T.PointInTimeDeleteAllResponse, TContext>
584
+ ): TransportRequestCallback;
553
585
  deleteByQuery<TContext = unknown>(
554
586
  params: T.DeleteByQueryRequest,
555
587
  options?: TransportRequestOptions
@@ -576,6 +608,22 @@ declare class Client {
576
608
  options: TransportRequestOptions,
577
609
  callback: callbackFn<T.DeleteByQueryRethrottleResponse, TContext>
578
610
  ): TransportRequestCallback;
611
+ deletePit<TContext = unknown>(
612
+ params?: T.PointInTimeDeleteRequest,
613
+ options?: TransportRequestOptions
614
+ ): TransportRequestPromise<ApiResponse<T.PointInTimeDeleteResponse, TContext>>;
615
+ deletePit<TContext = unknown>(
616
+ callback: callbackFn<T.PointInTimeDeleteResponse, TContext>
617
+ ): TransportRequestCallback;
618
+ deletePit<TContext = unknown>(
619
+ params: T.PointInTimeDeleteRequest,
620
+ callback: callbackFn<T.PointInTimeDeleteResponse, TContext>
621
+ ): TransportRequestCallback;
622
+ deletePit<TContext = unknown>(
623
+ params: T.PointInTimeDeleteRequest,
624
+ options: TransportRequestOptions,
625
+ callback: callbackFn<T.PointInTimeDeleteResponse, TContext>
626
+ ): TransportRequestCallback;
579
627
  deleteScript<TContext = unknown>(
580
628
  params: T.DeleteScriptRequest,
581
629
  options?: TransportRequestOptions
@@ -689,6 +737,22 @@ declare class Client {
689
737
  options: TransportRequestOptions,
690
738
  callback: callbackFn<T.GetResponse<TDocument>, TContext>
691
739
  ): TransportRequestCallback;
740
+ getAllPits<TContext = unknown>(
741
+ params?: T.PointInTimeGetAllRequest,
742
+ options?: TransportRequestOptions
743
+ ): TransportRequestPromise<ApiResponse<T.PointInTimeGetAllResponse, TContext>>;
744
+ getAllPits<TContext = unknown>(
745
+ callback: callbackFn<T.PointInTimeGetAllResponse, TContext>
746
+ ): TransportRequestCallback;
747
+ getAllPits<TContext = unknown>(
748
+ params: T.PointInTimeGetAllRequest,
749
+ callback: callbackFn<T.PointInTimeGetAllResponse, TContext>
750
+ ): TransportRequestCallback;
751
+ getAllPits<TContext = unknown>(
752
+ params: T.PointInTimeGetAllRequest,
753
+ options: TransportRequestOptions,
754
+ callback: callbackFn<T.PointInTimeGetAllResponse, TContext>
755
+ ): TransportRequestCallback;
692
756
  getScript<TContext = unknown>(
693
757
  params: T.GetScriptRequest,
694
758
  options?: TransportRequestOptions
@@ -136,14 +136,17 @@ interface OpenSearchDashboardsClient {
136
136
  }
137
137
  count<TContext = unknown>(params?: T.CountRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.CountResponse, TContext>>
138
138
  create<TDocument = unknown, TContext = unknown>(params: T.CreateRequest<TDocument>, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.CreateResponse, TContext>>
139
+ createPit<TContext = unknown>(params?: T.PointInTimeCreateRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.PointInTimeCreateResponse, TContext>>
139
140
  danglingIndices: {
140
141
  deleteDanglingIndex<TContext = unknown>(params?: TODO, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<TODO, TContext>>
141
142
  importDanglingIndex<TContext = unknown>(params?: TODO, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<TODO, TContext>>
142
143
  listDanglingIndices<TContext = unknown>(params?: TODO, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<TODO, TContext>>
143
144
  }
144
145
  delete<TContext = unknown>(params: T.DeleteRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.DeleteResponse, TContext>>
146
+ deleteAllPits<TContext = unknown>(params: T.PointInTimeDeleteAllRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.PointInTimeDeleteAllResponse, TContext>>
145
147
  deleteByQuery<TContext = unknown>(params: T.DeleteByQueryRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.DeleteByQueryResponse, TContext>>
146
148
  deleteByQueryRethrottle<TContext = unknown>(params: T.DeleteByQueryRethrottleRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.DeleteByQueryRethrottleResponse, TContext>>
149
+ deletePit<TContext = unknown>(params: T.PointInTimeDeleteRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.PointInTimeDeleteResponse, TContext>>
147
150
  deleteScript<TContext = unknown>(params: T.DeleteScriptRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.DeleteScriptResponse, TContext>>
148
151
  exists<TContext = unknown>(params: T.ExistsRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.ExistsResponse, TContext>>
149
152
  existsSource<TContext = unknown>(params: T.ExistsSourceRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.ExistsSourceResponse, TContext>>
@@ -154,6 +157,7 @@ interface OpenSearchDashboardsClient {
154
157
  }
155
158
  fieldCaps<TContext = unknown>(params?: T.FieldCapsRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.FieldCapsResponse, TContext>>
156
159
  get<TDocument = unknown, TContext = unknown>(params: T.GetRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.GetResponse<TDocument>, TContext>>
160
+ getAllPits<TContext = unknown>(params: T.PointInTimeGetAllRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.PointInTimeGetAllResponse, TContext>>
157
161
  getScript<TContext = unknown>(params: T.GetScriptRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.GetScriptResponse, TContext>>
158
162
  getScriptContext<TContext = unknown>(params?: T.GetScriptContextRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.GetScriptContextResponse, TContext>>
159
163
  getScriptLanguages<TContext = unknown>(params?: T.GetScriptLanguagesRequest, options?: TransportRequestOptions): TransportRequestPromise<ApiResponse<T.GetScriptLanguagesResponse, TContext>>
@@ -523,6 +523,14 @@ export interface Create<T = RequestBody> extends Generic {
523
523
  body: T;
524
524
  }
525
525
 
526
+ export interface CreatePit extends Generic {
527
+ index: string | string[];
528
+ allow_partial_pit_creation?: boolean;
529
+ keep_alive: string;
530
+ preference?: string;
531
+ routing?: string | string[];
532
+ }
533
+
526
534
  export interface DanglingIndicesDeleteDanglingIndex extends Generic {
527
535
  index_uuid: string;
528
536
  accept_data_loss?: boolean;
@@ -560,6 +568,8 @@ export interface Delete extends Generic {
560
568
  version_type?: 'internal' | 'external' | 'external_gte' | 'force';
561
569
  }
562
570
 
571
+ export interface DeleteAllPits extends Generic { }
572
+
563
573
  export interface DeleteByQuery<T = RequestBody> extends Generic {
564
574
  index: string | string[];
565
575
  _source_exclude?: string | string[];
@@ -605,6 +615,10 @@ export interface DeleteByQueryRethrottle extends Generic {
605
615
  requests_per_second: number;
606
616
  }
607
617
 
618
+ export interface DeletePit<T = RequestBody> extends Generic {
619
+ body: T;
620
+ }
621
+
608
622
  export interface DeleteScript extends Generic {
609
623
  id: string;
610
624
  timeout?: string;
@@ -705,6 +719,8 @@ export interface Get extends Generic {
705
719
  version_type?: 'internal' | 'external' | 'external_gte' | 'force';
706
720
  }
707
721
 
722
+ export interface GetAllPits extends Generic { }
723
+
708
724
  export interface GetScript extends Generic {
709
725
  id: string;
710
726
  cluster_manager_timeout?: string;
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 {
@@ -9963,3 +9963,50 @@ export interface SpecUtilsCommonCatQueryParameters {
9963
9963
  s?: string[]
9964
9964
  v?: boolean
9965
9965
  }
9966
+
9967
+ export interface PointInTime {
9968
+ pit_id: string;
9969
+ creation_time: Time;
9970
+ keep_alive: Time
9971
+ }
9972
+
9973
+ export interface PointInTimeDelete {
9974
+ pit_id: string;
9975
+ successful: boolean
9976
+ }
9977
+
9978
+ export interface PointInTimeCreateRequest extends RequestBase {
9979
+ index: Indices;
9980
+ keep_alive: string;
9981
+ preference?: string;
9982
+ routing?: string;
9983
+ expand_wildcards?: ExpandWildcards;
9984
+ allow_partial_pit_creation?: boolean
9985
+ }
9986
+
9987
+ export interface PointInTimeCreateResponse extends ShardsOperationResponseBase {
9988
+ pit_id: string;
9989
+ creation_time: Time;
9990
+ }
9991
+
9992
+ export interface PointInTimeGetAllRequest extends RequestBase { }
9993
+
9994
+ export interface PointInTimeGetAllResponse {
9995
+ pits: PointInTime[];
9996
+ }
9997
+
9998
+ export interface PointInTimeDeleteAllRequest extends RequestBase { }
9999
+
10000
+ export interface PointInTimeDeleteAllResponse {
10001
+ pits: PointInTimeDelete[];
10002
+ }
10003
+
10004
+ export interface PointInTimeDeleteRequest extends RequestBase {
10005
+ body?: {
10006
+ pit_id: string[]
10007
+ };
10008
+ }
10009
+
10010
+ export interface PointInTimeDeleteResponse {
10011
+ pits: PointInTimeDelete[];
10012
+ }
@@ -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
+ ```