Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pratik-k2 committed Nov 7, 2023
1 parent 3665035 commit 9f91df0
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# node-elasticsearch
<!-- Use this command to run the elasticsearch container -->
docker run -p 9200:9200 -p 9300:9300 --name elasticsearch -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.14

<!-- Update the host where elasticsearch container is deployed in elasticsearch.js -->

<!-- Install the node modules -->

<!-- Run index.js -->

<!-- Run curls from curl.sh -->
24 changes: 24 additions & 0 deletions curls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#curl1
curl --location 'http://localhost:3000/elastic/index/init?index_name=test1'

#curl2
curl --location 'http://localhost:3000/elastic/index/check?index_name=test1'

#curl3
curl --location 'http://localhost:3000/elastic/index/mapping' --header 'Content-Type: application/json' --data '{"index_name": "test1","payload": {"properties": {"title": {"type": "text"},"description": {"type": "text"},"tags": {"type": "keyword"}}}}'

#curl4
curl --location 'http://localhost:3000/elastic/add' --header 'Content-Type: application/json' --data '{"index_name": "courses","id": 1234567,"type": "document","payload": {"title": "Digital marketing","tags": "taga, tag-1b, tag3, tag4","categories": ["Online","Marketing"],"price": "12000","level": "Beginner","badges": ["Popular","Normal"]}}'

#curl5
curl --location --request PUT 'http://localhost:3000/elastic/update' --header 'Content-Type: application/json' --data '{"index_name": "courses","id": 1234567,"type": "document", "payload": {"title": "Rangoon"}}'

#curl6
curl --location 'http://localhost:3000/elastic/search' --header 'Content-Type: application/json' --data '{"index_name": "courses","type": "document","payload": {"query": {"match": { "title": "Digital marketing" }}}}'

#curl7
curl --location --request DELETE 'http://localhost:3000/elastic/delete-document' --header 'Content-Type: application/json' --data '{"index_name": "courses","id": 1234567,"type": "document"}'

#curl8
curl --location --request DELETE 'http://localhost:3000/elastic/delete-index/test1'

160 changes: 160 additions & 0 deletions elasticsearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
var { Client } = require('@elastic/elasticsearch');
var elasticClient = new Client({
node: 'http://localhost:9200'
});

module.exports = {
ping: function (req, res) {
elasticClient.ping({
requestTimeout: 30000,
}, function (error) {
if (error) {
res.status(500)
return res.json({ status: false, msg: 'Elasticsearch cluster is down!' })
} else {
res.status(200);
return res.json({ status: true, msg: 'Success! Elasticsearch cluster is up!' })
}
});
},

// 1. Create index
initIndex: function (req, res, indexName) {

elasticClient.indices.create({
index: indexName
}).then(function (resp) {
// console.log(resp);
res.status(200)
return res.json(resp)
}, function (err) {
// console.log(err.message);
res.status(500)
return res.json(err)
});
},

// 2. Check if index exists
indexExists: function (req, res, indexName) {
elasticClient.indices.exists({
index: indexName
}).then(function (resp) {
// console.log(resp);
res.status(200);
return res.json(resp)
}, function (err) {
// console.log(err.message);
res.status(500)
return res.json(err)
});
},

// 3. Preparing index and its mapping
initMapping: function (req, res, indexName, docType, payload) {

elasticClient.indices.putMapping({
index: indexName,
type: "document",
include_type_name: true,
body: {
properties: payload
}
}).then(function (resp) {
res.status(200);
return res.json(resp)
}, function (err) {
res.status(500)
return res.json(err)
});
},

// 4. Add/Update a document
addDocument: function (req, res, indexName, _id, docType, payload) {
elasticClient.index({
index: indexName,
type: docType,
id: _id,
body: payload
}).then(function (resp) {
// console.log(resp);
res.status(200);
return res.json(resp)
}, function (err) {
// console.log(err.message);
res.status(500)
return res.json(err)
});
},



// 5. Update a document
updateDocument: async function(req, res, indexName, _id, docType, payload){
try {
const result = await elasticClient.update({
index: indexName,
type: docType,
id: _id,
doc: payload
})
res.status(200);
res.json(result);
} catch (e) {
res.status(500);
res.json(e.message);
}
},

// 6. Search
search: function (req, res, indexName, docType, payload) {
elasticClient.search({
index: indexName,
type: docType,
body: payload
}).then(function (resp) {
console.log(resp);
return res.json(resp)
}, function (err) {
console.log(err.message);
return res.json(err.message)
});
},


/*
* [xxxxxxxxxxxxxxxxx=----- DANGER AREA [RESTRICTED USE] -----=xxxxxxxxxxxxxxxxxxxxx]
*/

// Delete a document from an index
deleteDocument: async function (req, res, index, _id, docType) {
try {
const result = await elasticClient.delete({
index: index,
type: docType,
id: _id,
})
res.status(200);
res.json(result);
} catch (e) {
res.status(500);
res.json(e.message);
}
},

// Delete all
deleteIndex: async function (req, res, index) {
try {
const result = await elasticClient.indices.delete({
index
})
res.status(200);
res.json(result);
} catch (e) {
console.log(e)
res.status(500);
res.json(e.message);
}
},

// [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
};
69 changes: 69 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const cp = require("child_process");
const elasticsearchService = require('./elasticsearch');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get("/", (req, res) => {
elasticsearchService.ping(req, res);
});

app.get("/elastic/index/init", (req, res) => {
var index = req.query['index_name'];
elasticsearchService.initIndex(req, res, index);
});

app.get("/elastic/index/check", (req, res) => {
var index = req.query['index_name'];
elasticsearchService.indexExists(req, res, index);
});

app.post("/elastic/index/mapping", (req, res) => {
var payload = req.body.payload;
var index = req.body.index_name;
elasticsearchService.initMapping(req, res, index, payload);
});

app.post("/elastic/add", (req, res) => {
var payload = req.body.payload;
var index = req.body.index_name;
var id = req.body.id;
var type = req.body.type;
elasticsearchService.addDocument(req, res, index, id, type, payload);
});

app.put("/elastic/update", (req, res) => {
var payload = req.body.payload;
var index = req.body.index_name;
var id = req.body.id;
var type = req.body.type;
// console.log(req.body);
elasticsearchService.updateDocument(req, res, index, id, type, payload);
});

app.post("/elastic/search", (req, res) => {
var payload = req.body.payload;
var index = req.body.index_name;
var type = req.body.type;
elasticsearchService.search(req, res, index, type, payload);
});

app.delete("/elastic/delete-document", (req, res) => {
var index = req.body.index_name;
var id = req.body.id;
var type = req.body.type;
elasticsearchService.deleteDocument(req, res, index, id, type);
});

app.delete("/elastic/delete-index/:index", (req, res) => {
var index = req.params.index;
elasticsearchService.deleteIndex(req, res, index);
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("Server running at port %d", PORT);
});
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "node-elasticsearch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@elastic/elasticsearch": "^8.10.0",
"express": "^4.18.2"
}
}

0 comments on commit 9f91df0

Please sign in to comment.