Skip to content

Instantly share code, notes, and snippets.

@AseedUsmani
Created March 31, 2018 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AseedUsmani/609618032a37e2edf064383b6ee671cc to your computer and use it in GitHub Desktop.
Save AseedUsmani/609618032a37e2edf064383b6ee671cc to your computer and use it in GitHub Desktop.
var elasticsearch = require('elasticsearch');
var url = require('url');
var http = require('http');
var fs = require('fs');
var client = new elasticsearch.Client({
host: 'localhost:9200',
});
client.ping({
// ping usually has a 3000ms timeout
requestTimeout: 1000
}, function (error) {
if (error) {
console.trace('elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});
http.createServer(function (req, res) {
var url_parts = url.parse(req.url, true);
var q = url_parts.query;
console.log(q.query);
var disease;
// res.write(JSON.stringify("Hello"));
// res.end();
var task=q.task;
if(task=='index') {
disease=q.disease;
url=q.url;
doc_id=q.doc_id;
data = {
'doc_id': doc_id,
"img_url": url,
"diseases": disease
}
client.index({
index: 'indexes',
type: 'reports',
body: data
}).then(response => {
console.log(response);
res.write(JSON.stringify(response));
res.end();
});
}
else if(task=='search') {
disease=q.disease;
var data = {
"query": {
"bool": {
"must": [
{ "match": { "diseases" : disease}}
]
}
},
"from" : 0,
"size" : 2,
"_source": true
}
client.search({
index: 'indexes',
type: 'reports',
body: data
}).then(function (resp) {
var hits = resp.hits.hits;
console.log(hits);
res.write(JSON.stringify(hits));
res.end();
//return hits;
}, function (err) {
console.trace(err.message);
});
}
else if(task == getImage) {
var url = q.url;
var imageAsBase64 = fs.readFileSync('E:\\SIH Project\\Image_classifier_django\\'+url, 'base64');
res.write(JSON.stringify(imageAsBase64));
res.end();
}
//res.end(JSON.stringify({ a: 1 }));
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment