Skip to content

Instantly share code, notes, and snippets.

@nodebotanist
Last active February 10, 2025 15:27
OpenSearch 102 Source Code and Sample Text
OPENSEARCH_HOST=''
OPENSEARCH_UNAME='icopensearch'
OPENSEARCH_PASS=''
'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)
}
}
// pull the dummy text into an array that we can pass to the OpenSearch cluster
let placeholderText = fs.readFileSync('./placeholder_text.txt').toString().split('\n')
const start = async () => {
try {
// Check if the 'opensearch-102' index exists
const indexExistsResponse = await client.indices.exists({ index: 'opensearch-102' })
if (indexExistsResponse.statusCode === 200) {
// Delete the 'opensearch-102' index if it exists
const deleteIndexResponse = await client.indices.delete({ index: 'opensearch-102' })
printResponse('Delete existing `opensearch-102` Index', deleteIndexResponse)
}
const indexText = async () => {
await (async () => {
for (let i = 0; i < placeholderText.length; i++) {
let response = await client.index({
index: 'opensearch-102',
id: i,
body: {
lineNumber: i+1,
text: placeholderText[i]
},
})
printResponse(`Added index ID ${i}:`, response)
}
})()
const searchText = async (query) => {
const response = await client.search({
index: 'opensearch-102',
body: query,
});
console.log('\nSearch Results:');
response.body.hits.hits.forEach((element) => {
console.log(element);
});
}
await searchText({
query: {
match: {
text: 'Programming is the Art of'
}
}
})
console.error('Error:', error.message)
}
}
start()
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. (Martin Golding) It would appear that we have reached the limits of what it is possible to achieve with computer technology, although one should be careful with such statements, as they tend to sound pretty silly in 5 years. (John Von Neumann, circa 1949) There are two major products that come out of Berkeley: LSD and UNIX. We don’t believe this to be a coincidence. (Jeremy S. Anderson) I think there’s a world market for about 5 computers. (Thomas J. Watson, Chairman of the Board, IBM, circa 1948) You can either have software quality or you can have pointer arithmetic, but you cannot have both at the same time. (Bertrand Meyer)
Complexity kills. It sucks the life out of developers, it makes products difficult to plan, build and test, it introduces security challenges, and it causes end-user and administrator frustration. (Ray Ozzie) Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. (Rick Cook) Every operating system out there is about equal… We all suck. (Microsoft senior vice president Brian Valentine describing the state of the art in OS security, 2003)
The use of COBOL cripples the mind; its teaching should therefore be regarded as a criminal offense. (E.W. Dijkstra) There are two ways to write error-free programs; only the third one works. (Alan J. Perlis) Without requirements or design, programming is the art of adding bugs to an empty text file. (Louis Srygley) It has been said that the great scientific disciplines are examples of giants standing on the shoulders of other giants. It has also been said that the software industry is an example of midgets standing on the toes of other midgets. (Alan Cooper)
Writing in C or C++ is like running a chain saw with all the safety guards removed. (Bob Gray) If debugging is the process of removing software bugs, then programming must be the process of putting them in. (Edsger Dijkstra) We are Microsoft. Resistance Is Futile. You Will Be Assimilated. (Bumper sticker) Ready, fire, aim: the fast approach to software development. Ready, aim, aim, aim, aim: the slow approach to software development. (Anonymous) Don’t worry if it doesn’t work right. If everything did, you’d be out of a job. (Mosher’s Law of Software Engineering)
There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup) Java is, in many ways, C++–. (Michael Feldman) First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack. (George Carrette) In C++ it’s harder to shoot yourself in the foot, but when you do, you blow off your whole leg. (Bjarne Stroustrup) The cheapest, fastest, and most reliable components are those that aren’t there. (Gordon Bell)
There are two major products that come out of Berkeley: LSD and UNIX. We don’t believe this to be a coincidence. (Jeremy S. Anderson) The bulk of all patents are crap. Spending time reading them is stupid. It’s up to the patent owner to do so, and to enforce them. (Linus Torvalds) First, solve the problem. Then, write the code. (John Johnson) Good programmers use their brains, but good guidelines save us having to think out every case. (Francis Glassborow)
You can’t have great software without a great team, and most software teams behave like dysfunctional families. (Jim McCarthy) In order to understand recursion, one must first understand recursion. (Anonymous) Just remember: you’re not a ‘dummy,’ no matter what those computer books claim. The real dummies are the people who–though technically expert–couldn’t design hardware and software that’s usable by normal consumers if their lives depended upon it. (Walter Mossberg) The best thing about a boolean is even if you are wrong, you are only off by a bit. (Anonymous) Good programmers use their brains, but good guidelines save us having to think out every case. (Francis Glassborow)
That’s the thing about people who think they hate computers. What they really hate is lousy programmers. (Larry Niven) Don’t worry if it doesn’t work right. If everything did, you’d be out of a job. (Mosher’s Law of Software Engineering) 19 Jan 2038 at 3:14:07 AM (End of the word according to Unix–2^32 seconds after January 1, 1970) The cheapest, fastest, and most reliable components are those that aren’t there. (Gordon Bell) Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves. (Alan Kay)
{
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment