What is Fuzzy Search?
Searching and returning not just for the exact match of input but also closely related ones. Example, when searching for planet, returning results not just for planet but also for planets or planetary. Also, when the spelling is wrong then returning results also for the correct ones. Example, on searching for 'Misissippi', the result includes also 'Mississippi'.Fuzzy Search in Mongoose
Here is a library that helps with fuzzy searching MongoDB using Mongoose in NodeJS: mongoose-fuzzy-searchingExample,
var mongoose_fuzzy_searching = require('mongoose-fuzzy-searching');
var UserSchema = new Schema({
firstName: String,
lastName: String,
email: String,
age: Number
});
UserSchema.plugin(mongoose_fuzzy_searching, {fields: ['firstName', 'lastName']});
var User = mongoose.model('User', UserSchema);
var user = new User({ firstName: 'Joe', lastName: 'Doe', email: 'joe.doe@mail.com', age: 30});
user.save(function () {
// mongodb: { ..., firstName_fuzzy: [String], lastName_fuzzy: [String] }
User.fuzzySearch('jo', function (err, users) {
console.error(err);
console.log(users);
// each user object will not contain the fuzzy keys:
// Eg.
// {
// "firstName": "Joe",
// "lastName": "Doe",
// "email": "joe.doe@mail.com",
// "age": 30,
// "confidenceScore": 34.3 ($text meta score)
// }
});
});
More detailed explanation can be seen in the library mongoose-fuzzy-searching
Comments
Post a Comment