Brief Description:
In my NodeJS Application, I have a model called User.
User.js
module.exports = {
attributes: {
name: {
type: 'string'
}
}
};
My application consists users searching functionality using name attribute. For that I used find users using exact name as below(I am using MongoDB as my Database). My User Controller as below.
UserController.js
var name = req.param('name');
User.find({ name: { 'contains': name } }).exec(function (err, results) {
if (err) {
return res.serverError(err);
}
return res.send(results);
});
Now I need to enhance my searching functionality.
As an example if my DB contains record as Michael and if I searched as micel, Michael should be given as a search result.
For that I used Fuzzy Logic Algorithm and I used fuzzy npm
package. Now my implementation is as below.
UserController.js
var fuzzy = require('fuzzy');
module.exports = {
search: function (req, res) {
var term = req.param('name');
var thisCtrl = this;
User.find().exec(function (err, users) {
if (err) {
return res.serverError(err);
}
thisCtrl.createSearchQuery(term, users, function (orQuery) {
User.find({or: orQuery}).exec( function (err, finalResulst) {
if (err) {
return res.serverError(err);
}
return res.send(finalResulst)
})
})
});
},
createSearchQuery: function (term, users, cb) {
var userNamesArr = [];
var or = [];
for (var i = 0; i < users.length; i++) {
userNamesArr.push(users[i].name)
}
var results = fuzzy.filter(term, userNamesArr);
var matches = results.map(function(el) { return el.string; });
for (var i = 0; i < matches.length; i++) {
var tempTerm = { name: { 'contains' : matches[i] }};
or.push(tempTerm)
}
cb(or);
}
};
This is working as I needed.
But My Question is,
I am getting all the users in my DB and create search term using Fuzzy Logic and finding again in User collection. Now this is ok if I have only about 100 user records. But the time when User collection contains Millions of user records , Will this causes to performance issue to my application or is there a Better Approach than this I should follow.
Sorry for my bad English and Suggestions are highly appreciated.