Returning Data from Elasticsearch


#1

I am using NPM elasticsearch to connect my app to elasticsearch. I am successfully indexing the data. And I get back the data I am expecting (my query works). But I can’t seem to send the data to the browser.

I made notes in the code below to clarify the issue.

Meteor.publish('searchContacts', function(searchContactsQuery) {
	var groupId = Meteor.users.findOne({groupId: groupId}).group.group_id;
	var query = {//working query inserted here};

	EsClient.search({
		index: 'contactsindex',
		type: 'contacts',
		size: 10,
		body: {
			query: query
		},
	}).then(function (body) {
		var contactIds = _.pluck(body.hits.hits, '_id');
		console.log(contactIds) //returns an array of ids as expected
		return Contacts.find({_id: {$in: contactIds}}) //returns nothing to the browser
	}, function (error) {
	  console.trace(error.message);
	});
});

Resources/Blogs/Threads on elasticsearch and meteor?
#2

Since you’re using promises with the elasticsearch package, you’ll want to look into using Meteor’s low-level publication API. Take a look at the Custom publications with the low level API section of the Guide.


#3

Thanks for pointing me in the right direction. I have used custom publications before but I am still having trouble with the implementation. Do you know of any specific code examples or tutorials I could look at?


#4

I wanted to do the same and was able to get it working after a lot of trial and error. The problem is that I actually do not understand why this is working, so if someone could explain I would really appreciate it.

Meteor.publish('issues.list', function (limit) {
    const future = new Future();

    let query = {//working query inserted here};

    Promise.await(EsClient.search({
        index: 'index',
	type: 'issue',
	size: limit,
	body: {
		query: query
	},
	}).then(Meteor.bindEnvironment(function (body) {
		const issueIds = _.map(body.hits.hits, '_id');
		future.return(Issue.find({_id: {$in: issueIds}}));
  		})));

    return future.wait();
});