2016-02-03 15 views
9

私の目標はmongoosastic検索で特定のフィールドを移入することですが、私は以下のコードをすれば、それは常にmongoosasticに入力するにはどうすればよいですか?

enter image description here

を返します。ここでは、コード

var mongoose = require('mongoose'); 
var Category = require('./category'); 
var mongoosastic = require('mongoosastic'); 
var Schema = mongoose.Schema; 

var ProductSchema = new Schema({ 
    category: { type: Schema.Types.ObjectId, ref: 'Category', es_schema: Category, es_indexed:true, es_select: 'name'}, 
    name: String, 
    price: Number, 
    image: String 
}); 

ProductSchema.plugin(mongoosastic, { 
    hosts: [ 
    'localhost:9200' 
    ], 
    populate: [ 
    {path: 'category', select: 'name'} 
    ] 
}); 

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


var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var CategorySchema = new Schema({ 
    name: { type: String, unique: true, lowercase: true} 
}); 

module.exports = mongoose.model('Category', CategorySchema); 

api.js

です
router.post('/search', function(req, res, next) { 
    console.log(req.body.search_term); 
    Product.search({ 
    query_string: { query: req.body.search_term } 
    }, function(err, results) { 
    if (err) return next(err); 
    res.json(results); 
    }); 
}); 

mongoosasticに特定のカテゴリを設定するにはどうすればよいですか?あなたはあなたのコードで

populate: [ 
    {path: 'categories', select: 'name'} 
] 

しかしcategoriesを使用している

+0

これはうまくいきましたか?私は居住するときに0ヒットを得る同様の問題があります。私は何度もドキュメントを読んだことがありますが、どこが間違っているのか分かりません。あなたは洞察力を提供することができます:http://stackoverflow.com/questions/37758652/mongoosastic-elastic-search-will-not-return-any-hits-when-populating – someoneHere

+0

この問題を解決しますか? – Mikhail

答えて

0

undefinedです。

あなたはスキーマ宣言

にカテゴリとしてキーを を述べてきたようcategoriesの代わりにカテゴリを使用する必要がありますが、それはあなたの問題を解決することを願っています。

+0

カテゴリに変更しましたが、機能しませんでした。 –

-1

編集:

カテゴリ:{タイプ:Schema.Types.ObjectId、REF: 'カテゴリー'、es_schema:カテゴリー、es_indexed:真、es_select: '名前'}、

=> _category:{タイプ:Schema.Types.ObjectId、REF: 'カテゴリー'}、

そして移入を使用します。.populate( '_カテゴリ'、 '名前')

は、それはあなたを助けることを願っています。

もっと見る:http://mongoosejs.com/docs/populate.html

+0

これはmongooseに関してだけ話しています。 –

関連する問題