3

ElasticSearch と mongoosastic を使用して、MongoDB と ElasticSearch の間でデータを同期しています。別のオブジェクトであるスキーマのプロパティを研究に含めたい: 検索しているカテゴリの記事を表示したい。これらは私の 2 つのスキーマです: ArticleSchema と CategorySchema です。記事には「Category」という Category オブジェクトが含まれています。

var ArticleSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    ...
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    },
    categorie: {
        type: Schema.ObjectId,
        es_indexed:true,
        ref: 'Category',
        required: 'Le champ "Categorie" ne peut pas etre vide'
    }
});

var CategorySchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please fill Category name',
        trim: true
    },
    ...
    niveau: {
        type: Number
    }
});
4

2 に答える 2

1

これはあなたが探しているものだと思いますhttps://github.com/mongoosastic/mongoosastic/pull/118

var Comment = new Schema({
    title: String,
    body: String,
    author: String
});


var User = new Schema({
    name: {type:String, es_indexed:true},
    email: String,
    city: String,
    comments: {type: Schema.Types.ObjectId, ref: 'Comment', es_schema: Comment, es_indexed:true, es_select: 'title body'}
})

User.plugin(mongoosastic, {
    populate: [
        {path: 'comments', select: 'title body'}
    ]
})

警告:

  1. 正しいマッピングを作成するには、ref スキーマを es_schema に提供する必要があります
  2. デフォルトでは、mongoosastic はスキーマ全体をインデックス化します。特定のフィールドを選択するには、スキーマに es_select フィールドを指定します。
  3. populate 配列は、mongoose Model.populate に渡すのと同じオプションの配列です。
于 2015-12-01T19:51:01.560 に答える