$search to mongodb
remove queries. Full-text search on documents with stemming as well as pattern matching on individual fields.
For full-text search, be sure to index your text fields, as this plugin uses mongodb $text.
For field pattern matching, mongodb $regex is used.
npm install feathers-mongodb-fuzzy-search
const search =// add search hook// may also use service.hooks to apply it on individual services onlyapp// create a text index on title property, for full-text search// you may add multiple fields to the text index// see the mongodb documentation for more on $textconst messages = appmessagesModel// find documents with title containing 'cat'// will find titles including 'cat', 'cats', etc. thanks to mongodb stemming// note: you can only use await inside async functionslet catDocuments = await messages// find users with first name containing a 's' and last name containing 'art'let userDocuments = await app
Complete example here.
$search is removed and
$search is padded with
some " text becomes
"some text". If you want to disable this behaviour and leverage the full MongoDB $text API, you can disable escaping like this:
RegExp field search
options object given to
search(options) supports the following:
fields: Array of field names to allow searching in.
excludedFields: Array of field names that can't be searched. If given, any field not in array can be searched.
fieldsNotEscaped: Array of fields to be excluded from RegExp escape. As default any field not given are escaped to avoid RegExp denial of service attacks.
You can pass MongoDB options for
$diacriticSensitive with your query. E.g. If you'd like to disable stemming add
$language: 'none' to your query parameters:
This package is tested with MongoDB version 3.2. You will probably run into problems using older versions of MongoDB, for example version 2.4 does not support
npm test # runs mocha
MIT © 2017 Arve Seljebu / Luc Claustres