@opensearch-project/opensearch 2.4.0 → 2.5.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,154 @@
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
+ const client = new Client({
13
+ ssl: {
14
+ rejectUnauthorized: false,
15
+ },
16
+ node: 'https://localhost:9200',
17
+ auth: {
18
+ username: 'admin',
19
+ password: 'admin',
20
+ },
21
+ });
22
+
23
+ const printResponse = (title, response) => {
24
+ console.log(`\n${title}:`);
25
+ console.log(response.body);
26
+ };
27
+
28
+ const refreshIndex = async (indexName) => {
29
+ try {
30
+ const response = await client.indices.refresh({
31
+ index: indexName,
32
+ });
33
+
34
+ printResponse(`Refresh response for index ${indexName}`, response);
35
+ } catch (error) {
36
+ handleErrorResponse(error);
37
+ }
38
+ };
39
+
40
+ const handleErrorResponse = (error) => {
41
+ if (error.meta && error.meta.body) {
42
+ console.error('Error:', error.meta.body.error);
43
+ } else {
44
+ console.error('Error:', error.message);
45
+ }
46
+ };
47
+
48
+ const createMoviesIndex = async () => {
49
+ try {
50
+ const response = await client.indices.create({
51
+ index: 'movies',
52
+ body: {
53
+ mappings: {
54
+ properties: {
55
+ title: { type: 'text' },
56
+ director: { type: 'text' },
57
+ year: { type: 'integer' },
58
+ },
59
+ },
60
+ },
61
+ });
62
+
63
+ printResponse('Create `movies` Index', response);
64
+ } catch (error) {
65
+ handleErrorResponse(error);
66
+ }
67
+ };
68
+
69
+ const indexMovies = async () => {
70
+ for (let i = 0; i < 10; i++) {
71
+ await client.index({
72
+ index: 'movies',
73
+ id: i,
74
+ body: {
75
+ title: `The Dark Knight ${i}`,
76
+ director: 'Christopher Nolan',
77
+ year: 2008 + i,
78
+ },
79
+ });
80
+ }
81
+
82
+ await client.index({
83
+ index: 'movies',
84
+ body: {
85
+ title: 'The Godfather',
86
+ director: 'Francis Ford Coppola',
87
+ year: 1972,
88
+ },
89
+ });
90
+
91
+ await client.index({
92
+ index: 'movies',
93
+ body: {
94
+ title: 'The Shawshank Redemption',
95
+ director: 'Frank Darabont',
96
+ year: 1994,
97
+ },
98
+ });
99
+ };
100
+
101
+ const searchMovies = async (query) => {
102
+ const response = await client.search({
103
+ index: 'movies',
104
+ body: query,
105
+ });
106
+
107
+ console.log('\nSearch Results:');
108
+ response.body.hits.hits.forEach((element) => {
109
+ console.log(element._source);
110
+ });
111
+ };
112
+
113
+ const start = async () => {
114
+ try {
115
+ // Check the cluster health
116
+ const clusterHealthResponse = await client.cluster.health({});
117
+ printResponse('Get Cluster Health', clusterHealthResponse);
118
+
119
+ // Check if the 'movies' index exists
120
+ const indexExistsResponse = await client.indices.exists({ index: 'movies' });
121
+ if (indexExistsResponse.statusCode === 200) {
122
+ // Delete the 'movies' index if it exists
123
+ const deleteIndexResponse = await client.indices.delete({ index: 'movies' });
124
+ printResponse('Delete existing `movies` Index', deleteIndexResponse);
125
+ }
126
+
127
+ await createMoviesIndex();
128
+ await indexMovies();
129
+
130
+ // Refresh the 'movies' index
131
+ await refreshIndex('movies');
132
+ console.log('\nRefreshed `movies` Index');
133
+
134
+ // List all movies using search
135
+ await searchMovies({});
136
+
137
+ // Search for 'dark knight'
138
+ await searchMovies({
139
+ query: {
140
+ match: {
141
+ title: 'dark knight',
142
+ },
143
+ },
144
+ });
145
+
146
+ // Delete the movies index
147
+ const deleteIndexResponse = await client.indices.delete({ index: 'movies' });
148
+ printResponse('Delete `movies` Index', deleteIndexResponse);
149
+ } catch (error) {
150
+ handleErrorResponse(error);
151
+ }
152
+ };
153
+
154
+ start();