node.js - 为什么 mongoosastic 填充/ Elasticsearch 没有填充我的引用之一?我得到一个空对象

标签 node.js elasticsearch mongoose mongoosastic

我尝试引用两个模型。风格和品牌。

Brand 填充了所需的对象,但 Style 始终为空。

我试过清除缓存/删除索引。使用和不使用 include_in_parent 并键入:'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(){
});

搜索查询:

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

最佳答案

我已经让它工作了,但它与 npm/github 上给出的示例有很大不同。

我不得不删除 es_schema: Style,(因为我不小心为品牌做了,这就是它起作用的原因)。我必须添加 es_type: "nested"/es_include_in_parent,这是我从 elasticsearch 和 mongoosastic 文档中收集的。

我不确定这是有意为之,但它似乎有效:

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

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


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

关于node.js - 为什么 mongoosastic 填充/ Elasticsearch 没有填充我的引用之一?我得到一个空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758652/

相关文章:

multithreading - Lucene如何在多线程环境下使用IndexReader?

javascript - 将自定义实例方法动态添加到 Mongoose 模式

node.js - NPM install 未安装所有组件,node-gyp 失败

node.js - 无法启动 Node 服务器,mongodb 数据库有问题吗?

search - 查询嵌套对象

ruby-on-rails - Searchkick AWS凭证到期

node.js - MongoError : ns not found when try to drop collection

html - Webpack 3.9.1 - 如何将 LESS 文件合并到一个 CSS 文件中?

javascript - Node js、PromiseFtp。如何下载目录中的所有文件然后从ftp中删除它们

node.js - NestJS - 无法将服务注入(inject)订阅者