【问题标题】:mongo db text search with mongoosemongodb 使用 mongoose 进行文本搜索
【发布时间】:2013-04-06 03:03:42
【问题描述】:

我正在使用 mongo db nodejs 和 mongoose。

我想使用 mongodb 新文本搜索。

尝试像 aaronheckmann 建议的那样使用 mongoose-text-search,但我不断收到错误消息。

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

运行时我得到:

no text index for: db.items

谢谢!

【问题讨论】:

  • 你得到什么错误?
  • 无文本索引:db.items

标签: mongodb search text mongoose


【解决方案1】:

据我所知,大多数驱动程序尚未实现 text 搜索命令/函数,因此调用它的唯一方法是使用 runCommand 函数。

您需要确保首先在您的数据库上启用它(显然要创建一个文本索引)。

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

或运行时

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

【讨论】:

  • 我的问题是,如果唯一的方法是runCommand,我该如何在mongoose中使用runCommand?好像mongoose不支持,试了executeDbCommand还是不行。
【解决方案2】:
npm install mongoose-text-search

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

发现更多猫鼬功能的好地方是http://plugins.mongoosejs.com

【讨论】:

  • 这个插件还需要吗,还是现在mongoose支持文本搜索...有点困惑,因为这是一年半前的事了
【解决方案3】:

在将架构注册为模型之前,您必须将插件添加到架构中

更新

同样,您需要在注册模型之前在模式上定义索引。所以像这样重新排序你的代码部分:

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 调用将猫鼬连接到数据库,我在任何地方都看不到,但我假设您在此代码范围之外执行此操作。这是我用来确认这个工作的完整代码:

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

创建的文本搜索索引在 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
  }
]

【讨论】:

  • 很好地解决了 previos 错误,但我仍然收到错误 - 没有文本索引:db.items。正如我在问题中所写,我确实声明了测试索引。怎么了?谢谢!
  • @Liatz 在将模式注册为模型之前,您需要在模式上调用index。查看更新的答案。
  • 运行上面的代码仍然会弹出一个错误 - [错误:没有文本索引:db.items]
  • @Liatz 您运行的是 2.4+ 版本的 Mongo 吗?您是否在 Mongo 服务器中启用了文本搜索? best_match_index 是否出现在 shell 中的 db.items.getIndexes() 结果中?
  • MongoDB shell 版本:2.4.2 低。文件数为 256,应至少为 1000 Sat Apr 27 15:02:42.366 [initandlisten] Index { v: 1, key: { type: "text", color: "text", category_A: "text", category_B : "text", category_C: "text" }, ns: "db.items", name: "type_text_color_text_category_A_text_category_B_text_category_C_text", sparse: false, background: false } 声称是'text'类型,要么无效要么没有在 v2.4 之前存在。见升级部分:dochub.mongodb.org/core/upgrade-2.4
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 2021-10-21
  • 2016-03-14
  • 2014-11-19
  • 1970-01-01
相关资源
最近更新 更多