Skip to content

Instantly share code, notes, and snippets.

@wbashir
Created December 2, 2013 21:14
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 wbashir/7759139 to your computer and use it in GitHub Desktop.
Save wbashir/7759139 to your computer and use it in GitHub Desktop.
//Using node-elasticsearch-client and meteor-npm
var ElasticSearchClient = Meteor.require('elasticsearchclient'),
url = Npm.require('url');
//local elastic search envrionment running on port 9200
// create an _opts object for remote systems with credentials: See ElasticSearch documentation
var serverOptions = {
host: 'localhost',
port: 9200,
secure: false
}
var elasticSearchClient = new ElasticSearchClient(serverOptions);
//indexing the users collection by observing - observing custom meteor collections internally
Meteor.users.find().observe({
added: function (id) {
var doc = elasticSearchClient.index(indexName, objName, {
"id": id._id,
"first_name": id.profile.first_name
}).on('data',function (data) {
}).exec();
}
});
//meteor search method
Meteor.methods({
qryObject: {} // can be any valid query set up from elastic search against this index.
searchUser: function (qryObject) {
var dataObj = {};
//run query in sync mode
var data = Meteor.sync(function (done) {
elasticSearchClient.search(indexName, objName, qryObject)
.on('data',function (data) {
dataObj = data;
dataObj = JSON.parse(dataObj);
done(null, dataObj);
}).on('error', function (error) {
console.log(error)
})
.exec()
});
return data;
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment