【发布时间】:2014-11-30 20:17:29
【问题描述】:
我正在尝试为我的流星网络应用程序创建关键字搜索。在大多数情况下,它的工作问题是它非常慢。在当前形式中,用户在撰写文章时为其提供关键字。 keyS 一次从 mongodb 中用搜索数组 (skeywords) 中的关键字查询一篇文章,然后给它一个分数,并将得分最高的 100 篇文章发送给用户。怎么可能一次查询所有相关文章?
ps 我是不是把这一切都搞错了。
来自客户端的数据如下所示。
var keyw = ['java','code','jdk','food','good','cook'];
Meteor.call('keyS',keyw);
'keyS' 看起来的数据是一个文章 ID 数组。
例子
Sarticles = [someid,someid]
服务器
Meteor.methods({
keyS: function(skeywords) {
article: 'tempid',
var score = {
totalScore: 0
};
var potentials = [];
var badArticles = [];
var i = 0;
while (i < skeywords.length) {
var key = [];
key.push(skeywords[i]);
console.log(key);
if (typeof badarticles == "undefined") {
var theArticle = Articles.findOne({
articlekeywords: {
$in: key
}
});
} else {
var theArticle = Articles.findOne({
$and: [{
articlekeywords: {
$in: key
}
}, {
_id: {
$nin: badArticles
}
}]
});
};
if (typeof theArticle == "undefined") {
console.log("no more articles with that keyword")
i++;
continue
}
score.post = theArticle._id;
console.log(score.article);
score.totalScore = 0;
var points = 0;
var theKeywords = thearticle.keywords;
console.log("score worked");
var points = 0;
for (var a = 0; a < skeywords.length; a++) {
var keynumber = theKeywords.indexOf(skeywords[a]);
if (keynumber > -1) {
points++
} else {
continue
}
};
score.totalScore = points;
console.log(score.totalScore);
if (score.totalScore > 2) {
//limiter on number of posts looked at and number added to potentials
potentials.push({
iD: score.post,
totalScore: score.totalScore
});
var ID = score.article;
badposts.push(score.article);
console.log("added to potential" + ID + "to bad");
} else {
var badId = score.post;
console.log("marked as bad" + badId);
badposts.push(score.post);
}
};
potentials.sort(function(a, b) {
return b.totalScore - a.totalScore
})
for (var b = 0; b < 100; b++) {
if (typeof potentials[b] == "undefined") {
break
};
var ID = potentials[b].iD;
Meteor.users.update({
"_id": this.userId
}, {
"$addToSet": {
"Sarticles": ID
}
});
}
}
});
【问题讨论】:
-
根据我的经验,任何时候一种方法都这么长,它会从重构中受益——这可能会暴露问题所在。但更重要的是,让我问这个问题:用户输入了什么,预期的结果是什么?因此,例如,用户类型:'meteor mongodb performance' 和一些文章被扫描以查看它们是否被标记为一个或多个关键字,然后根据他们实际拥有的关键字数量进行评分?
-
@SteveRoss 我添加了一些我认为应该有助于解释的内容。如果没有,我会再试一次。感谢您的帮助。
标签: javascript mongodb meteor mongodb-query keyword-search