Search code examples
node.jsmongodbfull-text-searchmongoosetext-search

Full Text Search in MongoDB/Node.js-mongoose-text-search


I am trying to perform full text search on MongoDB and Node.js using the mongoose-text-search plugin. I am following the example code from https://github.com/aheckmann/mongoose-text-search and my code is shown below. I keep getting an error stating: "Error: text search not enabled. undefined" I followed the directions from Installing plugins for mongoose - getting error, which led me to the MongoDB site: http://docs.mongodb.org/manual/tutorial/enable-text-search. However, after I enable text search by typing the command:

mongod --setParameter textSearchEnabled=true

in terminal, I start my application and encounter an additional error. It states: "MongoError: E11000 duplicate key error index: meddb.tweets.$id_1 dup key: { : null } This is error: Error: text search not enabled undefined"

If any of you have encountered this error and found a way around it , please let me know what I am missing or need to change.

var mongoose = require('mongoose');
var textSearch = require('mongoose-text-search');
var Schema = mongoose.Schema;
var twitterSchema = new Schema ({
        id: {type: Number, index: {unique: true, dropDups: true}},
        created_at: Date,
        user: [{
                id: Number,
                name: String,
                screen_name: String,
                location: String
        }],
        text: String,
        keywords: []
});

twitterSchema.plugin(textSearch);

twitterSchema.index({keywords: 'text' });

var Tweets = mongoose.model('Tweets', twitterSchema);

Tweets.create({text: 'flu', keywords: ['disease', 'doctor', 'shots']}, function(err){
    if(err){
        console.log('First error: ' + err);
    }

    Tweets.textSearch('shots', function(err, output){
        if(err){
            console.log('This is error: ' + err)
        }

        var inspect = require('util').inspect;
        console.log(inspect(output, {depth: null}));

   });
});

exports.Document = function(db) {
  return db.model('Tweets');
};

Solution

  • The mongoose-text-search plugin is working for me. I had to set textSearchEnabled to true. I didn't try it as a parameter on startup, but this worked once my mongod instance was already running:

    use admin
    db.runCommand({'setParameter':1,"textSearchEnabled":true})
    use <my db>
    <my db>.<my colleciton>.ensureIndex({"$**":"text"}) //Beware! "$**" indexes the entire document
    

    Also, you really might want to consider using Elasticsearch instead of Mongo's FTS. Mongo's FTS solution is not production ready (see the warning). I also have limited experience with Elasticsearch but found it very impressive.

    If you do choose to go the Elasticsearch route and still want to use Mongo, there are some decent options:

    • A river, which requires turning on replica set so the river can monitor the oplog (see the wiki)
    • Mongoosastic, which plugs in nicely to Mongoose for querying and keeps ES in sync with Mongo by writing to both Mongo and ES.

    Update Since 2.6 Mongo has text search enabled by default.