@opensearch-project/opensearch 2.4.0 → 2.6.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.
package/guides/bulk.md CHANGED
@@ -19,143 +19,103 @@ Next, create an index named `movies` and another named `books` with the default
19
19
  const movies = 'movies';
20
20
  const books = 'books';
21
21
 
22
- client.indices.create({ index: movies }).then(() => {
23
- console.log(`Index ${movies} created`);
24
- });
22
+ if ((await client.indices.exists({ index: movies })).body)
23
+ await client.indices.delete({ index: movies });
24
+ if ((await client.indices.exists({ index: books })).body)
25
+ await client.indices.delete({ index: books });
25
26
 
26
- client.indices.create({ index: books }).then(() => {
27
- console.log(`Index ${books} created`);
28
- });
27
+ await client.indices.create({ index: movies });
28
+ await client.indices.create({ index: books });
29
29
  ```
30
30
 
31
31
 
32
32
  ## Bulk API
33
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.
34
+ The `bulk` API action allows you to perform multiple document operations in a single request. The body of the request is an array of objects that contains the bulk operations and the target documents to index, create, update, or delete.
35
35
 
36
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
37
 
71
- ### Creating multiple documents
38
+ The following code create 3 documents in the `movies` index and one in the `books` index using the `bulk` API action:
72
39
 
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
40
 
75
41
  ```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
- });
42
+ client
43
+ .bulk({
44
+ index: movies, // the default index
45
+ body: [
46
+ { create: {} },
47
+ { title: 'Beauty and the Beast', year: 2030, director: 'Ella' },
48
+ { index : {} },
49
+ { title: 'Snow White', year: 2031, director: 'Jake' },
50
+ { create: {} },
51
+ { title: 'Cinderella', year: 2032, director: 'Clyde' },
52
+ { index: { _index: books } },
53
+ { title: 'The Lion King', year: 1994, author: 'John' },
54
+ ],
55
+ })
56
+ .then((response) => {
57
+ console.log(response.body.items);
58
+ });
87
59
  ```
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.
60
+ As you can see, each `create` operation comprises 2 objects: the first object contain the directive for the operation, and the second object is the document to be indexed. Notice that the first 3 operations do not specify the index name, so the `movies` index is used by default.
89
61
 
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.
62
+ ### Mixing Operations
103
63
 
64
+ Take a look at the code block below:
104
65
 
105
- ### Deleting multiple documents
106
66
  ```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
- });
67
+ client
68
+ .bulk({
69
+ index: movies, // The default index
70
+ body: [
71
+ { create: { _id: 1 } },
72
+ { title: 'Beauty and the Beast 1', year: 2050 },
73
+
74
+ { delete: { _id: 1 } },
75
+
76
+ { create: { _id: 2 } },
77
+ { title: 'Beauty and the Beast 2', year: 2051 },
78
+
79
+ { create: {} },
80
+ { title: 'Beauty and the Beast 2', year: 2051 },
81
+
82
+ { create: { _index: books } },
83
+ { title: '2012', year: 2012 },
84
+ ],
85
+ })
86
+ .then((response) => {
87
+ console.log(response.body.items);
88
+ });
116
89
  ```
117
90
 
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:
91
+ This single `bulk` request contains 5 operations:
92
+ - Creates a document with the ID `1` in the `movies` index.
93
+ - Deletes the document with the ID `1` in the `movies` index.
94
+ - Creates a document with the ID `2` in the `movies` index.
95
+ - Creates a document in the `movies` index (since `_id` is not specified, a new ID is generated automatically).
96
+ - Creates a document in the `books` index (since `movies` is the default index, you have to specify `books` for `_index`).
120
97
 
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
98
 
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.
99
+ ## Bulk Helper
137
100
 
138
- The following code shows how to look for errors in the response:
101
+ The OpenSearch JavaScript client provides a `bulk` helper that simplifies the process of creating a bulk request for operations of the same type. The following code block demonstrates how to use the `bulk` helper to create one document in the `movies` index for each item in the `docs` array:
139
102
 
140
103
  ```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
- }
104
+ const docs = [
105
+ { title: 'Beauty and the Beast 1', year: 2050 },
106
+ { title: 'Beauty and the Beast 2', year: 2051 },
107
+ ];
108
+
109
+ client.helpers
110
+ .bulk({
111
+ datasource: docs,
112
+ onDocument(_) {
113
+ return { index: { _index: movies } };
114
+ },
115
+ })
116
+ .then((result) => {
117
+ console.log(result);
155
118
  });
