Skip to content

pavelvlasov/mongoose-search-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongoose full-text search plugin

Simple mongoose plugin for full text search. Uses natural stemming and distance algorithms.

Example

  var mongoose = require('mongoose'),
    searchPlugin = require('mongoose-search-plugin');

  var Schema = mongoose.Schema({
    title: String,
    description: String,
    tags: [String]
  });

  Schema.plugin(searchPlugin, {
    fields: ['title', 'description', 'tags']
  });

  var Model = mongoose.model('MySearchModel', Schema);
  Model.search('some query', {title: 1}, {
    conditions: {title: {$exists: true}},
    sort: {title: 1},
    limit: 10
  }, function(err, data) {
    // array of found results
    console.log(data.results);
    // count of all matching objects
    console.log(data.totalCount);
  });

Installation

  $ npm install mongoose-search-plugin --save

Usage

Initialization

plugin accepts options argument with following format:

  var options = {
    keywordsPath: '_keywords', // path for keywords, `_keywords` as default
    relevancePath: '_relevance', // path for relevance number, '_relevance' as default
    fields: [], // array of fields to use as keywords (can be String or [String] types),
    stemmer: 'PorterStemmer', // natural stemmer, PorterStemmer as default
    distance: 'JaroWinklerDistance' // distance algorithm, JaroWinklerDistance as default
  };
  Schema.plugin(searchPlugin, options);

Search

Model.search(query, fields, options, callback)

Options are optional. The fields parameter can be an empty object to return all fields.

Method will return object of the following format:

  {
    results: [], // array of results objects
    totalCount: 0 // number of objects, that matched criteries
  }

Options has following format:

  {
    conditions: {}, // criteria for query
    sort: {} // sorting parameters
    populate: [{path: '', fields: ''}], // array of paths to populate
    ... and other options of Model.find method
  }

By default results sorts by relevance field, that defined in relevancePath plugin option.

Set keywords

If You start using plugin on existing database to initialize keywords field in object use setKeywords method.

  Model.setKeywords(function(err) {
    // ...
  });

About

Full text search plugin for mongoose

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •