2016-06-11 60 views
6

我有兩個模型,我試圖引用。風格和品牌。爲什麼mongoosastic填充/彈性搜索沒有填充我的一個引用?我得到一個空的對象

品牌填充所需的對象,但樣式始終爲空。

我試過清除緩存/刪除索引。帶和不帶include_in_parent並鍵入:'嵌套'。

我覺得它可能與指定的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(){ 
}); 

搜索查詢:

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

前/角:

$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 = {}; 
    }); 
    }; 
+0

你能後的代碼,您使用提交文件?彈性搜索/ mongoosastic查詢的 –

+0

,或者我如何添加產品?我已添加查詢和一些其他信息。 – NoobSter

+0

對不起,我的意思是添加產品 –

回答

3

我找到了工作,但它與這個有很大的不同在npm/github上給出的例子。

我不得不刪除es_schema:Style(正如我爲品牌意外完成的,這就是它的工作原理)。我必須添加從elasticsearch和mongoosastic文檔收集的es_type:「嵌套」/ es_include_in_parent。

我不知道這樣做的目的,但它似乎工作:

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

我現在得到:款式:[對象]根據需要,當我CONSOLE.LOG results.hits。


下面是故宮給出的例子,這並沒有爲我工作:

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'} 
    ] 
}) 
相關問題