156
- }).catch((error) => {
157
- console.error(error);
158
- });
159
119
  ```
160
120
 
161
121
  ## Cleanup
@@ -163,4 +123,4 @@ To clean up the resources created in this guide, delete the `movies` and `books`
163
123
 
164
124
  ```javascript
165
125
  client.indices.delete({ index: [movies, books] });
166
- ```
126
+ ```
@@ -3,23 +3,13 @@ This guide covers OpenSearch JavaScript Client API actions for Index Lifecycle.
3
3
 
4
4
  ## Setup
5
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:
6
+ Let's create a client instance to access an OpenSearch cluster:
16
7
 
17
8
  ```javascript
18
9
  const { Client } = require('@opensearch-project/opensearch');
19
10
 
20
11
  const client = new Client({
21
- node: 'https://admin:admin@localhost:9200',
22
- ssl: { rejectUnauthorized: false }
12
+ node: 'http://localhost:9200',
23
13
  });
24
14
 
25
15
  client.info().then(response => {
@@ -139,9 +129,9 @@ client.indices.delete({index: 'movies'})
139
129
  We can also delete multiple indices at once:
140
130
 
141
131
  ```javascript
142
- client.indices.delete({index: ['movies', 'paintings', 'burner'], ignore: 404})
132
+ client.indices.delete({index: ['movies', 'paintings', 'burner'], ignore_unavailable: true,})
143
133
  ```
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.
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.
145
135
 
146
136
  ## Cleanup
147
137
 
package/guides/json.md ADDED
@@ -0,0 +1,53 @@
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
+ ```
@@ -0,0 +1,56 @@
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.index.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
+ ```
package/guides/search.md CHANGED
@@ -1,30 +1,10 @@
1
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.
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. To complement your exploration of the OpenSearch Search API and its parameters, a working sample is available in [`../samples/search.js`](../samples/search.js).
3
3
  # Setup
4
4
  Let's start by creating an index and adding some documents to it:
5
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
- },
6
+ const client = new Client({
7
+ node: 'http://localhost:9200',
28
8
  });
29
9
 
30
10
  await client.indices.create({index: 'movies'});
@@ -120,24 +100,27 @@ const page_1 = await client.search({
120
100
  size: 2,
121
101
  body: search_body
122
102
  });
103
+ const documents_1 = page_1.body.hits.hits;
123
104
 
124
105
  const page_2 = await client.search({
125
106
  index: 'movies',
126
107
  size: 2,
127
108
  body: {
128
109
  ...search_body,
129
- search_after: page_1[page_1.length - 1].sort
110
+ search_after: documents_1[documents_1.length - 1].sort
130
111
  }
131
112
  });
113
+ const documents_2 = page_2.body.hits.hits;
132
114
 
133
115
  const page_3 = await client.search({
134
116
  index: 'movies',
135
117
  size: 2,
136
118
  body: {
137
119
  ...search_body,
138
- search_after: page_2[page_2.length - 1].sort
120
+ search_after: documents_2[documents_2.length - 1].sort
139
121
  }
140
- })
122
+ });
123
+ const documents_3 = page_3.body.hits.hits;
141
124
  ```
142
125
  ### Pagination with scroll
143
126
  When retrieving large amounts of non-real-time data, you can use the `scroll` parameter to paginate through the search results.
@@ -182,25 +165,28 @@ const page_1 = await client.search({
182
165
  size: 2,
183
166
  body: pit_search_body
184
167
  });
185
- console.log(page_1.body.hits.hits);
168
+ const documents_1 = page_1.body.hits.hits;
169
+ console.log(documents_1);
186
170
 
187
171
  const page_2 = await client.search({
188
172
  size: 2,
189
173
  body: {
190
174
  ...pit_search_body,
191
- search_after: page_1[page_1.length - 1].sort
175
+ search_after: documents_1[documents_1.length - 1].sort
192
176
  }
193
177
  });
194
- console.log(page_2.body.hits.hits);
178
+ const documents_2 = page_2.body.hits.hits;
179
+ console.log(documents_2);
195
180
 
196
181
  const page_3 = await client.search({
197
182
  size: 2,
198
183
  body: {
199
184
  ...pit_search_body,
200
- search_after: page2[page_2.length-1].sort
185
+ search_after: documents_2[documents_2.length-1].sort
201
186
  }
202
187
  });
203
- console.log(page_3.body.hits.hits);
188
+ const documents_3 = page_3.body.hits.hits;
189
+ console.log(documents_3);
204
190
 
205
191
  /** Print out the titles of the first 3 pages of results */
206
192
  console.log(page_1.map(hit => hit._source.title));