我正在完成一个项目,其最终挑战是系统可以根据对网站的正面投票推荐其他网站或对其进行评论。
系统使用keywords
字段,在该字段上使用easysearch
进行搜索,并根据该字段建立该推荐系统。
算法很简单,当用户对网站投赞成票时,该事件就是您的全部keywords
并发送easysearch
,找到至少包含其中一个关键字的网站,只返回一个随机网站并在一个很好的模态消息中返回结果。
邮件的模板是:
<template name="recommend_site">
<div class="modal fade" tabindex="-1" role="alertdialog" id="website_recommend">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title">{{title}}</h4>
</div>
<div class="modal-body">
<h6 class="text-right">{{momentFormat createdOn}}</h6>
<p>
{{description}}
</p>
<a href="/site/{{_id}}">See more...</a>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<span class="badge alert-success">{{votes.Up}}</span>
{{#if currentUser}}
<div class="btn-group" role="group" aria-label="...">
<button class="btn btn-default js-upvote">
<span class="fa fa-thumbs-o-up" aria-hidden="true"></span>
</button>
<button class="btn btn-default js-downvote">
<span class="fa fa-thumbs-o-down" aria-hidden="true"></span>
</button>
</div>
{{/if}}
<span class="badge alert-danger">{{votes.Down}}</span>
</div>
</div>
</div>
</div>
</template>
该事件应该使用此搜索:
Template.website_item.events({
"click .js-upvote": function (event) {
var website_id = this._id;
var keywords = this.keywords;
console.log("Up voting website with id " + website_id);
Websites.update({_id: website_id},
{$inc: {'votes.Up': 1}});
WebsitesIndex.getComponentMethods().search(keywords);
console.log(keywords);
return false;
}
});
文档网站的例子是:
Websites.insert({
title:"Google",
url:"http://www.google.com",
description:"Popular search engine.",
keywords: ['google', 'popular', 'search', 'engine'],
votes:{
Up: 0,
Down: 0
},
createdOn:new Date()
});
正如我之前所说,使用easysearch的sarch工作得很好,但我找不到那种搜索关键字并将结果发送到模板的方法。正如你在活动中看到的那样,我试图在文档后面得到结果,但是那时我无法想到我还能做些什么。
有人可以指导我吗?
更新 我在事件中添加了一些代码:
WebsitesIndex.getComponentMethods().search(keywords, { limit: 1 });
使用limit: 1
,我等待获得此搜索的一个结果,但是,我有一个问题:
Exception in template helper: Error: Match error: Failed Match.OneOf or Match.Optional validation
at check (http://localhost:3000/packages/check.js?9c4972d3f4fe56cd455d3022e70b5195e642fc41:61:15)
at MinimongoEngine.checkSearchParam (http://localhost:3000/packages/easysearch_core.js?a1c2859756f8a6aa5fec4727e1f529e581d0f124:386:7)
at Index.search (http://localhost:3000/packages/easysearch_core.js?a1c2859756f8a6aa5fec4727e1f529e581d0f124:91:26)
at Object.getCursor (http://localhost:3000/packages/easysearch_components.js?aa6c7b1263bc20dda4038492ac0d4fd3c5104ae8:331:26)
at EachComponent.doc (http://localhost:3000/packages/easysearch_components.js?aa6c7b1263bc20dda4038492ac0d4fd3c5104ae8:956:63)
at http://localhost:3000/packages/peerlibrary_blaze-components.js?4eddb4e3f0f660190548347e680d109a51a7050f:1234:23
at http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1650:16
at http://localhost:3000/packages/peerlibrary_blaze-components.js?4eddb4e3f0f660190548347e680d109a51a7050f:1287:66
at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:12)
at http://localhost:3000/packages/peerlibrary_blaze-components.js?4eddb4e3f0f660190548347e680d109a51a7050f:1286:27
好吧,我可以看到问题是一个帮助问题,所以,websiteIndex的全局帮助是:
Template.registerHelper('websiteIndex', () => WebsitesIndex);
但是,我还没有理解这个问题。