2016-06-11 17 views
6

Mam dwa modele, które próbuję odwołać. Styl i marka.Dlaczego populacje mangoastyczne/wyszukiwanie elastyczne nie wypełniają jednego z moich odnośników? Dostaję pusty obiekt

Marka wypełnia wymaganym obiektem, ale styl jest zawsze pusty.

Próbowałem wyczyścić cache/usuwanie indeksów. Z i bez include_in_parent i type: 'nested'.

Czuję, że może to mieć coś wspólnego z określonym typem es_item, itd .. nie jestem pewien. Schemat


produktu: 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(){ 
    }); 

styl:

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

wyszukiwane:

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

Kiedy zapytać, otrzymuję ten wynik w przeboju:

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


dotyczące komentarza życzenie:

dodawanego produktu do DB:

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

przód/kątowa:

$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

można pisać kod użyć do złożenia dokumentu? –

+0

dla zapytania elasticsearch/mongoosastic lub sposobu dodawania produktu? Dodałem zapytanie i dodatkowe informacje. – NoobSter

+0

Przepraszamy, miałem na myśli dodanie produktu –

Odpowiedz

3

Mam go pracy , ale różni się znacznie od przykłady podane na npm/github.

Musiałem usunąć es_schema: Style, (jak to zrobiłem przypadkowo dla marki, i dlatego zadziałało). Musiałem dodać es_type: "nested"/es_include_in_parent, który zebrałem z elastycznej analizy i dokumentacji mongoosastycznej.

nie jestem pewien, że to jest zamierzone, ale wydaje się działać:

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

teraz uzyskać: styl: [Obiekt], ile potrzeba, kiedy console.log results.hits.


Poniżej jest przykład podany w KMP, który nie działa dla mnie:

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