mongo db text search with mongoose mongo db text search with mongoose mongoose mongoose

mongo db text search with mongoose


You have to add the plugin to the schema before registering the schema as a model.

UPDATED

Likewise, you need to define the index on the schema before registering the model. So re-order that section of your code like this:

var textSearch = require("mongoose-text-search");Items.plugin(textSearch);Items.index({    type            :"text",    color           :"text",    category_A      :"text",    category_B      :"text",    category_C      :"text"}, {    name: "best_match_index",    weights: {        type: 5,          color: 4    }});var ItemModel = mongoose.model('Item', Items);

Note that you also need to connect mongoose to the database via a mongoose.connect call which I don't see anywhere, but I'm assuming you're doing that outside of the scope of this code. Here's the full code I used to confirm this works:

var mongoose = require("mongoose");var Schema  = mongoose.Schema;var ObjectId = Schema.ObjectId;var Items = new Schema({    type            : { type : String , default:""},    color           : { type : [String] , default:""},    category_A      : { type : String , default:""},    category_B      : { type : String , default:""},    category_C      : { type : String , default:""},});var textSearch = require("mongoose-text-search");Items.plugin(textSearch);Items.index({    type            :"text",    color           :"text",    category_A      :"text",    category_B      :"text",    category_C      :"text"}, {    name: "best_match_index",    weights: {        type: 5,        color: 4,    }});var ItemModel = mongoose.model('Item', Items);mongoose.connect('mongodb://localhost/test', function (err) {  ItemModel.textSearch('D', function (err, output) {    if (err)      console.log(err);    else      console.log(output);    mongoose.disconnect();  });});

The created text search index looks like this in the shell:

test> db.items.getIndexes()[  {    "v": 1,    "key": {      "_id": 1    },    "ns": "test.items",    "name": "_id_"  },  {    "v": 1,    "key": {      "_fts": "text",      "_ftsx": 1    },    "ns": "test.items",    "name": "best_match_index",    "weights": {      "category_A": 1,      "category_B": 1,      "category_C": 1,      "color": 4,      "type": 5    },    "background": true,    "safe": null,    "default_language": "english",    "language_override": "language",    "textIndexVersion": 1  }]


npm install mongoose-text-search

https://github.com/aheckmann/mongoose-text-search

A good place to discover additional mongoose functionality is http://plugins.mongoosejs.com


As far as I'm aware, most drivers haven't implemented text search commands/functions so the only way to call it is using the runCommand function.

You need to make sure you enable it on your database first though (and obviously create a text index).

http://docs.mongodb.org/manual/tutorial/enable-text-search/

or runtime

db.adminCommand( { setParameter : 1, textSearchEnabled : true } )