forked from opensearch-project/opensearch-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msearch.js
130 lines (114 loc) · 3.18 KB
/
msearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/
const { Client } = require('@opensearch-project/opensearch');
const client = new Client({
ssl: {
rejectUnauthorized: false,
},
node: 'https://localhost:9200',
auth: {
username: 'admin',
password: 'admin',
},
});
const printResponse = (title, body) => {
console.log(`\n${title}:`);
console.log(body);
};
const handleErrorResponse = (error) => {
if (error.meta && error.meta.body) {
console.error('Error:', error.meta.body.error);
} else {
console.error('Error:', error.message);
}
};
const refreshIndex = async (indexName) => {
try {
const response = await client.indices.refresh({
index: indexName,
});
printResponse(`Refresh response for index ${indexName}`, response.body);
} catch (error) {
handleErrorResponse(error);
}
};
const deleteIndexIfExists = async (indexName) => {
const indexExistsResponse = await client.indices.exists({ index: indexName });
if (indexExistsResponse.statusCode === 200) {
const response = await client.indices.delete({ index: indexName });
printResponse(`Delete existing \`${indexName}\` Index`, response.body);
}
};
const createMoviesIndex = async () => {
const response = await client.indices.create({
index: 'movies',
body: {
mappings: {
properties: {
title: { type: 'text' },
director: { type: 'text' },
year: { type: 'integer' },
},
},
},
});
printResponse('Create `movies` Index', response.body);
};
const addDocumentsToMoviesIndex = async () => {
const response = await client.bulk({
body: [
{ index: { _index: 'movies', _id: 1 } },
{ title: 'The Godfather', director: 'Francis Ford Coppola', year: 1972 },
{ index: { _index: 'movies', _id: 2 } },
{ title: 'The Godfather: Part II', director: 'Francis Ford Coppola', year: 1974 },
],
});
printResponse('Add documents to the `movies` Index using the Bulk API', response.body);
};
const multiSearchExample = async () => {
const queries = [
{},
{
query: { match: { title: 'The Godfather' } },
},
{},
{
query: { match: { title: 'Part II' } },
},
];
const multiSearchResponse = await client.msearch({
index: 'movies',
body: queries,
});
printResponse(
'MultiSearch Response',
multiSearchResponse.body.responses.map((res) => res.hits.hits.map((movie) => movie._source))
);
};
const deleteMoviesIndex = async () => {
const response = await client.indices.delete({ index: 'movies' });
printResponse('Delete `movies` Index', response.body);
};
const start = async () => {
try {
await deleteIndexIfExists('movies');
await createMoviesIndex();
await addDocumentsToMoviesIndex();
// Refresh the 'movies' index
await refreshIndex('movies');
// Multi-search example
await multiSearchExample();
// Delete the 'movies' index
await deleteMoviesIndex();
} catch (error) {
handleErrorResponse(error);
}
};
start();