Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Derives keywords from document properties

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 History.md
Octocat-spinner-32 LICENSE
Octocat-spinner-32 Makefile
Octocat-spinner-32 README.md
Octocat-spinner-32 index.js
Octocat-spinner-32 package.json
README.md

Mongoose-keywordize Plugin

Provides keyword derivation for Mongoose documents.

Build Status

Options:

  • fields: an array of paths you want watched and converted into keywords
  • fn: optional function to execute when keywordize() runs; if a value is returned it is included in the keywords array
  • pre: optional function to run against each value returned from each field before it's parsed and added to the keywords array
  • keywordField: the name of the field in which keywords will be stored; defaults to keywords
  • upper: true to retain letter casing. default is false (all keywords are lowercased)

Example:

var schema = new Schema({ name: String, title: String });
schema.plugin(keywordize, { fields: 'name title'.split(' ') });

This will introduce a new keywordize() document method which detects if any of the passed fields have been modified and updates the new keywords property appropriately.

Example:

var Person = mongoose.model('Person', schema);
var me = new Person({ name: 'aaron' });
me.keywordize();
console.log(me.keywords) // ['aaron']

The keywordize method is always called upon saving each document, auto-updating to the latest keywords.

me.title = 'Mr';
me.save(function (err) {
  console.log(me.keywords) // ['aaron', 'Mr']
})

index

Keywordize, by default, does not define an index on the "keywords" key. If you want to define an index you should use the "index" option:

var opts = {}
opts.index = true

pre

To have the opportunity to pre-process field values as they're retreived by the keywordize plugin before they are processed, pass an optional pre function. This function, when provided, will be run against each value returned from each field before it's parsed and added to the keywords array. The function is passed the value and field name.

var opts = {};
opts.fields = ['description', 'title']
opts.pre = function (value, field) {
  // remove html entities from each keyword picked from description
  if ('description' == field) {
    return value.replace(/&#?[a-z0-9]{2,8};/ig, ' ');
  } else {
    return value;
  }
}
var schema = new Schema({ description: String, title: String });
schema.plugin(keywordize, opts);

var Person = mongoose.model('Person', schema);
var me = new Person({ name: 'aaron' });
me.description = 'Tall & Awkward';
me.keywordize();
console.log(me.keywords) // ['aaron', 'tall', 'awkward']

fn

One may also pass an optional function to run custom logic within the call to keywordize. The optional function will be executed within the context of the document, meaning we have access to the documents properties through the this keyword to perform any custom logic necessary.

var opts = {};
opts.fields = ['name', 'title']
opts.fn = function custom () {
  if ('Mister' === this.title) {
    return 'Mr';
  }
}
var schema = new Schema({ name: String, title: String });
schema.plugin(keywordize, opts);

var Person = mongoose.model('Person', schema);
var me = new Person({ name: 'aaron' });
me.title = 'Mister';
me.keywordize();
console.log(me.keywords) // ['aaron', 'Mister', 'Mr']

Either a an Array or single string may be returned from the function and will be pushed onto the keywords array.

upper

By default mongoose-keywordize lowercases the keywords. To preserve casing pass the upper: true option.

Mongoose Version

>= 2.x

LICENSE

Something went wrong with that request. Please try again.