mongoose-fuzzy-searching is simple and lightweight plugin that enables fuzzy searching in documents in MongoDB. This code is based on this article.
- Creates Ngrams for the selected keys in the collection
- Add fuzzySearch method on model
- Work with pre-existing data
Install using npm
npm i mongoose-fuzzy-searching
In the below example, we have a User
collection and we want to make fuzzy searching in firstName
and lastName
.
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)
// }
});
});
The results are sorted by the confidenceScore
key. You can override this option.
User.fuzzySearch('jo').sort({ age: -1 }).exec(function (err, users) {
console.error(err);
console.log(users);
});
Options must have a fields
key, which is an Array of Strings
or an Array of Objects
.
var mongoose_fuzzy_searching = require('mongoose-fuzzy-searching');
var UserSchema = new Schema({
firstName: String,
lastName: String,
email: String
});
UserSchema.plugin(mongoose_fuzzy_searching, {fields: ['firstName', 'lastName']});
// or
UserSchema.plugin(mongoose_fuzzy_searching, {
fields: [{
name: 'firstName'
}, {
name: 'lastName'
}]
});
The below table contains the expected keys for an object
key | type | default | description |
---|---|---|---|
name | String | null | Collection key name |
minSize | Integer | 2 | N-grams min size. Learn more about N-grams |
weight | Integer | 1 | Denotes the significance of the field relative to the other indexed fields in terms of the text search score. Learn more about index weights |
prefixOnly | Boolean | false | Only return ngrams from start of word. (It gives more precise results) |
escapeSpecialCharacters | Boolean | true | Remove special characters from N-grams. |
keys | Array[String] | null | If the type of the collection attribute is Object or [Object] (see example), you can define which attributes will be used for fuzzy searching |
Example:
var mongoose_fuzzy_searching = require('mongoose-fuzzy-searching');
var UserSchema = new Schema({
firstName: String,
lastName: String,
email: String,
text: [{
title: String,
description: String,
language: String
}]
});
UserSchema.plugin(mongoose_fuzzy_searching, {
fields: [{
name: 'firstName',
minSize: 2,
weight: 5
}, {
name: 'lastName',
minSize: 3,
prefixOnly: true,
}, {
name: 'email',
escapeSpecialCharacters: false,
}, {
name: 'text',
keys: ["title"] // supports only one key so far.
}]
});
fuzzySearch
method can accept up to three parameters. The first one is the query, which can either be either a String
or an Object
. This parameter is required.
The second parameter can either be eiter an Object
with other queries, for example age: { $gt: 18 }
, or a callback function.
If the second parameter is the options, then the third parameter is the callback function. If you don't set a callback function, the results will be returned inside a Promise.
The below table contains the expected keys for the first parameter (if is an object)
key | type | deafult | description |
---|---|---|---|
query | String | null | String to search |
minSize | Integer | 2 | N-grams min size. |
prefixOnly | Boolean | false | Only return ngrams from start of word. (It gives more precise results) the prefix |
Example:
/* Without options and callback */
Model.fuzzySearch('jo').then(console.log).catch(console.error);
// or
Model.fuzzySearch({query: 'jo'}).then(console.log).catch(console.error);
// with prefixOnly and minSize
Model.fuzzySearch({query: 'jo', prefixOnly: true, minSize: 4}).then(console.log).catch(console.error);
/* With options and without callback */
Model.fuzzySearch('jo', {age: { $gt: 18 }}).then(console.log).catch(console.error);
/* With callback */
Model.fuzzySearch('jo', function(err, doc) {
if(err) {
console.error(err);
} else {
console.log(doc);
}
});
/* With options and callback */
Model.fuzzySearch('jo', {age: { $gt: 18 }}, function(err, doc) {
if(err) {
console.error(err);
} else {
console.log(doc);
}
});
The plugin creates indexes for the selected fields. In the below example the new indexes will be firstName_fuzzy
and lastName_fuzzy
. Also, each document will have the fields firstName_fuzzy
[String] and lastName_fuzzy
[String]. These arrays will contain the anagrams for the selected fields.
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']});
In other words, thit plugin creates anagrams when you create or update a document. All the pre-existing documents won't contain these fuzzy arrays, so fuzzySearch
function, will not be able to find them.
In order to create anagrams for pre-existing documents, you should update each document. The below example, updates the firstName
attribute to every document on the collection User
.
const { each, queue } = require('async');
const updateFuzzy = async (Model, attrs) => {
const docs = await Model.find();
const updateToDatabase = async (data, callback) => {
try {
if(attrs && attrs.length) {
const obj = attrs.reduce((acc, attr) => ({ ...acc, [attr]: data[attr] }), {});
return Model.findByIdAndUpdate(data._id, obj).exec();
}
return Model.findByIdAndUpdate(data._id, data).exec();
} catch (e) {
console.log(e);
} finally {
callback();
}
};
const myQueue = queue(updateToDatabase, 10);
each(docs, (data) => myQueue.push(data.toObject()));
myQueue.empty = function () {};
myQueue.drain = function () {};
}
// usage
updateFuzzy(User, ['firstName']);
In the previous example, we set firstName
and lastName
as the fuzzy attributes. If you remove the firstName
from the fuzzy fields, the firstName_fuzzy
array will not be removed by the collection. If you want to remove the array on each document you have to unset that value.
const { each, queue } = require('async');
const removeUnsedFuzzyElements = (Model, attrs) => {
const docs = await Model.find();
const updateToDatabase = async (data, callback) => {
try {
const $unset = attrs.reduce((acc, attr) => ({...acc, [`${attr}_fuzzy`]: 1}), {})
return Model.findByIdAndUpdate(data._id, { $unset }, { new: true, strict: false }).exec();
} catch (e) {
console.log(e);
} finally {
callback();
}
};
const myQueue = queue(updateToDatabase, 10);
each(docs, (data) => myQueue.push(data.toObject()), () => { });
myQueue.empty = function () {
};
myQueue.drain = function () {
console.log("done");
};
}
// usage
removeUnsedFuzzyElements(User, ['firstName']);
MIT License
Copyright (c) 2019 Vassilis Pallas
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.