@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.
@@ -1,162 +0,0 @@
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
- ```
@@ -1,142 +0,0 @@
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
- Let's create a client instance to access an OpenSearch cluster:
7
-
8
- ```javascript
9
- const { Client } = require('@opensearch-project/opensearch');
10
-
11
- const client = new Client({
12
- node: 'http://localhost:9200',
13
- });
14
-
15
- client.info().then(response => {
16
- console.log(response.body);
17
- }); // Check server info and make sure the client is connected
18
- ```
19
-
20
- ## Index API Actions
21
-
22
- ### Create a new index
23
- 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:
24
-
25
- ```javascript
26
- client.indices.create({index: 'paintings'})
27
- ```
28
- 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:
29
-
30
- ```javascript
31
- client.indices.create({
32
- index: 'movies',
33
- body: {
34
- settings: {
35
- index: {
36
- number_of_shards: 2,
37
- number_of_replicas: 1
38
- }
39
- },
40
- mappings: {
41
- properties: {
42
- title: { type: 'text' },
43
- year: { type: 'integer' }
44
- }
45
- }
46
- }
47
- })
48
- ```
49
- When you create a new document for an index, OpenSearch will automatically create the index if it doesn't exist:
50
-
51
- ```javascript
52
- client.indices.exists({ index: 'burner' }).then((exists) => {
53
- console.log(exists); // => false
54
- });
55
- client.create({index: 'burner', body: { lorem: 'ipsum' }});
56
- client.indices.exists({ index: 'burner' }).then((exists) => {
57
- console.log(exists); // => true
58
- });
59
- ```
60
-
61
-
62
- ### Update an Index
63
- You can update an index's settings and mappings by using the `indices.put_settings` and `indices.put_mapping` API actions.
64
-
65
- The following example updates the `movies` index's number of replicas to `0`:
66
-
67
- ```javascript
68
- client.indices.put_settings({
69
- index: 'movies',
70
- body: {
71
- index: {
72
- number_of_replicas: 0
73
- }
74
- }
75
- })
76
- ```
77
- The following example updates the `movies` index's mappings to add a new field named `director`:
78
-
79
- ```javascript
80
- client.indices.put_mapping({
81
- index: 'movies',
82
- body: {
83
- properties: {
84
- director: { type: 'text' }
85
- }
86
- }
87
- })
88
- ```
89
-
90
- ### Get Metadata for an Index
91
- Let's check if the index's settings and mappings have been updated by using the `indices.get` API action:
92
-
93
- ```javascript
94
- client.indices.get({ index: 'movies' })
95
- .then(response => console.log(response))
96
- ```
97
- The response body contains the index's settings and mappings:
98
-
99
- ```javascript
100
- {
101
- "movies": {
102
- "aliases": {},
103
- "mappings": {
104
- "properties": {
105
- "title": { "type": "text" },
106
- "year": { "type": "integer" },
107
- "director": { "type": "text" }
108
- }
109
- },
110
- "settings": {
111
- "index": {
112
- "creation_date": "1680297372024",
113
- "number_of_shards": "2",
114
- "number_of_replicas": "0",
115
- "uuid": "FEDWXgmhSLyrCqWa8F_aiA",
116
- "version": { "created": "136277827" },
117
- "provided_name": "movies"
118
- }
119
- }
120
- }
121
- }
122
- ```
123
- ### Delete an Index
124
- Let's delete the `movies` index by using the `indices.delete` API action:
125
-
126
- ```javascript
127
- client.indices.delete({index: 'movies'})
128
- ```
129
- We can also delete multiple indices at once:
130
-
131
- ```javascript
132
- client.indices.delete({index: ['movies', 'paintings', 'burner'], ignore_unavailable: true,})
133
- ```
134
- Notice that we are passing `ignore_unavailable: true` to the request. This tells the client to ignore throwing error and deleting the index if it doesn't exist. Without it, the above `delete` request will throw an error because the `movies` index has already been deleted in the previous example.
135
-
136
- ## Cleanup
137
-
138
- All resources created in this guide are automatically deleted when the cluster is stopped. You can stop the cluster by running the following command:
139
-
140
- ```bash
141
- docker-compose down
142
- ```
@@ -1,228 +0,0 @@
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
- ```
package/guides/json.md DELETED
@@ -1,53 +0,0 @@
1
- # Making raw HTTP requests
2
-
3
- The OpenSearch client implements many high-level REST DSLs that invoke OpenSearch APIs. However, you may find yourself in a situation that requires you to invoke an API that is not supported by the client. In this case, you can use raw HTTP requests to invoke any OpenSearch API. This guide shows you different ways to make custom API calls using the OpenSearch JS client.
4
-
5
- ## Setup
6
- First, create a client instance with the following code to interact with an OpenSearch cluster with default security settings:
7
-
8
- ```javascript
9
- const client = new Client({
10
- node: 'http://localhost:9200',
11
- });
12
- ```
13
-
14
- ## The http Namespace
15
- The `http` namespace comes with all functions representing HTTP methods like `connect`, `delete`, `get`, `head`, `options`, `patch`, `post`, `put`, and `trace`.
16
-
17
- Let's create in index using `http.put`:
18
- ```javascript
19
- await client.http.put({
20
- path: '/movies',
21
- body: {
22
- mappings: {
23
- properties: {
24
- title: { type: 'text' },
25
- director: { type: 'text' },
26
- year: { type: 'integer' },
27
- },
28
- },
29
- },
30
- });
31
- ```
32
-
33
- Make sure that the index was created successfully using `http.head`:
34
-
35
- ```javascript
36
- await client.http.head({ path: '/movies' });
37
- ```
38
-
39
- Add two documents to the index using `http.post` and the bulk endpoint
40
- ```javascript
41
- const body = [
42
- { index: { _index: index } },
43
- { title: 'The quick brown fox' },
44
- { index: { _index: index } },
45
- { title: 'The quick brown fox jumps over the lazy dog' },
46
- ];
47
- await client.http.post({ path: `_bulk`, body });
48
- ```
49
-
50
- Delete the index using `http.delete`:
51
- ```javascript
52
- await client.http.delete({ path: '/movies' });
53
- ```
package/guides/msearch.md DELETED
@@ -1,56 +0,0 @@
1
- # Multi-Search (msearch)
2
-
3
- OpenSearch's Multi-Search (`msearch`) API allows you to execute multiple search operations for documents within an index efficiently. This API supports various parameters that provide flexibility in customizing your search operations. It's crucial to understand the key differences between `msearch` and a regular `search` operation in OpenSearch. Unlike a single search operation, `msearch` enables you to perform multiple searches in a single API call, significantly reducing network overhead as it need not initialize a new network connection for each search. This proves particularly advantageous for batch operations or scenarios where searches span multiple indices. By combining searches into one request, `msearch` streamlines the process and enhances overall efficiency. However, it's important to note that there will also be a bit more memory overhead in the `msearch`, as the coordinating node will have to accumulate the various search responses in memory before sending back the final response. Explore the functionalities of the msearch API with the provided sample code in [../samples/msearch.js](../samples/msearch.js).
4
-
5
- # Setup
6
-
7
- ```javascript
8
- const client = new Client({
9
- node: 'http://localhost:9200',
10
- });
11
-
12
- await client.bulk({
13
- body: [
14
- { index: { _index: 'movies', _id: 1 } },
15
- { title: 'The Godfather', director: 'Francis Ford Coppola', year: 1972 },
16
- { index: { _index: 'movies', _id: 2 } },
17
- { title: 'The Godfather: Part II', director: 'Francis Ford Coppola', year: 1974 },
18
- ],
19
- });
20
-
21
- await client.indices.refresh({ index: 'movies' });
22
- ```
23
-
24
- # Multi Search API
25
-
26
- The multi-search allows you to execute multiple search requests within a single API call. The following example shows how each search query provides a list of documents from the movie index.
27
-
28
- ```javascript
29
- try {
30
- const queries = [
31
- {},
32
- { query: { match: { title: 'The Godfather' } } },
33
- {},
34
- { query: { match: { title: 'Part II' } } },
35
- ];
36
-
37
- const multiSearchResponse = await client.msearch({
38
- index: 'movies',
39
- body: queries,
40
- });
41
-
42
- multiSearchResponse.body.responses.map((res) =>
43
- res.hits.hits.map((movie) => {
44
- console.log(movie._source);
45
- })
46
- );
47
- } catch (error) {
48
- console.error('An error occurred:', error);
49
- }
50
- ```
51
-
52
- # Cleanup
53
-
54
- ```javascript
55
- await client.indices.delete({index: 'movies'});
56
- ```