npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

feathers-mongodb-fuzzy-search

v2.0.1

Published

hook which adds fuzzy search for mongodb through $search in find query

Downloads

5,738

Readme

npm version Build Status

feathers-mongodb-fuzzy-search

Add $search to mongodb service.find, update, patch and 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.

Install

npm install feathers-mongodb-fuzzy-search

Usage

const service = require('feathers-mongodb')
const search = require('feathers-mongodb-fuzzy-search')

app.use('/messages', service({
  Model: db.collection('messages'),
  whitelist: ['$text', '$search'], // fields used by feathers-mongodb-fuzzy-search
}))

// add search hook
// may also use service.hooks to apply it on individual services only
app.hooks({
  before: {
    all: [
      search(), // full text search on text indexes
      search({  // regex search on given fields
        fields: ['firstName', 'lastName']
      })
    ]
  }
})

// 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 $text
const messages = app.service('messages')

// If you're using MongoDB database adapter:
messages.Model.createIndex({ title: 'text' })
// or if you're using Mongoose database adapter:
// messages.Model.index({ title: 'text' })

// 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 functions
let catDocuments = await messages.find({ query: { $search: 'cat' } })

// find users with first name containing a 's' and last name containing 'art'
let userDocuments = await app.service('users').find({
  query: {
    firstName: { $search: 's' },
    lastName: { $search: 'art' }
  }
})

Complete example here.

REST usage

Full text search for qwerty with mongodb $text:

curl http://localhost:3030/messages?$search=qwerty

Search for qwerty on field firstName with mongodb $regex:

curl http://localhost:3030/users?firstName[$search]=qwerty

Notes

Full-text search

As default " in $search is removed and $search is padded with ". E.g. 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:

app.hooks({
  before: {
    find: search({ escape: false })
  }
})

RegExp field search

Remember to allow $regex in the service:

app.use('/messages', service({
  Model: db.collection('messages'),
  whitelist: ['$regex'], // field used by feathers-mongodb-fuzzy-search
}))

If not, you will get the error BadRequest: Invalid query parameter $regex on requests.

The 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.
app.service('users').hooks({
  before: {
    find: search({
      // make all fields but 'fullName' are searchable
      excludedFields: ['fullName'],
      // do not escape RegExp special characters for the field 'firstName'
      fieldsNotEscaped: ['firstName']
    })
  }
})

MongoDB options

You can pass MongoDB options for $text, like $language, $caseSensitive and $diacriticSensitive with your query. E.g. If you'd like to disable stemming add $language: 'none' to your query parameters:

users.find({
  query: {
    $search: 'cats',
    $language: 'none'
  }
})

NOTE: Remeber to allow the fields in your service:

  app.use('/messages', service({
    Model: db.collection('messages'),
    // fields used by feathers-mongodb-fuzzy-search
    whitelist: ['$text', '$search', '$caseSensitive', '$language', '$diacriticSensitive'],
  }))

Additional information

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 $text search.

See mongodb documentation for more details about $text.

See mongodb documentation for more details about $regex.

Development

npm test  # runs mocha

License

MIT © 2017 Arve Seljebu / Luc Claustres