Skip to content

Instantly share code, notes, and snippets.

@bcoe
Created May 11, 2014 18:36
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 bcoe/959891a3e44a416fda92 to your computer and use it in GitHub Desktop.
Save bcoe/959891a3e44a416fda92 to your computer and use it in GitHub Desktop.
search.js
var _ = require('lodash'),
elasticsearch = require('elasticsearch');
function Search(opts) {
_.extend(this, {
client: new elasticsearch.Client({
host: process.env.ELASTIC_SEARCH_URL || 'localhost:9200'
})
}, opts);
}
// The search method called by server.js.
Search.prototype.search = function(q, cb) {
this.client.search({
index: 'npm',
size: 50,
body: {
query: {
query_string: {
fields: ['_id'],
query: q + '*'
}
}
}
}, function(err, resp) {
return cb(
err,
_.map(resp.hits.hits, function(hit) {
return {value: hit._id};
})
);
});
};
exports.Search = Search;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment