-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
60 lines (50 loc) · 1.74 KB
/
index.ts
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
/*
* 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.
*
*/
import { defaultProvider } from "@aws-sdk/credential-provider-node" // V3 SDK.
import { Client } from '@opensearch-project/opensearch'
import { AwsSigv4Signer } from '@opensearch-project/opensearch/aws'
async function main() {
const client = new Client({
...AwsSigv4Signer({
region: process.env.AWS_REGION || 'us-east-1',
service: process.env.SERVICE as any || 'es',
getCredentials: () => {
const credentialsProvider = defaultProvider()
return credentialsProvider()
},
}),
node: process.env.ENDPOINT
})
// TODO: remove when OpenSearch Serverless re-adds GET /
if (process.env.SERVICE !== 'aoss') {
var info = await client.info()
var version = info.body.version
console.log(version.distribution + ": " + version.number)
}
// create an index
const index = 'movies'
await client.indices.create({ index: index })
try {
// index data
const document = { title: 'Moneyball', director: 'Bennett Miller', year: 2011 }
await client.index({ index: index, body: document, id: '1' })
// wait for the document to index
await new Promise(r => setTimeout(r, 1000))
// search for the document
var results = await client.search({ body: { query: { match: { director: 'miller' } } } })
results.body.hits.hits.forEach((hit) => console.log(hit._source))
// delete the document
await client.delete({ index: index, id: '1' })
} finally {
// delete the index
await client.indices.delete({ index: index })
}
}
main()