Currently, I am attempting to implement a keyword search feature for my meteor web app. Although the functionality is there, it's running quite slow. The way it works now is that when a user creates an article, they assign keywords to it. The keyS
function then queries one article at a time from MongoDB based on a keyword in the search array (skeywords
), generates a score for each article, and finally sends the top 100 scored articles back to the user. Is there a more efficient way to query all relevant articles simultaneously?
P.S. Is my approach completely wrong?
This is how the data sent from the client appears:
var keyw = ['java','code','jdk','food','good','cook'];
Meteor.call('keyS', keyw);
The output of 'keyS' is an array of article IDs.
Example:
Sarticles = [someid, someid]
Server-side:
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
}
});
}
}
});