为什么mongoosastic填充/弹性search不填充我的一个引用? 我得到一个空的对象

我有两个模型,我试图引用。 风格和品牌。

品牌填充所需的对象,但风格总是空的。

我已经尝试清除caching/删除索引。 有和没有include_in_parent并input:'nested'。

我觉得这可能与指定的es_type等有关。不知道。


产品架构:

var mongoose = require('mongoose'); var Schema = mongoose.Schema; var Style = require('./style'); var Brand = require('./brand'); var mongoosastic = require('mongoosastic'); var ProductSchema = new mongoose.Schema({ name: { type: String, lowercase: true , required: true}, brand: {type: mongoose.Schema.Types.ObjectId, ref: 'Brand', es_type:'nested', es_include_in_parent:true}, style: {type: mongoose.Schema.Types.ObjectId, ref: 'Style', es_schema: Style, es_type:'nested', es_include_in_parent: true}, year: { type: Number } }); ProductSchema.plugin(mongoosastic, { hosts: [ 'localhost:9200' ], populate: [ {path: 'style'}, {path: 'brand'} ] }); Product = module.exports = mongoose.model('Product', ProductSchema); Product.createMapping(function (err,mapping) { if(err){ console.log('error creating mapping (you can safely ignore this)'); console.log(err); }else{ console.log('product mapping created!'); console.log(mapping); } }); var stream = Product.synchronize(); var count = 0; stream.on('data', function(){ count++ }); stream.on('close', function(){ console.log('indexed whisks ' + count + " documents"); }); stream.on('error', function(){ }); 

样式模式:

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var mongoosastic = require('mongoosastic'); var StyleSchema = new mongoose.Schema({ name: { type: String, lowercase: true , required: true}, }); Style = module.exports = mongoose.model('Style', StyleSchema); Style.createMapping(function(err, mapping){ if(err) console.log('error w/ mapping : ', err); console.log('mapping created'); console.log(mapping); }) var stream = Style.synchronize(); var count = 0; stream.on('data', function(){ count++ }); stream.on('close', function(){ console.log('indexed styles ' + count + " documents"); }); stream.on('error', function(){ }); 

search查询:

  exports.topSearch = function(req, res) { console.log(req.body, "search product") Product.search({query_string: {query: req.body.search}}, {from: req.body.fromNum, size: req.body.size, hydrate: req.body.hydrate }, function(err, results) { if (err) console.log('ERR', err); if (results){ var data = results.hits.hits.map(function(hit) { return hit }); console.log('product data', data) res.send(data); } else { res.send({errmsg:'results not defined'}) } }); }; 

当我查询时,我得到了这个结果:

 _source: { name: 'Redemption White Rye Whiskey', brand: [Object], style: {},} }, 


关于评论请求:

正在添加到数据库的产品:

 exports.create = function(req, res) { Product.create(req.body, function(err, product) { if (err) { console.log('ERR', err) }; res.send({ product: product }); }); }; 

前/angular:

  $scope.add = function () { var prodStyle = JSON.parse($scope.selectedStyle); $scope.product = $scope.product._id; $scope.product.style = prodStyle._id; console.log($scope.product.style, 'prod style'); Product.create($scope.product).then(function (res) { res.data.product.style = { name: prodStyle.name }; $scope.products.push(res.data.product); $scope.product = {}; $scope.selectedStyle = {}; }); }; 

我有它的工作,但它与npm / github上给出的例子大不相同。

我不得不删除es_schema:Style(正如我不小心为品牌所做的那样,这就是为什么它起作用)。 我不得不添加es_type:“嵌套”/ es_include_in_parent,我从elasticsearch和mongoosastic文档收集。

我不确定这是有意的,但它似乎工作:

 style: {type: mongoose.Schema.Types.ObjectId, ref: 'Style', es_type:'nested', es_include_in_parent:true}, 

我现在得到:style:[Object]根据需要,当我console.log results.hits。


下面是在npm中给出的例子,它并不适用于我:

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