node.js - Why is mongoosastic populate / elastic search not populating one of my references? I'm getting an empty object -
i have 2 models i'm attempting reference. style , brand.
brand populates needed object, style empty.
i've tried clearing cache / deleting indexes. , without include_in_parent , type: 'nested'.
i feel may have specified es_type, etc.. not sure.
product schema:
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(){ });
style schema:
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 query:
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'}) } }); };
when query, result in hit:
_source: { name: 'redemption white rye whiskey', brand: [object], style: {},} },
regarding comment request:
product being added db:
exports.create = function(req, res) { product.create(req.body, function(err, product) { if (err) { console.log('err', err) }; res.send({ product: product }); }); };
front / 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 = {}; }); };
i've got working, differs examples given on npm / github.
i had remove es_schema: style, (as had accidentally done brand, why worked). had add es_type: "nested" / es_include_in_parent, gathered elasticsearch , mongoosastic documentation.
i'm not sure intended, seems work:
style: {type: mongoose.schema.types.objectid, ref: 'style', es_type:'nested', es_include_in_parent:true},
i : style: [object] needed, when console.log results.hits .
below example given in npm , did not work me:
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'} ] })
Comments
Post a Comment