Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Last active November 20, 2015 16:20
Show Gist options
  • Select an option

  • Save hectorgool/35136cc527f1c9805850 to your computer and use it in GitHub Desktop.

Select an option

Save hectorgool/35136cc527f1c9805850 to your computer and use it in GitHub Desktop.
'use strict';
/*
npm install --save elasticsearch
create document
https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference-2-1.html
*/
module.exports = function() {
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
var INDEX = 'mx';
var TYPE = 'postal_code';
return {
createDocument: function () {
client.create({
index: INDEX,
type: TYPE,
id: '999999999',
body: {
cp: 208,
colonia: 'xxx',
ciudad: 'yyy',
delegacion: 'zzz',
location: {
lat: 22.0074,
lon: -102.2837
}
}
}, function(err, response) {
if (err) {
console.trace(err.message);
}
return response;
});
},
searchDocument: function (term, callback) {
var params = {
index: INDEX,
type: TYPE,
body: {
'size': 10,
'query': {
'match': {
'_all': {
'query': term,
'operator': 'and'
}
}
},
'sort' : [
{ 'colonia' : { 'order' : 'asc', 'mode' : 'avg' } }
]
}
};
client.search(params).then(function (resp) {
return callback(resp.hits.hits);
}, function (err) {
console.trace(err.message);
});
},
ping: function () {
var params = {
requestTimeout: Infinity, // ping usually has a 3000ms timeout
hello: 'elasticsearch!' // undocumented params are appended to the query string
};
client.ping(params, function (error) {
if (error) {
console.trace('elasticsearch cluster is down!');
}
return ('elasticsearch cluster is up!');
});
},
deleteDocument: function (id) {
client.delete({
index: INDEX,
type: TYPE,
id: id
}, function(err, response) {
if (err) {
console.trace(err.message);
}
return response;
});
},
updateDocument: function(id){
var doc = {
cp : 208,
colonia : 'zzzzz',
ciudad : 'aaaaa',
delegacion : 'ccccc',
location: {
lat: 22.0074,
lon: -102.2837
}
};
var param = {
index: INDEX,
type: TYPE,
id : id,
body: {
doc: doc
}
};
client.update( param, function( err,response ) {
if (err) {
console.trace(err.message);
}
return response;
});
},
existsDocument: function(id){
client.exists({
index: INDEX,
type: TYPE,
id: id
}, function (err, exists) {
if (exists === true) {
console.log('exists: ' + exists );
return exists;
} else {
console.log('not found: ' + id );
}
});
}
}//return
};
//curl -v localhost:3000
var es = require('./elasticsearch')();
var express = require('express');
var app = express();
app.get('/', function(req, res, next) {
res.status(200).json({ hello: 'world'});
});
app.get('/users/:id', function(req, res, next) {
res.send(req.params.id);
console.log(req.params.id);
});
app.get('/search/:id', function(req, res, next) {
es.searchDocument(req.params.id, function(data){
res.status(200).json( {data:data} );
});
});
app.listen(3000);
console.log('Express started on port 3000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment