Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo db text search with mongoose

I'm using mongo db nodejs and mongoose.

I would like to use mongodb new text search.

Trying to use mongoose-text-search like aaronheckmann advised but I keep getting an error.

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);
var ItemModel = mongoose.model('Item', Items);
Items.index({
    type            :"text",
    color           :"text",
   category_A      :"text",
    category_B      :"text",
    category_C      :"text"
},
   {
        name: "best_match_index",
       weights: {
            type: 5,  
            color:   4,
      }
    }
)
ItemModel.textSearch('D', function (err, output) {
    if (err) 
    console.log(err);
    else
    console.log(output)
})

When running this I get:

no text index for: db.items

Thanks!

like image 384
Liatz Avatar asked Apr 13 '13 17:04

Liatz


3 Answers

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
  }
]
like image 123
JohnnyHK Avatar answered Nov 15 '22 09:11

JohnnyHK


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

like image 34
aaronheckmann Avatar answered Nov 15 '22 07:11

aaronheckmann


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 } )
like image 23
sambomartin Avatar answered Nov 15 '22 08:11

sambomartin