Mongoose 深填充 Mongoosastic

Mongoose deep populate with Mongoosastic

我已经阅读了许多 mongoose deep populate 文档和文章,但在这种情况下我似乎无法让它工作,但我有一种感觉,因为在存储数据时,dealSchema 没有 id。想法?

由于这里的目标是使用 mongoosastic 并在 elasticsearch 中索引信息,我将使用插件传递填充参数。

//mongoose version 4.8.0

var dealSchema = new Schema({
    deal: {type: Schema.ObjectId, ref: 'Deal'},
    discounts: [discountSchema]
}, {_id:false});


var offerSchema = new Schema({
    name: String,
    description: String,
    partner: {type: Schema.ObjectId, ref: 'Partner'},
    venues: [{type: Schema.ObjectId, ref: 'Venue'}],
    deals: [dealSchema],
    modified: Date,
    created: Date
});


//=================================================//
mongoose.connect('mongodb://127.0.0.1:27017/production');
offerSchema.plugin(mongoosastic, {
  hosts: [ 'http://127.0.0.1:9200' ],
    index: 'myIndex',
    type: 'offer',
    populate: [
        { path: 'partner', model: 'Partner', select: 'name' },
        { path: 'venues', model: 'Venue', select: 'name' },
        { path: 'deals.deal', model: 'Deal', select: 'name' }
    ]
});

///////// indexed data in ES ///////////
// 1. partners and venues are populated as expected, name only.
// 2. deals.deal is not populated as intended

{
  "_index": "myIndex",
  "_type": "offer",
  "_id": "568454a104439f0300a57e86",
  "_score": 2.1182644,
  "_source": {
    "name": "offer name",
    "description": "This is an offer.",
    "partner": {
      "_id": "566ddbf61e11f0030020fcc5",
      "name": "partner one"
    },
    "venues": [
      {
        "_id": "566df15d1e11f00300211c13",
        "name": "venue one"
      },
      {
        "_id": "566df2f1b6e09103003595a6",
        "name": "venue two"
      }
    ],
    "deals": [
      {
        "deal": "566df755b6e091030035cbed",
        "discounts": [
          {
            "amount": 0,
            "attribute": "566ddeff1e11f0030020fccf"
          },
          {
            "amount": 0,
            "attribute": "566ddef91e11f0030020fcce"
          }
        ]
      }
    ],
    "modified": "2016-05-31T23:04:55.476Z",
    "created": "2015-12-30T22:03:13.354Z"
  }
}

(代表OP发帖).

以这种方式填充,实际上按预期填充了 deals.deal。因此,这可能是声明式填充时的 mongoosastic 问题。

Offer.findOne({'_id': '568454a104439f0300a57e86'}).populate('deals.deal').exec(function(err, doc){
    console.log(JSON.stringify(doc));
});

下面的代码有效。我需要在重新 运行.

之前删除我的 elasticsearch 索引
mongoose.connect('mongodb://127.0.0.1:27017/production');
    offerSchema.plugin(mongoosastic, {
      hosts: [ 'http://127.0.0.1:9200' ],
        index: 'myIndex',
        type: 'offer',
        populate: [
            { path: 'partner', model: 'Partner', select: 'name' },
            { path: 'venues', model: 'Venue', select: 'name' },
            { path: 'deals.deal', model: 'Deal', select: 'name' }
        ]
    });