@opensearch-project/opensearch 2.2.0 → 2.3.0

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.
@@ -0,0 +1,162 @@
1
+ # Document Lifecycle
2
+ This guide covers OpenSearch JavaScript Client API actions for Document Lifecycle. You'll learn how to create, read, update, and delete documents in your OpenSearch cluster. Whether you're new to OpenSearch or an experienced user, this guide provides the information you need to manage your document lifecycle effectively.
3
+
4
+ ## Setup
5
+ Assuming you have OpenSearch running locally on port 9200, you can create a client instance
6
+ with the following code:
7
+ ```javascript
8
+ const { Client } = require('@opensearch-project/opensearch');
9
+
10
+ const client = new Client({
11
+ node: 'http://localhost:9200',
12
+ });
13
+ ```
14
+ Next, create an index named `movies` with the default settings:
15
+ ```javascript
16
+ const index = 'movies';
17
+ client.indices.exists({ index: index })
18
+ .then(exists => {
19
+ if (!exists) {
20
+ return client.indices.create({ index: index });
21
+ }
22
+ })
23
+ .then(response => console.log(`Index ${response.body.index} created`));
24
+ ```
25
+
26
+ ## Document API Actions
27
+ ### Create a new document with specified ID
28
+ To create a new document, use the `create` or `index` API action. The following code creates two new documents with IDs of `1` and `2`:
29
+ ```javascript
30
+ client.create({index: index, id: 1, body: { title: 'Beauty and the Beast', year: 1991 }})
31
+ client.create({index: index, id: 2, body: { title: 'Beauty and the Beast - Live Action', year: 2017 }})
32
+ ```
33
+ Note that the `create` action is NOT idempotent. If you try to create a document with an ID that already exists, the request will fail:
34
+
35
+ ```javascript
36
+ client.create({ index: index, id: 1, body: { title: 'Just Another Movie' } })
37
+ .catch(err => console.log(`Error creating document: ${err}`));
38
+ ```
39
+
40
+ The `index` action, on the other hand, is idempotent. If you try to index a document with an existing ID, the request will succeed and overwrite the existing document. Note that no new document will be created in this case. You can think of the `index` action as an upsert:
41
+
42
+ ```javascript
43
+ client.index({index: index, id: 2, body: { title: 'Updated Title' }})
44
+ client.index({index: index, id: 2, body: { title: 'The Lion King', year: 1994 }})
45
+ ```
46
+
47
+ ### Create a new document with auto-generated ID
48
+ You can also create a new document with an auto-generated ID by omitting the `id` parameter. The following code creates documents with an auto-generated IDs in the `movies` index:
49
+ ```javascript
50
+ puts client.create({index: index, body: { title: 'The Lion King 2', year: 1998 }})
51
+ # OR client.index({index: index, body: { title: 'The Lion King 2', year: 1998 }})
52
+ ```
53
+ In this case, the ID of the created document in the `result` field of the response body:
54
+ ```javascript
55
+ {
56
+ "_index": "movies",
57
+ "_type": "_doc",
58
+ "_id": "1",
59
+ "_version": 1,
60
+ "result": "created",
61
+ "_shards": {
62
+ "total": 2,
63
+ "successful": 1,
64
+ "failed": 0
65
+ },
66
+ "_seq_no": 0,
67
+ "_primary_term": 1
68
+ }
69
+ ```
70
+
71
+ ### Get a document
72
+ To get a document, use the `get` API action. The following code gets the document with ID `1` from the `movies` index:
73
+ ```javascript
74
+ client.get({
75
+ index: index,
76
+ id: 1
77
+ }).then(response => console.log(response._source));
78
+ // OUTPUT: {"title": "Beauty and the Beast", "year": 1991}
79
+ ```
80
+ You can also use `_source_include` and `_source_exclude` parameters to specify which fields to include or exclude in the response:
81
+ ```javascript
82
+ client.get({
83
+ index: index,
84
+ id: 1,
85
+ _source_includes: ['title']
86
+ }).then(response => console.log(response._source));
87
+ // OUTPUT: {"title": "Beauty and the Beast"}
88
+ client.get({
89
+ index: index,
90
+ id: 1,
91
+ _source_excludes: ['title']
92
+ }).then(response => console.log(response._source));
93
+ // OUTPUT: {"year": 1991}
94
+ ```
95
+
96
+ ### Get multiple documents
97
+ To get multiple documents, use the `mget` API action:
98
+ ```javascript
99
+ client.mget({
100
+ index: index,
101
+ body: { docs: [{ _id: 1 }, { _id: 2 }] }
102
+ }).then(response => {
103
+ const sources = response.docs.map(doc => doc._source);
104
+ console.log(sources);
105
+ });
106
+ // OUTPUT: [ { title: 'Beauty and the Beast', year: 1991 }, { title: 'Beauty and the Beast - Live Action', year: 2017 } ]
107
+ ```
108
+
109
+ ### Check if a document exists
110
+ To check if a document exists, use the `exists` API action. The following code checks if the document with ID `1` exists in the `movies` index:
111
+ ```javascript
112
+ client.exists({
113
+ index: index,
114
+ id: 1
115
+ }).then(response => {
116
+ console.log(response.body);
117
+ });
118
+ ```
119
+
120
+ ### Update a document
121
+ To update a document, use the `update` API action. The following code updates the `year` field of the document with ID `1` in the `movies` index:
122
+ ```javascript
123
+ client.update({index: index, id: 1, body: { doc: { year: 1995 } }})
124
+ ```
125
+ Alternatively, you can use the `script` parameter to update a document using a script. The following code increments the `year` field of the of document with ID `1` by 5 using painless script, the default scripting language in OpenSearch:
126
+ ```javascript
127
+ client.update({index: index, id: 1, body: { script: { source: 'ctx._source.year += 5' } }})
128
+ ```
129
+ Note that while both `update` and `index` actions perform updates, they are not the same. The `update` action is a partial update, while the `index` action is a full update. The `update` action only updates the fields that are specified in the request body, while the `index` action overwrites the entire document with the new document.
130
+
131
+ ### Update multiple documents by query
132
+ To update documents that match a query, use the `update_by_query` API action. The following code decreases the `year` field of all documents with `year` greater than 2023:
133
+ ```javascript
134
+ client.update_by_query({index: index, body: {
135
+ script: { source: 'ctx._source.year -= 1' },
136
+ query: { range: { year: { gt: 2023 } } }
137
+ }})
138
+ ```
139
+
140
+ ### Delete a document
141
+ To delete a document, use the `delete` API action. The following code deletes the document with ID `1`:
142
+ ```javascript
143
+ client.delete({index: index, id: 1})
144
+ ```
145
+ By default, the `delete` action is not idempotent. If you try to delete a document that does not exist, or delete the same document twice, you will run into Not Found (404) error. You can make the `delete` action idempotent by setting the `ignore` parameter to `404`:
146
+ ```javascript
147
+ client.delete({index: index, id: 1, ignore: 404})
148
+ ```
149
+
150
+ ### Delete multiple documents by query
151
+ To delete documents that match a query, use the `delete_by_query` API action. The following code deletes all documents with `year` greater than 2023:
152
+ ```javascript
153
+ client.delete_by_query({index: index, body: {
154
+ query: { range: { year: { gt: 2023 } } }
155
+ }})
156
+ ```
157
+
158
+ ## Cleanup
159
+ To clean up the resources created in this guide, delete the `movies` index:
160
+ ```javascript
161
+ client.indices.delete({index: index})
162
+ ```
@@ -0,0 +1,152 @@
1
+ # Index Lifecycle
2
+ This guide covers OpenSearch JavaScript Client API actions for Index Lifecycle. You'll learn how to create, read, update, and delete indices in your OpenSearch cluster. We will also leverage index templates to create default settings and mappings for indices of certain patterns.
3
+
4
+ ## Setup
5
+
6
+ In this guide, we will need an OpenSearch cluster with more than one node. Let's use the sample [docker-compose.yml](https://opensearch.org/samples/docker-compose.yml) to start a cluster with two nodes. The cluster's API will be available at `localhost:9200` with basic authentication enabled with default username and password of `admin:admin`.
7
+
8
+ To start the cluster, run the following command:
9
+
10
+ ```bash
11
+ cd /path/to/docker-compose.yml
12
+ docker-compose up -d
13
+ ```
14
+
15
+ Let's create a client instance to access this cluster:
16
+
17
+ ```javascript
18
+ const { Client } = require('@opensearch-project/opensearch');
19
+
20
+ const client = new Client({
21
+ node: 'https://admin:admin@localhost:9200',
22
+ ssl: { rejectUnauthorized: false }
23
+ });
24
+
25
+ client.info().then(response => {
26
+ console.log(response.body);
27
+ }); // Check server info and make sure the client is connected
28
+ ```
29
+
30
+ ## Index API Actions
31
+
32
+ ### Create a new index
33
+ You can quickly create an index with default settings and mappings by using the `indices.create` API action. The following example creates an index named `paintings` with default settings and mappings:
34
+
35
+ ```javascript
36
+ client.indices.create({index: 'paintings'})
37
+ ```
38
+ To specify settings and mappings, you can pass them as the `body` of the request. The following example creates an index named `movies` with custom settings and mappings:
39
+
40
+ ```javascript
41
+ client.indices.create({
42
+ index: 'movies',
43
+ body: {
44
+ settings: {
45
+ index: {
46
+ number_of_shards: 2,
47
+ number_of_replicas: 1
48
+ }
49
+ },
50
+ mappings: {
51
+ properties: {
52
+ title: { type: 'text' },
53
+ year: { type: 'integer' }
54
+ }
55
+ }
56
+ }
57
+ })
58
+ ```
59
+ When you create a new document for an index, OpenSearch will automatically create the index if it doesn't exist:
60
+
61
+ ```javascript
62
+ client.indices.exists({ index: 'burner' }).then((exists) => {
63
+ console.log(exists); // => false
64
+ });
65
+ client.create({index: 'burner', body: { lorem: 'ipsum' }});
66
+ client.indices.exists({ index: 'burner' }).then((exists) => {
67
+ console.log(exists); // => true
68
+ });
69
+ ```
70
+
71
+
72
+ ### Update an Index
73
+ You can update an index's settings and mappings by using the `indices.put_settings` and `indices.put_mapping` API actions.
74
+
75
+ The following example updates the `movies` index's number of replicas to `0`:
76
+
77
+ ```javascript
78
+ client.indices.put_settings({
79
+ index: 'movies',
80
+ body: {
81
+ index: {
82
+ number_of_replicas: 0
83
+ }
84
+ }
85
+ })
86
+ ```
87
+ The following example updates the `movies` index's mappings to add a new field named `director`:
88
+
89
+ ```javascript
90
+ client.indices.put_mapping({
91
+ index: 'movies',
92
+ body: {
93
+ properties: {
94
+ director: { type: 'text' }
95
+ }
96
+ }
97
+ })
98
+ ```
99
+
100
+ ### Get Metadata for an Index
101
+ Let's check if the index's settings and mappings have been updated by using the `indices.get` API action:
102
+
103
+ ```javascript
104
+ client.indices.get({ index: 'movies' })
105
+ .then(response => console.log(response))
106
+ ```
107
+ The response body contains the index's settings and mappings:
108
+
109
+ ```javascript
110
+ {
111
+ "movies": {
112
+ "aliases": {},
113
+ "mappings": {
114
+ "properties": {
115
+ "title": { "type": "text" },
116
+ "year": { "type": "integer" },
117
+ "director": { "type": "text" }
118
+ }
119
+ },
120
+ "settings": {
121
+ "index": {
122
+ "creation_date": "1680297372024",
123
+ "number_of_shards": "2",
124
+ "number_of_replicas": "0",
125
+ "uuid": "FEDWXgmhSLyrCqWa8F_aiA",
126
+ "version": { "created": "136277827" },
127
+ "provided_name": "movies"
128
+ }
129
+ }
130
+ }
131
+ }
132
+ ```
133
+ ### Delete an Index
134
+ Let's delete the `movies` index by using the `indices.delete` API action:
135
+
136
+ ```javascript
137
+ client.indices.delete({index: 'movies'})
138
+ ```
139
+ We can also delete multiple indices at once:
140
+
141
+ ```javascript
142
+ client.indices.delete({index: ['movies', 'paintings', 'burner'], ignore: 404})
143
+ ```
144
+ Notice that we are passing `ignore: 404` to the request. This tells the client to ignore the `404` error if the index doesn't exist for deletion. Without it, the above `delete` request will throw an error because the `movies` index has already been deleted in the previous example.
145
+
146
+ ## Cleanup
147
+
148
+ All resources created in this guide are automatically deleted when the cluster is stopped. You can stop the cluster by running the following command:
149
+
150
+ ```bash
151
+ docker-compose down
152
+ ```
@@ -0,0 +1,228 @@
1
+ # Index Template
2
+ Index templates are a convenient way to define settings, mappings, and aliases for one or more indices when they are created. In this guide, you'll learn how to create an index template and apply it to an index.
3
+
4
+ ## Setup
5
+
6
+ Assuming you have OpenSearch running locally on port 9200, you can create a client instance
7
+ with the following code:
8
+ ```javascript
9
+ const { Client } = require('@opensearch-project/opensearch');
10
+
11
+ const client = new Client({
12
+ node: 'http://localhost:9200',
13
+ });
14
+ ```
15
+
16
+ ## Index Template API Actions
17
+
18
+ ### Create an Index Template
19
+ You can create an index template to define default settings and mappings for indices of certain patterns. The following example creates an index template named `books` with default settings and mappings for indices of the `books-*` pattern:
20
+
21
+ ```javascript
22
+ client.indices.put_index_template({
23
+ name: 'books',
24
+ body: {
25
+ index_patterns: ['books-*'],
26
+ settings: {
27
+ index: {
28
+ number_of_shards: 3,
29
+ number_of_replicas: 0
30
+ }
31
+ },
32
+ mappings: {
33
+ properties: {
34
+ title: { type: 'text' },
35
+ author: { type: 'text' },
36
+ published_on: { type: 'date' },
37
+ pages: { type: 'integer' }
38
+ }
39
+ }
40
+ }
41
+ }).then(resp => {
42
+ console.log(`Index template created: ${resp}`);
43
+ });
44
+ ```
45
+
46
+ Now, when you create an index that matches the `books-*` pattern, OpenSearch will automatically apply the template's settings and mappings to the index.
47
+ Let's create an index named `books-nonfiction` and verify that its settings and mappings match those of the template:
48
+
49
+ ```javascript
50
+ client.indices.create({ index: 'books-nonfiction' })
51
+ .then(() => {
52
+ return client.indices.get({ index: 'books-nonfiction' });
53
+ })
54
+ .then(response => {
55
+ console.log(response);
56
+ });
57
+ ```
58
+
59
+ ### Multiple Index Templates
60
+ If multiple index templates match the index's name, OpenSearch will apply the template with the highest priority. The following example creates two index templates named `books-*` and `books-fiction-*` with different settings:
61
+
62
+ ```javascript
63
+ client.indices.put_index_template({
64
+ name: 'books',
65
+ body: {
66
+ index_patterns: ['books-*'],
67
+ settings: {
68
+ index: {
69
+ number_of_shards: 3,
70
+ number_of_replicas: 0
71
+ }
72
+ }
73
+ }
74
+ })
75
+ .then(resp => {
76
+ console.log(`Index template created: ${resp}`);
77
+ });
78
+
79
+ client.indices.put_index_template({
80
+ name: 'books-fiction',
81
+ body: {
82
+ index_patterns: ['books-fiction-*'],
83
+ priority: 1, # higher priority than the `books` template
84
+ template: {
85
+ settings: {
86
+ index: {
87
+ number_of_shards: 1,
88
+ number_of_replicas: 1
89
+ }
90
+ }
91
+ }
92
+ }
93
+ })
94
+ .then(resp => {
95
+ console.log(`Index template created: ${resp}`);
96
+ });
97
+ ```
98
+
99
+ When we create an index named `books-fiction-romance`, OpenSearch will apply the `books-fiction-*` template's settings to the index:
100
+
101
+ ```javascript
102
+ client.indices.create({ index: 'books-fiction-romance' })
103
+ .then(() => {
104
+ return client.indices.get({ index: 'books-fiction-romance' });
105
+ })
106
+ .then(response => {
107
+ console.log(response);
108
+ });
109
+ ```
110
+
111
+ ### Composable Index Templates
112
+ Composable index templates are a new type of index template that allow you to define multiple component templates and compose them into a final template. The following example creates a component template named `books_mappings` with default mappings for indices of the `books-*` and `books-fiction-*` patterns:
113
+
114
+ ```javascript
115
+ client.cluster.put_component_template({
116
+ name: 'books_mappings',
117
+ body: {
118
+ template: {
119
+ mappings: {
120
+ properties: {
121
+ title: { type: 'text' },
122
+ author: { type: 'text' },
123
+ published_on: { type: 'date' },
124
+ pages: { type: 'integer' }
125
+ }
126
+ }
127
+ }
128
+ }
129
+ })
130
+ .then(resp => {
131
+ console.log(`Component template created: ${resp}`);
132
+ });
133
+
134
+ client.indices.put_index_template({
135
+ name: 'books',
136
+ body: {
137
+ index_patterns: ['books-*'],
138
+ composed_of: ['books_mappings'], # use the `books_mappings` component template
139
+ priority: 0,
140
+ template: {
141
+ settings: {
142
+ index: {
143
+ number_of_shards: 3,
144
+ number_of_replicas: 0
145
+ }
146
+ }
147
+ }
148
+ }
149
+ })
150
+ .then(resp => {
151
+ console.log(`Index template created: ${resp}`);
152
+ });
153
+
154
+ client.indices.put_index_template({
155
+ name: 'books',
156
+ body: {
157
+ index_patterns: ['books-*'],
158
+ composed_of: ['books_mappings'], # use the `books_mappings` component template
159
+ priority: 1,
160
+ template: {
161
+ settings: {
162
+ index: {
163
+ number_of_shards: 1,
164
+ number_of_replicas: 1
165
+ }
166
+ }
167
+ }
168
+ }
169
+ })
170
+ .then(resp => {
171
+ console.log(`Index template created: ${resp}`);
172
+ });
173
+ ```
174
+
175
+ When we create an index named `books-fiction-horror`, OpenSearch will apply the `books-fiction-*` template's settings, and `books_mappings` template mappings to the index:
176
+
177
+ ```javascript
178
+ client.indices.create({ index: 'books-fiction-horror' })
179
+ .then(() => {
180
+ return client.indices.get({ index: 'books-fiction-horror' });
181
+ })
182
+ .then(response => {
183
+ console.log(response);
184
+ });
185
+ ```
186
+
187
+ ### Get an Index Template
188
+ You can get an index template with the `get_index_template` API action:
189
+
190
+ ```javascript
191
+ client.indices.get_index_template({name: 'books'}).then(response => {
192
+ console.log(response.body);
193
+ });
194
+ ```
195
+
196
+ ### Delete an Index Template
197
+ You can delete an index template with the `delete_template` API action:
198
+
199
+ ```javascript
200
+ client.indices.delete_index_template({
201
+ name: 'books'
202
+ }).then((response) => {
203
+ console.log(`Index template deleted: ${response}`);
204
+ });
205
+ ```
206
+
207
+ ## Cleanup
208
+ Let's delete all resources created in this guide:
209
+
210
+ ```javascript
211
+ client.indices.delete({
212
+ index: 'books-*'
213
+ }).then(response => {
214
+ console.log(`Index deleted: ${response}`)
215
+ });
216
+
217
+ client.indices.delete_index_template({
218
+ name: 'books-fiction'
219
+ }).then(response => {
220
+ console.log(`Index template deleted: ${response}`)
221
+ });
222
+
223
+ client.cluster.delete_index_template({
224
+ name: 'books_mappings'
225
+ }).then(response => {
226
+ console.log(`Component template deleted: ${response}`)
227
+ });
228
+ ```