Giter VIP home page Giter VIP logo

feathers-mongodb-fuzzy-search's Introduction

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 search = require('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

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'
  }
})

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

feathers-mongodb-fuzzy-search's People

Contributors

arve0 avatar claustres avatar raphaklaus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.