Registry / database / feathers-mongodb-fuzzy-search

feathers-mongodb-fuzzy-search

JSON →
library2.0.1jsnpmunverified

Feathers.js hook that adds fuzzy search to MongoDB queries via $search, supporting both full-text search (MongoDB $text with stemming) and field-specific regex pattern matching. Version 2.0.1 is current. It applies as a before hook on find, update, patch, and remove. Key differentiators: simple API, automatic escaping for ReDoS prevention, and option to disable escaping for direct $text API access. Requires whitelisting of query operators in the service configuration.

npm install feathers-mongodb-fuzzy-search
INSTALL
IMPORT
SIG · FEATHERS-MONGODB-F
F
feathers-mongodb-fuzzy-search
databasejavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import search from 'feathers-mongodb-fuzzy-search'
const search = require('feathers-mongodb-fuzzy-search')
Default export is a function that returns the hook. CommonJS require works but TypeScript may need esModuleInterop.
search (named)
import { search as fuzzySearch } from 'feathers-mongodb-fuzzy-search'
import { search } from 'feathers-mongodb-fuzzy-search'
Named export exists but default is more common. Check package exports.

Demonstrates installing, configuring, and using the hook for both full-text and regex fuzzy searches.

const search = require('feathers-mongodb-fuzzy-search'); // Register service with MongoDB app.use('/messages', service({ Model: db.collection('messages'), whitelist: ['$text', '$search'] })); // Apply fuzzy search hook globally app.hooks({ before: { all: [ search(), // full-text search on text indexes search({ fields: ['firstName', 'lastName'] }) // regex on fields ] } }); // Create text index on title app.service('messages').Model.createIndex({ title: 'text' }); // Search: full-text for 'cat' const results = await app.service('messages').find({ query: { $search: 'cat' } }); // Search: regex on firstName const results = await app.service('users').find({ query: { firstName: { $search: 's' } } });
Debug
Known issues
breakingVersion 2.0.0 removed 'escape' option defaulting to true; previously it was false.
fix
Update hooks to explicitly set escape: false if needed.
affects: <2.0.0
deprecatedThe 'fields' option without 'excludedFields' will search only listed fields. In future, all fields might be searchable by default.
affects: >=2.0.0
gotchaMust whitelist '$text' and '$search' in service options, otherwise queries will fail with 'BadRequest: Invalid query parameter'.
fix
Add 'whitelist: ["$text", "$search", "$regex"]' to service configuration.
affects: >=1.0.0
gotchaFull-text search requires a MongoDB text index on the field(s) being searched. Without it, $text will not work.
fix
Create a text index via Model.createIndex() or MongoDB shell.
affects: >=1.0.0
gotchaBy default, double quotes in $search are stripped and the string is wrapped in quotes for phrase search. To disable, set escape: false.
affects: >=2.0.0
Errors
Common errors & fixes
BadRequest: Invalid query parameter $search
$search is not whitelisted in the service configuration.
fix
Add '$search' to the whitelist array in the service options, e.g., { whitelist: ['$text', '$search'] }.
TypeError: Cannot read property 'Model' of undefined
Service not initialized or Model not provided.
fix
Ensure the service is properly set up with a Mongoose or MongoDB collection Model.
error: Expected 0 arguments, but got 1.
Using search(options) without calling the returned hook function correctly.
fix
Call search() to get the hook function, e.g., app.hooks({ before: { all: [search()] } });
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
feathersrequiredpeer dependency; requires feathers application and hook system
Agent activity
4 hits · last 30 days
node
4
Resources
feathers-mongodb-fuzzy-search — npm install feathers-mongodb-fuzzy-search · libregistry