为什么mongoosastic填充/弹性搜索不填充我的参考之一?我得到一个空的对象 [英] Why is mongoosastic populate / elastic search not populating one of my references? I'm getting an empty object

查看:273
本文介绍了为什么mongoosastic填充/弹性搜索不填充我的参考之一?我得到一个空的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个我试图引用的模型。风格和品牌。品牌填充所需的对象,但Style始终为空。



我尝试清除缓存/删除索引。有和没有include_in_parent并且键入:'嵌套'。



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






产品模式:

  var mongoose = require('mongoose'); 
var Schema = mongoose.Schema;
var Style = require('./ style');
var品牌= require('./品牌');
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,{
主机:[
'localhost:9200'
],
填充:[
{path :'style'},
{path:'brand'}
]
});

Product = module.exports = mongoose.model('Product',ProductSchema);

Product.createMapping(function(err,mapping){
if(err){
console.log('错误创建映射(你可以安全地忽略这个))
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错误,结果){
if(err)console.log('ERR',err);
if(results){
var data = results.hits.hits.map(function
res.send(data);
} $ b $(

$)
console.log('product data' b else {
res.send({errmsg:'results not defined'})
}
});
};

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

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









关于评论请求:



添加到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 = {};
});
};


解决方案

我有它的工作,但它有很大的不同从npm / github给出的例子。



我不得不删除es_schema:Style,(正如我不小心为品牌做的,这就是为什么它的工作)。我不得不添加es_type:嵌套/ es_include_in_parent,我从弹性搜索和mongoosastic文档收集。



我不知道这是打算的,但似乎工作:

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

我现在得到:style:[Object] ,当我的console.log结果.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
,注释:{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'}
]
})


I have two models I'm attempting to reference. Style and Brand.

Brand populates with the needed object, but Style is always empty.

i've tried clearing cache / deleting indexes. With and without include_in_parent and type: 'nested'.

I feel it may have something to do with the 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 I query, I get this result in a hit:

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



regarding comment request:

Product being added to 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 it working, but it differs much from the examples given on npm / github.

I had to remove the es_schema: Style, (as I had accidentally done for brand, which was why it worked). I had to add the es_type: "nested" / es_include_in_parent, which I gathered from elasticsearch and mongoosastic documentation.

I'm not sure this is intended, but it seems to work:

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

I now get : style: [Object] as needed, when I console.log results.hits .


Below is the example given in npm , which did not work for 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'}
  ]
})

这篇关于为什么mongoosastic填充/弹性搜索不填充我的参考之一?我得到一个空的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