@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.
package/guides/search.md DELETED
@@ -1,203 +0,0 @@
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. 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
- # Setup
4
- Let's start by creating an index and adding some documents to it:
5
- ```javascript
6
- const client = new Client({
7
- node: 'http://localhost:9200',
8
- });
9
-
10
- await client.indices.create({index: 'movies'});
11
-
12
- for(let i = 0; i < 10; i++) {
13
- await client.index({
14
- index: 'movies',
15
- id: i,
16
- body: {
17
- title: `The Dark Knight ${i}`,
18
- director: 'Christopher Nolan',
19
- year: 2008 + i
20
- }
21
- });
22
- }
23
-
24
- await client.index({
25
- index: 'movies',
26
- body: {
27
- title: 'The Godfather',
28
- director: 'Francis Ford Coppola',
29
- year: 1972
30
- }
31
- });
32
-
33
- await client.index({
34
- index: 'movies',
35
- body: {
36
- title: 'The Shawshank Redemption',
37
- director: 'Frank Darabont',
38
- year: 1994
39
- }
40
- });
41
-
42
- await client.index({index: 'movies', refresh: true});
43
- ```
44
- # Search API
45
- ### Basic Search
46
- The search API allows you to search for documents in an index. The following example searches for ALL documents in the movies index:
47
-
48
- ```javascript
49
- const response = await client.search({
50
- index: 'movies'
51
- });
52
- ```
53
- You can also search for documents that match a specific query. The following example searches for documents that match the query `dark knight`:
54
- ```javascript
55
- const response = await client.search({
56
- index: 'movies',
57
- body: {
58
- query: {
59
- match: {
60
- title: 'dark knight'
61
- }
62
- }
63
- }
64
- });
65
- console.log(response.body.hits.hits);
66
- ```
67
-
68
- OpenSearch query DSL allows you to specify complex queries. Check out the [OpenSearch query DSL documentation](https://opensearch.org/docs/latest/query-dsl/) for more information.
69
-
70
- ### Basic Pagination
71
- The search API allows you to paginate through the search results. The following example searches for documents that match the query `dark knight`, sorted by `year` in ascending order, and returns the first 2 results after skipping the first 5 results:
72
- ```javascript
73
- search_body = {
74
- query: {
75
- match: {
76
- title: 'dark knight'
77
- }
78
- },
79
- sort: [
80
- {
81
- year: {
82
- order: 'asc'
83
- }
84
- }
85
- ]
86
- }
87
-
88
- await client.search({
89
- index: 'movies',
90
- size: 2,
91
- from: 5,
92
- body: search_body
93
- });
94
- ```
95
-
96
- With sorting, you can also use the `search_after` parameter to paginate through the search results. Let's say you have already displayed the first page of results, and you want to display the next page. You can use the `search_after` parameter to paginate through the search results. The following example will demonstrate how to get the first 3 pages of results using the search query of the previous example:
97
- ```javascript
98
- const page_1 = await client.search({
99
- index: 'movies',
100
- size: 2,
101
- body: search_body
102
- });
103
- const documents_1 = page_1.body.hits.hits;
104
-
105
- const page_2 = await client.search({
106
- index: 'movies',
107
- size: 2,
108
- body: {
109
- ...search_body,
110
- search_after: documents_1[documents_1.length - 1].sort
111
- }
112
- });
113
- const documents_2 = page_2.body.hits.hits;
114
-
115
- const page_3 = await client.search({
116
- index: 'movies',
117
- size: 2,
118
- body: {
119
- ...search_body,
120
- search_after: documents_2[documents_2.length - 1].sort
121
- }
122
- });
123
- const documents_3 = page_3.body.hits.hits;
124
- ```
125
- ### Pagination with scroll
126
- When retrieving large amounts of non-real-time data, you can use the `scroll` parameter to paginate through the search results.
127
- ```javascript
128
- const page_1 = await client.search({
129
- index: 'movies',
130
- scroll: '1m',
131
- size: 2,
132
- body: search_body
133
- });
134
-
135
- const page_2 = await client.scroll({
136
- scroll_id: page_1._scroll_id,
137
- scroll: '1m'
138
- });
139
-
140
- const page_3 = await client.scroll({
141
- scroll_id: page_2._scroll_id,
142
- scroll: '1m'
143
- });
144
- ```
145
- ### Pagination with point in time
146
- The scroll example above has one weakness: if the index is updated while you are scrolling through the results, they will be paginated inconsistently. To avoid this, you should use the "Point in Time" feature. The following example demonstrates how to use the `point_in_time` and `pit_id` parameters to paginate through the search results:
147
- ```javascript
148
- /** Create a point in time */
149
- const pit = client.create_pit({
150
- index: 'movies',
151
- keep_alive: '1m'
152
- });
153
-
154
- /** Include pit info in the search body */
155
- const pit_search_body = {
156
- ..search_body,
157
- pit: {
158
- id: pit.pit_id,
159
- keep_alive: '1m'
160
- }
161
- }
162
-
163
- /** Get the first 3 pages of results */
164
- const page_1 = await client.search({
165
- size: 2,
166
- body: pit_search_body
167
- });
168
- const documents_1 = page_1.body.hits.hits;
169
- console.log(documents_1);
170
-
171
- const page_2 = await client.search({
172
- size: 2,
173
- body: {
174
- ...pit_search_body,
175
- search_after: documents_1[documents_1.length - 1].sort
176
- }
177
- });
178
- const documents_2 = page_2.body.hits.hits;
179
- console.log(documents_2);
180
-
181
- const page_3 = await client.search({
182
- size: 2,
183
- body: {
184
- ...pit_search_body,
185
- search_after: documents_2[documents_2.length-1].sort
186
- }
187
- });
188
- const documents_3 = page_3.body.hits.hits;
189
- console.log(documents_3);
190
-
191
- /** Print out the titles of the first 3 pages of results */
192
- console.log(page_1.map(hit => hit._source.title));
193
- console.log(page_2.map(hit => hit._source.title));
194
- console.log(page_3.map(hit => hit._source.title));
195
-
196
- /** Delete the point in time */
197
- client.delete_pit({body: {pit_id: pit.pit_id}});
198
- ```
199
- Note that a point-in-time is associated with an index or a set of index. So, when performing a search with a point-in-time, you DO NOT specify the index in the search.
200
- # Cleanup
201
- ```javascript
202
- await client.indices.delete({index: 'movies'});
203
- ```
@@ -1,11 +0,0 @@
1
- lib = library(identifier: 'jenkins@1.1.1', retriever: modernSCM([
2
- $class: 'GitSCMSource',
3
- remote: 'https://github.com/opensearch-project/opensearch-build-libraries.git',
4
- ]))
5
-
6
- standardReleasePipelineWithGenericTrigger(
7
- tokenIdCredential: 'jenkins-opensearch-js-generic-webhook-token',
8
- causeString: 'A tag was cut on opensearch-project/opensearch-js repository causing this workflow to run',
9
- publishRelease: true) {
10
- publishToNpm(repository: 'https://github.com/opensearch-project/opensearch-js', tag: "$tag")
11
- }
package/samples/README.md DELETED
@@ -1,17 +0,0 @@
1
- # OpenSearch JavaScript Samples
2
-
3
- The following will set up a single node OpenSearch instance with the default admin password `myStrongPassword123!`, which we will use to instantiate a client in all samples.
4
-
5
- ```shell
6
- docker pull opensearchproject/opensearch:latest
7
- docker run -d -p 9200:9200 -p 9600:9600 \
8
- -e 'discovery.type=single-node' -e 'OPENSEARCH_INITIAL_ADMIN_PASSWORD=myStrongPassword123!' \
9
- opensearchproject/opensearch:latest
10
- ```
11
-
12
- ## Run Samples
13
-
14
- To run the `json.js` sample:
15
- ```shell
16
- node json.js
17
- ```
package/samples/bulk.js DELETED
@@ -1,91 +0,0 @@
1
- /*
2
- * Copyright OpenSearch Contributors
3
- * SPDX-License-Identifier: Apache-2.0
4
- *
5
- * The OpenSearch Contributors require contributions made to
6
- * this file be licensed under the Apache-2.0 license or a
7
- * compatible open source license.
8
- *
9
- */
10
-
11
- // For more information on how to make custom HTTP requests, check out: ./guides/bulk.md
12
-
13
- const { Client } = require('@opensearch-project/opensearch');
14
-
15
- // Instantiate a client with basic auth as setup in the README.md file
16
- const client = new Client({
17
- ssl: {
18
- rejectUnauthorized: false,
19
- },
20
- node: 'https://localhost:9200',
21
- auth: {
22
- username: 'admin',
23
- password: 'myStrongPassword123!',
24
- },
25
- });
26
-
27
- const start = async () => {
28
- const movies = 'movies';
29
- const books = 'books';
30
-
31
- if ((await client.indices.exists({ index: movies })).body)
32
- await client.indices.delete({ index: movies });
33
- if ((await client.indices.exists({ index: books })).body)
34
- await client.indices.delete({ index: books });
35
-
36
- await client.indices.create({ index: movies });
37
- await client.indices.create({ index: books });
38
-
39
- client
40
- .bulk({
41
- index: movies,
42
- body: [
43
- { create: {} },
44
- { title: 'Beauty and the Beast', year: 2030, director: 'Ella' },
45
- { index: {} },
46
- { title: 'Snow White', year: 2031, director: 'Jake' },
47
- { create: {} },
48
- { title: 'Cinderella', year: 2032, director: 'Clyde' },
49
- ],
50
- })
51
- .then((response) => {
52
- console.log(response.body.items);
53
- });
54
-
55
- client
56
- .bulk({
57
- index: movies,
58
- body: [
59
- { create: { _id: 1 } },
60
- { title: 'Beauty and the Beast 1', year: 2050 },
61
- { delete: { _id: 1 } },
62
- { create: { _id: 2 } },
63
- { title: 'Beauty and the Beast 2', year: 2051 },
64
- { create: {} },
65
- { title: 'Beauty and the Beast 2', year: 2051 },
66
- { create: { _index: books } },
67
- { title: '2012', year: 2012 },
68
- ],
69
- })
70
- .then((response) => {
71
- console.log(response.body.items);
72
- });
73
-
74
- const docs = [
75
- { title: 'Beauty and the Beast 1', year: 2050 },
76
- { title: 'Beauty and the Beast 2', year: 2051 },
77
- ];
78
-
79
- client.helpers
80
- .bulk({
81
- datasource: docs,
82
- onDocument(_) {
83
- return { index: { _index: movies } };
84
- },
85
- })
86
- .then((result) => {
87
- console.log(result);
88
- });
89
- };
90
-
91
- start().then(() => console.log('done'));
package/samples/json.js DELETED
@@ -1,75 +0,0 @@
1
- /*
2
- * Copyright OpenSearch Contributors
3
- * SPDX-License-Identifier: Apache-2.0
4
- *
5
- * The OpenSearch Contributors require contributions made to
6
- * this file be licensed under the Apache-2.0 license or a
7
- * compatible open source license.
8
- *
9
- */
10
-
11
- // For more information on how to make custom HTTP requests, check out: ./guides/json.md
12
-
13
- const { Client } = require('@opensearch-project/opensearch');
14
-
15
- // Instantiate a client with basic auth as setup in the README.md file
16
- const client = new Client({
17
- ssl: {
18
- rejectUnauthorized: false,
19
- },
20
- node: 'https://localhost:9200',
21
- auth: {
22
- username: 'admin',
23
- password: 'myStrongPassword123!',
24
- },
25
- });
26
-
27
- let response;
28
-
29
- const print_response = (title) => {
30
- console.log(`\n${title}:`);
31
- console.log(response.body);
32
- };
33
-
34
- const start = async () => {
35
- // Check the cluster health
36
- response = await client.http.get({ path: '/_cluster/health' });
37
- print_response('Get Cluster Health');
38
-
39
- // Create the movies index
40
- response = await client.http.put({
41
- path: '/movies',
42
- body: {
43
- mappings: {
44
- properties: {
45
- title: { type: 'text' },
46
- director: { type: 'text' },
47
- year: { type: 'integer' },
48
- },
49
- },
50
- },
51
- });
52
- print_response('Create `movies` Index');
53
-
54
- // Make sure the index is created before indexing documents
55
- response = await client.http.head({ path: '/movies' });
56
- print_response('Check If Index Exists');
57
-
58
- // Add 2 documents to the index using the bulk endpoint
59
- response = await client.http.post({
60
- path: '/_bulk',
61
- body: [
62
- { index: { _index: 'movies' } },
63
- { title: 'The Godfather', director: 'Francis Ford Coppola', year: 1972 },
64
- { index: { _index: 'movies' } },
65
- { title: 'The Godfather: Part II', director: 'Francis Ford Coppola', year: 1974 },
66
- ],
67
- });
68
- print_response('Add 2 documents to the `movies` Index using the Bulk API');
69
-
70
- // Delete the movies index
71
- response = await client.http.delete({ path: '/movies' });
72
- print_response('Delete `movies` Index');
73
- };
74
-
75
- start();
@@ -1,131 +0,0 @@
1
- /*
2
- * Copyright OpenSearch Contributors
3
- * SPDX-License-Identifier: Apache-2.0
4
- *
5
- * The OpenSearch Contributors require contributions made to
6
- * this file be licensed under the Apache-2.0 license or a
7
- * compatible open source license.
8
- *
9
- */
10
- const { Client } = require('@opensearch-project/opensearch');
11
-
12
- // Instantiate a client with basic auth as setup in the README.md file
13
- const client = new Client({
14
- ssl: {
15
- rejectUnauthorized: false,
16
- },
17
- node: 'https://localhost:9200',
18
- auth: {
19
- username: 'admin',
20
- password: 'myStrongPassword123!',
21
- },
22
- });
23
-
24
- const printResponse = (title, body) => {
25
- console.log(`\n${title}:`);
26
- console.log(body);
27
- };
28
-
29
- const handleErrorResponse = (error) => {
30
- if (error.meta && error.meta.body) {
31
- console.error('Error:', error.meta.body.error);
32
- } else {
33
- console.error('Error:', error.message);
34
- }
35
- };
36
-
37
- const refreshIndex = async (indexName) => {
38
- try {
39
- const response = await client.indices.refresh({
40
- index: indexName,
41
- });
42
- printResponse(`Refresh response for index ${indexName}`, response.body);
43
- } catch (error) {
44
- handleErrorResponse(error);
45
- }
46
- };
47
-
48
- const deleteIndexIfExists = async (indexName) => {
49
- const indexExistsResponse = await client.indices.exists({ index: indexName });
50
- if (indexExistsResponse.statusCode === 200) {
51
- const response = await client.indices.delete({ index: indexName });
52
- printResponse(`Delete existing \`${indexName}\` Index`, response.body);
53
- }
54
- };
55
-
56
- const createMoviesIndex = async () => {
57
- const response = await client.indices.create({
58
- index: 'movies',
59
- body: {
60
- mappings: {
61
- properties: {
62
- title: { type: 'text' },
63
- director: { type: 'text' },
64
- year: { type: 'integer' },
65
- },
66
- },
67
- },
68
- });
69
- printResponse('Create `movies` Index', response.body);
70
- };
71
-
72
- const addDocumentsToMoviesIndex = async () => {
73
- const response = await client.bulk({
74
- body: [
75
- { index: { _index: 'movies', _id: 1 } },
76
- { title: 'The Godfather', director: 'Francis Ford Coppola', year: 1972 },
77
- { index: { _index: 'movies', _id: 2 } },
78
- { title: 'The Godfather: Part II', director: 'Francis Ford Coppola', year: 1974 },
79
- ],
80
- });
81
- printResponse('Add documents to the `movies` Index using the Bulk API', response.body);
82
- };
83
-
84
- const multiSearchExample = async () => {
85
- const queries = [
86
- {},
87
- {
88
- query: { match: { title: 'The Godfather' } },
89
- },
90
- {},
91
- {
92
- query: { match: { title: 'Part II' } },
93
- },
94
- ];
95
-
96
- const multiSearchResponse = await client.msearch({
97
- index: 'movies',
98
- body: queries,
99
- });
100
-
101
- printResponse(
102
- 'MultiSearch Response',
103
- multiSearchResponse.body.responses.map((res) => res.hits.hits.map((movie) => movie._source))
104
- );
105
- };
106
-
107
- const deleteMoviesIndex = async () => {
108
- const response = await client.indices.delete({ index: 'movies' });
109
- printResponse('Delete `movies` Index', response.body);
110
- };
111
-
112
- const start = async () => {
113
- try {
114
- await deleteIndexIfExists('movies');
115
- await createMoviesIndex();
116
- await addDocumentsToMoviesIndex();
117
-
118
- // Refresh the 'movies' index
119
- await refreshIndex('movies');
120
-
121
- // Multi-search example
122
- await multiSearchExample();
123
-
124
- // Delete the 'movies' index
125
- await deleteMoviesIndex();
126
- } catch (error) {
127
- handleErrorResponse(error);
128
- }
129
- };
130
-
131
- start();
@@ -1,139 +0,0 @@
1
- /*
2
- * Copyright OpenSearch Contributors
3
- * SPDX-License-Identifier: Apache-2.0
4
- *
5
- * The OpenSearch Contributors require contributions made to
6
- * this file be licensed under the Apache-2.0 license or a
7
- * compatible open source license.
8
- *
9
- */
10
-
11
- const { Client } = require('@opensearch-project/opensearch');
12
-
13
- // Instantiate a client with basic auth as setup in the README.md file
14
- const client = new Client({
15
- ssl: {
16
- rejectUnauthorized: false,
17
- },
18
- node: 'https://localhost:9200',
19
- auth: {
20
- username: 'admin',
21
- password: 'myStrongPassword123!',
22
- },
23
- });
24
-
25
- const start = async () => {
26
- const sourceIndex = 'sample-index';
27
- const rollupIndex = 'rollup-index';
28
- const jobId = 'my-rollup-job';
29
-
30
- /////////////////// SETUP //////////////////
31
- // Create source index
32
- await client.indices.create({
33
- index: sourceIndex,
34
- body: {
35
- mappings: {
36
- properties: {
37
- timestamp: { type: 'date' },
38
- field1: { type: 'keyword' },
39
- field2: { type: 'integer' },
40
- },
41
- },
42
- },
43
- });
44
-
45
- // Add some documents to the index
46
- const now = Date.now();
47
- for (let i = 0; i < 100; i++) {
48
- await client.index({
49
- index: sourceIndex,
50
- body: {
51
- timestamp: new Date(now - i * 3600000), // Subtract 1 hour for each document
52
- field1: `field1_value_${i % 10}`, // Cycle through 10 different values
53
- field2: i,
54
- },
55
- });
56
- }
57
-
58
- // Refresh the index to make sure all documents are searchable
59
- await client.indices.refresh({ index: sourceIndex });
60
- //////////// ROLLUP OPERATIONS ////////////
61
- // Create a rollup job
62
- await client.rollups.put({
63
- id: jobId,
64
- body: {
65
- rollup: {
66
- description: 'sample rollup',
67
- source_index: sourceIndex,
68
- target_index: rollupIndex,
69
- schedule: {
70
- interval: {
71
- period: 1,
72
- unit: 'Minutes',
73
- start_time: 1602100553,
74
- },
75
- },
76
- page_size: 10,
77
- delay: '0',
78
- continuous: false,
79
- dimensions: [
80
- {
81
- date_histogram: {
82
- source_field: 'timestamp',
83
- fixed_interval: '1440m',
84
- timezone: 'UTC',
85
- },
86
- },
87
- {
88
- terms: {
89
- source_field: 'field1',
90
- },
91
- },
92
- ],
93
- metrics: [
94
- {
95
- source_field: 'field2',
96
- metrics: [
97
- {
98
- avg: {},
99
- },
100
- {
101
- max: {},
102
- },
103
- ],
104
- },
105
- ],
106
- },
107
- },
108
- });
109
-
110
- // Start the rollup job
111
- await client.rollups.start({ id: jobId });
112
-
113
- // Check the rollup job
114
- const status = await client.rollups.get({ id: jobId });
115
- console.log('Running Rollup Job:');
116
- console.log(status.body);
117
-
118
- // Wait till the rollup index is created
119
- while (!(await client.indices.exists({ index: rollupIndex })).body) {
120
- await new Promise((resolve) => setTimeout(resolve, 5000));
121
- }
122
-
123
- // Explain the rollup job
124
- const explain = await client.rollups.explain({ id: jobId });
125
- console.log('Rollup Job Explanation:');
126
- console.log(explain.body);
127
-
128
- // Stop the rollup job
129
- await client.rollups.stop({ id: jobId });
130
-
131
- // Delete the rollup job
132
- await client.rollups.delete({ id: jobId });
133
-
134
- ///////////////// TEAR DOWN ///////////////
135
- await client.indices.delete({ index: sourceIndex });
136
- await client.indices.delete({ index: rollupIndex });
137
- };
138
-
139
- start().then(() => console.log('done'));