6

私は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

ありがとう!

4

3 に答える 3

12

スキーマをモデルとして登録する前に、プラグインをスキーマに追加する必要があります。

更新しました

同様に、モデルを登録する前に、スキーマにインデックスを定義する必要があります。したがって、コードのそのセクションを次のように並べ替えます。

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

作成されたテキスト検索インデックスは、シェルでは次のようになります。

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
  }
]
于 2013-04-27T14:41:25.387 に答える
5
npm install mongoose-text-search

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

追加の mongoose 機能を見つけるには、 http://plugins.mongoosejs.comが適しています。

于 2013-04-15T01:35:57.100 に答える
2

私の知る限り、ほとんどのドライバーはtext検索コマンド/関数を実装していないため、それを呼び出す唯一の方法はrunCommand関数を使用することです。

ただし、最初にデータベースで有効にする必要があります (そして、明らかにテキスト インデックスを作成します)。

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

またはランタイム

db.adminCommand( { setParameter : 1, textSearchEnabled : true } )
于 2013-04-13T22:29:57.823 に答える