在流星中使用npm弹性搜索 [英] using npm elastic-search in meteor
问题描述
//客户端
if(Meteor.isClient)
{
Template.body.events({
'keyup #pinCode':function(data){
//event.preventDefault();
console.log($('#pinCode')。val());
Meteor.call('new_earch',$('#pinCode')。val(),
function (错误,结果){
if(error){
console.error(error)
}
else {
console.log(result)
}
});
}});
}
上述客户端代码正在调用以下服务器方法:
//服务器端
var elasticsearch = Npm.require('elasticsearch');
Meteor.methods({
new_earch:function(data){
var client = new elasticsearch.Client({
host :'localhost:9200',
log:'trace'
});
client.search({
index:'myjdbc',
q :data +*
},
function(resp){
var hits = resp;
console.log('************ *****************************');
console.log(hits);
return hits;
});
}
});
但是我在客户端获得未定义的值。有什么问题?
您是否在全球安装了elasticsearch模块?
https://gentlenode.com/journal/ meteor-17-using-npm-modules-in-your-application / 36
(更新)尝试这样:
result = client.search({
index:'myjdbc',
q:data + *
});
return result.hits.hits.map(function(doc){
return doc._source;
});
(更新#2)
问题是因为流星使用光纤。你可以尝试这样的东西,使弹性搜索工作与纤维:
//使用
的示例var Future = Npm.require( '纤维/未来');
var future = new Future();
// es客户端 - https://www.npmjs.com/package/elasticsearch
esClient.search({
index:'searchable-collection',
q:q, // querystring
size:10,// 10个结果
函数(err,response){
//抓取所有的ids
var hitIds = _.pluck(reponse.hits .hits,'_id');
//通过id找到所有的文档并发布他们
future.return(
SearchableCollection.find({_ id:{$ in:hitIds}})
);
}
});
你有想法?尝试 future.return()
或者你可以尝试meteorhaks包:
ps:对我来说通过http PUT请求发送数据,因为我将仅使用一个ES实例。
I am using elastic-search node module in meteor Following is my code snippet.
//client side
if(Meteor.isClient)
{
Template.body.events({
'keyup #pinCode': function(data) {
//event.preventDefault();
console.log($('#pinCode').val());
Meteor.call('new_earch',$('#pinCode').val(),
function(error, result) {
if(error){
console.error(error)
}
else {
console.log(result)
}
});
}});
}
The above client code is calling following server method:
//server side
var elasticsearch = Npm.require('elasticsearch');
Meteor.methods({
new_earch: function(data){
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
client.search({
index: 'myjdbc',
q:data+"*"
},
function (resp) {
var hits = resp;
console.log('*******************************************');
console.log(hits);
return hits;
});
}
});
But i am getting undefined value at client side. Whats the issue?
Have you installed elasticsearch module globally?
https://gentlenode.com/journal/meteor-17-using-npm-modules-in-your-application/36
(update) try this:
result = client.search({
index: 'myjdbc',
q:data+"*"
});
return result.hits.hits.map(function(doc) {
return doc._source;
});
(update #2)
The problem is because Meteor use Fibers. You can try something like this to make elasticsearch work with fibers:
//Example to work with
var Future = Npm.require('fibers/future');
var future = new Future();
// es client - https://www.npmjs.com/package/elasticsearch
esClient.search({
index: 'searchable-collection',
q: q, // querystring
size: 10, // 10 results
function (err, response) {
// grab all the ids
var hitIds = _.pluck(reponse.hits.hits, '_id');
// find all of documents by id and publish them
future.return(
SearchableCollection.find({_id: {$in: hitIds}})
);
}
});
You get the idea? Try future.return()
Or you can try meteorhaks packages:
ps: as for me I'm going to send data through http PUT request since I will using only one ES instance.
这篇关于在流星中使用npm弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!