@opensearch-project/opensearch 2.8.0 → 2.10.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,139 @@
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'));
@@ -0,0 +1,146 @@
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('../index.js')
12
+ const { Client } = require('@opensearch-project/opensearch');
13
+
14
+ const faker = require('faker');
15
+
16
+ // Instantiate a client with basic auth as setup in the README.md file
17
+ const client = new Client({
18
+ ssl: {
19
+ rejectUnauthorized: false,
20
+ },
21
+ node: 'https://localhost:9200',
22
+ auth: {
23
+ username: 'admin',
24
+ password: 'myStrongPassword123!',
25
+ },
26
+ });
27
+
28
+ const start = async () => {
29
+ const sourceIndex = 'sample-index';
30
+ const transformIndex = 'transform-index';
31
+ const transformJob = 'my-transform-job';
32
+
33
+ /////////////////// SETUP //////////////////
34
+ // Create source index
35
+ await client.indices.create({
36
+ index: sourceIndex,
37
+ body: {
38
+ mappings: {
39
+ properties: {
40
+ name: { type: 'keyword' },
41
+ age: { type: 'integer' },
42
+ gender: { type: 'keyword' },
43
+ salary: { type: 'integer' },
44
+ },
45
+ },
46
+ },
47
+ });
48
+
49
+ // Create 100 documents
50
+ for (let i = 0; i < 100; i++) {
51
+ // Generate random data for each field
52
+ const name = faker.name.findName(); // generates a random name
53
+ const age = faker.datatype.number({ min: 20, max: 60 }); // generates a random age between 20 and 60
54
+ const gender = faker.random.arrayElement(['Male', 'Female']); // randomly selects 'Male' or 'Female'
55
+ const salary = faker.datatype.number({ min: 30000, max: 100000 }); // generates a random salary between 30000 and 100000
56
+
57
+ // Index the document
58
+ await client.index({
59
+ index: sourceIndex,
60
+ body: {
61
+ name,
62
+ age,
63
+ gender,
64
+ salary,
65
+ },
66
+ });
67
+ }
68
+
69
+ // Refresh the index to make sure all documents are searchable
70
+ await client.indices.refresh({ index: sourceIndex });
71
+
72
+ //////////// TRANSFORM OPERATIONS ////////////
73
+ // Create transform
74
+ await client.transforms.put({
75
+ id: transformJob,
76
+ body: {
77
+ transform: {
78
+ enabled: true,
79
+ continuous: true,
80
+ schedule: {
81
+ interval: {
82
+ period: 1,
83
+ unit: 'Minutes',
84
+ start_time: 0
85
+ }
86
+ },
87
+ description: 'Sample transform job',
88
+ source_index: sourceIndex,
89
+ target_index: transformIndex,
90
+ data_selection_query: {
91
+ match_all: {}
92
+ },
93
+ page_size: 1,
94
+ groups: [
95
+ {
96
+ terms: {
97
+ source_field: 'gender',
98
+ target_field: 'gender'
99
+ }
100
+ },
101
+ {
102
+ terms: {
103
+ source_field: 'age',
104
+ target_field: 'age'
105
+ }
106
+ }
107
+ ],
108
+ aggregations: {
109
+ quantity: {
110
+ sum: {
111
+ field: 'salary'
112
+ }
113
+ }
114
+ }
115
+ }
116
+ },
117
+ });
118
+
119
+ // Start transform
120
+ await client.transforms.start({ id: transformJob });
121
+
122
+ // Get transform
123
+ const transform = await client.transforms.get({ id: transformJob });
124
+ console.log(transform.body);
125
+
126
+ // Wait till the transform index is created
127
+ while (!(await client.indices.exists({ index: transformIndex })).body) {
128
+ await new Promise((resolve) => setTimeout(resolve, 5000));
129
+ }
130
+
131
+ // Get transform status
132
+ const explain = await client.transforms.explain({ id: transformJob });
133
+ console.log(explain.body);
134
+
135
+ // Stop transform
136
+ await client.transforms.stop({ id: transformJob });
137
+
138
+ // Delete the transform job
139
+ await client.transforms.delete({ id: transformJob });
140
+
141
+ ///////////////// TEAR DOWN ///////////////
142
+ await client.indices.delete({ index: sourceIndex });
143
+ // await client.indices.delete({ index: transformIndex });
144
+ };
145
+
146
+ start().then(() => console.log('done'));