dimanche 30 novembre 2014

How to make this keyword search work?


I am trying to create keyword search for my meteor webapp. And For the most part it works the problem is it is very slow. In the current form when making a article the user gives it keywords. keyS queries one article with a keyword from the search array(skeywords) at a time from mongodb then gives it a score and the 100 highest scored articles are sent to the user. How could it query all the relevant articles at once?


ps Am I going about this all wrong.


The data coming from the client looks like this.



var keyw = ['java','code','jdk','food','good','cook'];
Meteor.call('keyS',keyw);


the data coming out of 'keyS' looks is a array of article ids.


example


Sarticles = [someid,someid]


server



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
}
});
}
}

});




Aucun commentaire:

Enregistrer un commentaire