-
-
Save nodebotanist/5cf0461e6b912c199e3d2b4d63d0c39e to your computer and use it in GitHub Desktop.
OpenSearch 101 by NetApp Instaclustr Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OPENSEARCH_HOST='' | |
OPENSEARCH_UNAME='icopensearch' | |
OPENSEARCH_PASS='' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
require('dotenv').config() | |
var host = process.env.OPENSEARCH_HOST | |
var protocol = 'https' | |
var port = 9200 | |
var auth = `${process.env.OPENSEARCH_UNAME}:${process.env.OPENSEARCH_PASS}` | |
// Create a client with SSL/TLS enabled. | |
var { Client } = require('@opensearch-project/opensearch') | |
var fs = require('fs') | |
var client = new Client({ | |
node: protocol + '://' + auth + '@' + host + ':' + port, | |
}) | |
const printResponse = (title, response) => { | |
console.log(`\n${title}:`) | |
console.log(response.body) | |
} | |
const handleErrorResponse = (error) => { | |
if (error.meta && error.meta.body) { | |
console.error('Error:', error.meta.body.error) | |
} else { | |
console.error('Error:', error.message) | |
} | |
} | |
const start = async () => { | |
try { | |
// Check the cluster health | |
const clusterHealthResponse = await client.cluster.health({}) | |
printResponse('Get Cluster Health', clusterHealthResponse) | |
// Check if the 'devrel_team' index exists | |
const indexExistsResponse = await client.indices.exists({ index: 'devrel_team' }) | |
if (indexExistsResponse.statusCode === 200) { | |
// Delete the 'devrel_team' index if it exists | |
const deleteIndexResponse = await client.indices.delete({ index: 'devrel_team' }) | |
printResponse('Delete existing `devrel_team` Index', deleteIndexResponse) | |
// Create the `devrel_team` index | |
const createIndexResponse = await client.indices.create({ | |
index: 'devrel_team', | |
body: { | |
mappings: { | |
properties: { | |
title: { type: 'text' }, | |
name: { type: 'text' }, | |
months_with_team: { type: 'integer' }, | |
}, | |
}, | |
}, | |
}) | |
printResponse('Create `devrel_team` Index', createIndexResponse) | |
const indexDevRelTeam = async () => { | |
const titles = [ | |
'dev advocate', | |
'dev advocate', | |
'dev advocate manager', | |
'senior dev advocate', | |
'dev advocate' | |
] | |
const names = [ | |
'Sam', | |
'Mark', | |
'Jane', | |
'Alex', | |
'John' | |
] | |
for (let i = 0; i < 5; i++) { | |
let response = await client.index({ | |
index: 'devrel_team', | |
id: i, | |
body: { | |
title: `${titles[i]}` | |
name: `${names[i]}`, | |
months_with_team: Math.floor(Math.random() * 24) | |
}, | |
}) | |
printResponse(`Added index ID ${i}:`, response) | |
} | |
} | |
await indexDevRelTeam() | |
} catch (error) { | |
handleErrorResponse(error) | |
} | |
} | |
start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "opensearch-101", | |
"version": "1.0.0", | |
"description": "Hello! If you're here, it's probably from TODO:link(this post). This is the source code for the demo.", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+https://github.com/nodebotanist/OpenSearch-101-demo.git" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"type": "commonjs", | |
"bugs": { | |
"url": "https://github.com/nodebotanist/OpenSearch-101-demo/issues" | |
}, | |
"homepage": "https://github.com/nodebotanist/OpenSearch-101-demo#readme", | |
"dependencies": { | |
"@opensearch-project/opensearch": "^3.1.0", | |
"dotenv": "^16.4.7" | |
} | |
} |
Comments are disabled for this gist.