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