Skip to content

Instantly share code, notes, and snippets.

@timcroydon
Created June 24, 2015 10:45
Show Gist options
  • Save timcroydon/53e2246500afc27bf68c to your computer and use it in GitHub Desktop.
Save timcroydon/53e2246500afc27bf68c to your computer and use it in GitHub Desktop.
Simple ES client code example
// setup client
var elastic = require('elasticsearch')
var client = new elastic.Client({
host: self.config.host,
log: self.config.loglevel
})
// construct some query
var query = ejs.Request().query(ejs.FilteredQuery(
ejs.MatchAllQuery(),
ejs.AndFilter([
ejs.TermFilter('role', role)
]
)))
.sort(ejs.Sort(paging.sort.param)
.order(paging.sort.desc ? 'desc' : 'asc'))
// and then actually make the call
async.series([
function (cb) {
// call is made here
self.client.search({
index: config.index,
type: config.type,
body: query,
size: paging.size,
from: paging.page * paging.size
}, cb)
}],
// this is called on completion of the call
function (err, data) {
self.client.close()
callback(err, {
// some dumb mapping stuff - 'data' is object returned by ES
preferences: _.map(data[0][0].hits.hits, function (pref) {
return {
id: pref._id,
preference: pref._source.preference,
keywords: pref._source.keywords,
text: pref._source.text,
cluster: pref._source.topicId
}
}),
total: data[0][0].hits.total
})
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment