Last active
April 17, 2019 05:03
-
-
Save SiddharthaChowdhury/d07cf2a79019d8ea97b2f1713a64660d to your computer and use it in GitHub Desktop.
Elasticsearch CRUD implementation
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
{ | |
"id": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"name": "ELASTIC SEARCH API temp", | |
"description": "", | |
"order": [ | |
"b37eb178-4eac-e356-a391-41d7087d3565", | |
"ff953532-addd-52ec-d040-8444486d2532", | |
"b9159693-f81d-1fc9-80d2-e11feb9184f0", | |
"a4398b3c-a076-d7d6-0646-a52f75f690bb", | |
"70dbadea-a78d-752f-2b3c-2fa1dd607872", | |
"640fbd36-3c3a-dcda-793e-15579d4e16b3", | |
"f98db2e6-d421-f1c0-4a3c-fa308098e3bf", | |
"e41922f3-efe2-bb3f-54c9-71de2479a35a", | |
"cff7091b-db76-6447-41b4-ec591df75952", | |
"cd433fc7-fa74-007a-edf5-6eaeb31f064e" | |
], | |
"folders": [], | |
"timestamp": 1482909145213, | |
"owner": "121591", | |
"public": false, | |
"requests": [ | |
{ | |
"id": "640fbd36-3c3a-dcda-793e-15579d4e16b3", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/search", | |
"preRequestScript": null, | |
"pathVariables": {}, | |
"method": "POST", | |
"data": [], | |
"dataMode": "raw", | |
"version": 2, | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"time": 1485334273400, | |
"name": "[5] Search index for a value", | |
"description": "**Explaination**\n\nThe actual search query starts from the `payload` field of request parameter.\n\n> The Query does the following in respective order\n\n1. `from`,`size`: Total no of results epected *\"from\"* index (useful for pagination) \n2. `query`: The actual query string containing all conditions and logic implemented\n3. `bool`: It is used to check condition. In case of finding course we need to check if a particular course is **published**\n4. `bool.filter`: Following *\"term\"* must be matched\n5. `bool.must`: Following condition must be satisfied\n6. **`multi_match.query`**: **THE search string**\n7. `multi_match.fields`: Set of fields to be searched from each documents. `field^nummber` is priority, bigger the number - higher the priority of search.\n8. `multi_match.operator`: Can be *\"and\"* or *\"or\"* ", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"responses": [], | |
"rawModeData": "{\n\t\"index_name\": \"courses\",\n\t\"type\": \"coursemeta\",\n\t\"payload\":{\n\t\t\"from\":0,\n\t\t\"size\":10000,\n\t\t\"query\": {\n\t\t\t\"bool\": {\n\t\t\t\t\"must\": {\n\t\t\t\t\t \"multi_match\" : {\n\t\t\t\t \"query\" : \"marketing\",\n\t\t\t\t \"fields\" : [ \"title^5\", \"tags^4\", \"categories.name^3\" ],\n\t\t\t\t \"operator\": \"and\"\n\t\t\t\t }\n\t\t\t\t},\n\t\t\t\t\"filter\":{\n\t\t\t\t\t \"term\": {\"status\": \"published\"}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}" | |
}, | |
{ | |
"id": "70dbadea-a78d-752f-2b3c-2fa1dd607872", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/add", | |
"preRequestScript": null, | |
"pathVariables": {}, | |
"method": "POST", | |
"data": [], | |
"dataMode": "raw", | |
"version": 2, | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"time": 1485239231532, | |
"name": "[4] Add data to index", | |
"description": "", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"responses": [], | |
"rawModeData": "{\n\t\"index_name\":\"courses\",\n\t\"_id\":1234567,\n\t\"type\": \"coursemeta\",\n\t\"payload\":{\n\t\t\"title\": \"Digital marketing\",\n\t \"tags\": \"taga, tag-1b, tag3, tag4\",\n\t \"categories\":[\"Online\", \"Marketing\"],\n\t \"price\": \"12000\",\n\t \"level\": \"Beginner\",\n\t \"badges\":[\"Popular\", \"Normal\"]\n\t}\n}" | |
}, | |
{ | |
"id": "a4398b3c-a076-d7d6-0646-a52f75f690bb", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/index/mapping", | |
"preRequestScript": null, | |
"pathVariables": {}, | |
"method": "POST", | |
"data": [], | |
"dataMode": "raw", | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"time": 1484552102854, | |
"name": "[3] (optional) Map/Define created index", | |
"description": "Basically defining what are the data-attributes(+ datatypes) were going to add/save in this index", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"responses": [], | |
"rawModeData": "{\n\t\"index_name\":\"lorem_ipsum\",\n\t\"payload\": {\n\t\t\"properties\": {\n\t \"title\": {\"type\": \"string\"},\n\t \"description\": {\"type\": \"string\"},\n\t \"tags\": {\"type\": \"string\"},\n\t \"suggest\":{\n\t \t\"type\": \"completion\",\n\t \"analyzer\": \"simple\",\n\t \"search_analyzer\": \"simple\"\n\t }\n\t }\n\t}\n}\n" | |
}, | |
{ | |
"id": "b37eb178-4eac-e356-a391-41d7087d3565", | |
"headers": "", | |
"url": "localhost:1338/elastic/ping", | |
"pathVariables": {}, | |
"preRequestScript": null, | |
"method": "GET", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"data": null, | |
"dataMode": "params", | |
"name": "Ping Elastic search", | |
"description": "URL: **localhost:1338/elastic/ping**", | |
"descriptionFormat": "html", | |
"time": 1482909145213, | |
"version": 2, | |
"responses": [], | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {} | |
}, | |
{ | |
"id": "b9159693-f81d-1fc9-80d2-e11feb9184f0", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/index/check", | |
"preRequestScript": null, | |
"pathVariables": {}, | |
"method": "POST", | |
"data": [], | |
"dataMode": "raw", | |
"version": 2, | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"time": 1485151097968, | |
"name": "[2] Check if index exists", | |
"description": "", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"responses": [], | |
"rawModeData": "{\n\t\"index_name\":\"courses\"\n}" | |
}, | |
{ | |
"id": "cd433fc7-fa74-007a-edf5-6eaeb31f064e", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/delete-document", | |
"preRequestScript": null, | |
"pathVariables": {}, | |
"method": "DELETE", | |
"data": [], | |
"dataMode": "raw", | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"time": 1485239250021, | |
"name": "[Danger] Delete a document from an index", | |
"description": "URL: `localhost:1338/elastic/delete-document`", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"responses": [], | |
"rawModeData": "{\n\t\"index_name\":\"lorem_ipsum\",\n\t\"_id\":1234567,\n\t\"type\": \"coursemeta\"\n}" | |
}, | |
{ | |
"id": "cff7091b-db76-6447-41b4-ec591df75952", | |
"headers": "", | |
"url": "localhost:1338/elastic/delete_all", | |
"pathVariables": {}, | |
"preRequestScript": null, | |
"method": "GET", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"data": null, | |
"dataMode": "params", | |
"name": "[Danger] Delete all indexes created", | |
"description": "", | |
"descriptionFormat": "html", | |
"time": 1482919090248, | |
"version": 2, | |
"responses": [], | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {} | |
}, | |
{ | |
"id": "e41922f3-efe2-bb3f-54c9-71de2479a35a", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/search", | |
"pathVariables": {}, | |
"preRequestScript": null, | |
"method": "POST", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"data": [], | |
"dataMode": "raw", | |
"name": "[7] Get all documents in an index", | |
"description": "**localhost:1338/elastic/search**", | |
"descriptionFormat": "html", | |
"time": 1485256103389, | |
"version": 2, | |
"responses": [], | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"rawModeData": "{\n\t\"index_name\": \"courses\",\n\t\"type\": \"coursemeta\",\n\t\"payload\":{\n\t\t\"from\":0,\n\t\t\"size\":10000,\n\t\t\"query\": {\n\t\t\t\"match_all\": {}\n\t\t}\n\t}\n}" | |
}, | |
{ | |
"id": "f98db2e6-d421-f1c0-4a3c-fa308098e3bf", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/update", | |
"preRequestScript": null, | |
"pathVariables": {}, | |
"method": "PUT", | |
"data": [], | |
"dataMode": "raw", | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"time": 1485239240292, | |
"name": "[6] Update a document", | |
"description": "URL: \"localhost:1338/elastic/update\"\n\nIndex `payload` can contain:\n\n1. Either **partial** document, which will be merged with the existing document \n2. Or **script** which will update the document.\n\n**Refer ES 5.0 Docs**: *https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update*\n\n**Please note**\n\nWhen using **partial** the update should be put in `doc` key. Example: \"doc\":{\"title\": \"Something\"} as `payload` value of the request.\n\n\n\n", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"responses": [], | |
"rawModeData": "{\n\t\"index_name\":\"lorem_ipsum\",\n\t\"_id\":1234567,\n\t\"type\": \"coursemeta\",\n\t\"payload\":{\n\t\t\"doc\": {\n\t \"title\": \"Rangoon\"\n\t }\n\t}\n}" | |
}, | |
{ | |
"id": "ff953532-addd-52ec-d040-8444486d2532", | |
"headers": "Content-Type: application/json\n", | |
"url": "localhost:1338/elastic/index/init", | |
"preRequestScript": null, | |
"pathVariables": {}, | |
"method": "POST", | |
"data": [], | |
"dataMode": "raw", | |
"tests": null, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"time": 1485151101241, | |
"name": "[1] Create an Index", | |
"description": "must be in lowercase", | |
"collectionId": "c4c920d9-eca2-bcd7-6ad7-285556c577c9", | |
"responses": [], | |
"rawModeData": "{\n\t\"index_name\":\"courses\"\n}" | |
} | |
] | |
} |
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
var elasticsearch = require('elasticsearch'); | |
var elasticClient = new elasticsearch.Client({ | |
host: 'localhost:9200', | |
log: 'trace' | |
}); | |
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: docType, | |
body: 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: function(req, res, index, _id, docType, payload){ | |
elasticClient.update({ | |
index: index, | |
type: docType, | |
id: _id, | |
body: payload | |
}, function (err, resp) { | |
if(err) return res.json(err); | |
return res.json(resp); | |
}) | |
}, | |
// 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: function(req, res, index, _id, docType){ | |
elasticClient.delete({ | |
index: index, | |
type: docType, | |
id: _id, | |
}, function(err, resp) { | |
if (err) return res.json(err); | |
return res.json(resp); | |
}); | |
}, | |
// Delete all | |
deleteAll: function(req, res){ | |
elasticClient.indices.delete({ | |
index: '_all' | |
}, function(err, resp) { | |
if (err) { | |
console.error(err.message); | |
} else { | |
console.log('Indexes have been deleted!', resp); | |
return res.json(resp) | |
} | |
}); | |
}, | |
// [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] | |
}; |
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
/*============================================================================= | |
T E S T A P I s | |
===============================================================================*/ | |
'/elastic/ping': function(req, res){ | |
ELASTICSEARCH_Service.ping(req, res); | |
}, | |
'post /elastic/index/init': function(req, res){ | |
// [ 1 ] Create an index | |
var index = req.param('index_name'); | |
ELASTICSEARCH_Service.initIndex(req, res, index); | |
}, | |
'post /elastic/index/check': function(req, res){ | |
// [ 2 ] Check if Index exists | |
var index = req.param('index_name'); | |
ELASTICSEARCH_Service.indexExists(req, res, index); | |
}, | |
'post /elastic/index/mapping': function(req, res){ | |
// [ 3 ] Preparing index and its mapping (basically setting data-types of each attributes and more) | |
var payload = req.param('payload'); | |
var index = req.param('index_name'); | |
ELASTICSEARCH_Service.initMapping(req, res, index, payload); | |
return null; | |
}, | |
'post /elastic/add': function(req, res){ | |
// [ 4 ] Add data to index | |
var payload = req.param('payload'); | |
var index = req.param('index_name'); | |
var _id = req.param('_id'); | |
var docType = req.param('type'); | |
ELASTICSEARCH_Service.addDocument(req, res, index, _id, docType, payload); | |
return null; | |
}, | |
'put /elastic/update': function(req, res){ | |
// [ 5 ] Update a document | |
var payload = req.param('payload'); | |
var index = req.param('index_name'); | |
var _id = req.param('_id'); | |
var docType = req.param('type'); | |
ELASTICSEARCH_Service.updateDocument(req, res, index, _id, docType, payload); | |
return null; | |
}, | |
'post /elastic/search': function(req, res, next){ | |
// [ 6 ] Search an index | |
var index = req.param('index_name'); | |
var payload = req.param('payload'); | |
var docType = req.param('type'); | |
ELASTICSEARCH_Service.search(req, res, index, docType, payload); | |
}, | |
// -----------------------DANGER ZONE APIs------------------- | |
'delete /elastic/delete-document': function(req, res){ | |
// Delete a document | |
var index = req.param('index_name'); | |
var _id = req.param('_id'); | |
var docType = req.param('type'); | |
ELASTICSEARCH_Service.deleteDocument(req, res, index, _id, docType); | |
return null; | |
}, | |
'/elastic/delete_all': function(req, res){ | |
// Delete all indexes | |
ELASTICSEARCH_Service.deleteAll(req, res); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could not undersatand how will the routes.js will work.Since i am using loopback as my framework i want to integrate my project with elastic search,But i m not getting where should i place routes.js in my project.And do i need to require somethimg in routes.js?