【问题标题】:Full Text Search in MongoDB/Node.js-mongoose-text-searchMongoDB/Node.js-mongoose-text-search 中的全文搜索
【发布时间】:2013-11-19 21:47:03
【问题描述】:

我正在尝试使用 mongoose-text-search 插件在 MongoDB 和 Node.js 上执行全文搜索。我正在关注https://github.com/aheckmann/mongoose-text-search 的示例代码,我的代码如下所示。我不断收到一条错误消息: “错误:未启用文本搜索。 不明确的” 我按照Installing plugins for mongoose - getting error 的指示,将我带到了 MongoDB 站点:http://docs.mongodb.org/manual/tutorial/enable-text-search。 但是,在我通过键入命令启用文本搜索后:

mongod --setParameter textSearchEnabled=true

在终端中,我启动了我的应用程序并遇到了一个额外的错误。它指出: “MongoError:E11000 重复键错误索引:meddb.tweets.$id_1 重复键:{:null} 这是错误:错误:未启用文本搜索 未定义”

如果你们中的任何人遇到此错误并找到解决方法,请让我知道我缺少什么或需要更改。

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');
};

【问题讨论】:

标签: node.js mongodb full-text-search mongoose text-search


【解决方案1】:

mongoose-text-search 插件对我有用。我必须将textSearchEnabled 设置为true。我没有在启动时尝试将它作为参数,但是一旦我的 mongod 实例已经运行,这就会起作用:

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

另外,您可能真的想考虑使用 Elasticsearch 而不是 Mongo 的 FTS。 Mongo 的 FTS 解决方案是 not production ready查看警告)。我对 Elasticsearch 的经验也有限,但发现它非常令人印象深刻。

如果您确实选择走 Elasticsearch 路线并且仍想使用 Mongo,有一些不错的选择:

  • 一个river,需要开启replica set 这样河就可以监控oplog(见wiki
  • Mongoosastic,很好地插入 Mongoose 进行查询,并通过写入 Mongo 和 ES 使 ES 与 Mongo 保持同步。

更新 从 2.6 开始,Mongo 默认启用文本搜索。

【讨论】:

猜你喜欢
  • 2020-07-15
  • 2017-05-14
  • 2012-08-02
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
相关资源
最近更新 更多