首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mongoosastic中将嵌套文档作为对象获取

如何在mongoosastic中将嵌套文档作为对象获取
EN

Stack Overflow用户
提问于 2016-01-30 17:33:13
回答 1查看 1.5K关注 0票数 0

我有一个带有mongoosastic的nodejs服务器,尝试将嵌套的搜索结果作为对象来获取,而不仅仅是索引。

这是我的代码:

代码语言:javascript
复制
require('../server/serverInit');


var elasticsearch = require('elasticsearch');
var esclient = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'trace'
});


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

var elasticsearch = require('elasticsearch');
var esclient = new elasticsearch.Client({
    host: '127.0.0.1:9200',
    log: 'trace'
});
global.DBModel = {};
/**
 * StoreSchema
 * @type type
 */

var storeSchema = global.mongoose.Schema({
    Name: {type: String, es_indexed: true},
    Email: {type: String, es_indexed: true},
   .....
    _articles: {type: [articleSchema],
        es_indexed: true,
        es_type: 'nested',
        es_include_in_parent: true}
});

/**
 * ArtikelSchema
 * @type Schema
 */

var articleSchema = new Schema({       
    Name: {type: String, es_indexed: true},
    Kategorie: String,
    ....
    _stores: {type: [storeSchema],
        es_indexed: true,
        es_type: 'nested',
        es_include_in_parent: true}
});

storeSchema.plugin(mongoosastic, {
    esClient: esclient
});
articleSchema.plugin(mongoosastic, {
    esClient: esclient
});
global.DBModel.Artikel = global.mongoose.model('Artikel', articleSchema);

global.DBModel.Store = global.mongoose.model('Store', storeSchema);

当我现在从路由"/search“启动搜索时,它具有以下示例代码:

代码语言:javascript
复制
global.DBModel.Artikel.search({
                    query_string: {
                        query: "*"
                    }
                }, {
                    hydrate: true
                }, function (err, results) {
                    if (err)
                        return res.send(500, {error: err});
                    res.send(results);
                }); 

我得到的结果是:

代码语言:javascript
复制
...
      {
        "_id": "56ab6b15352a43725a21bc92",
        "stores": [
          "56ab6b03352a43725a21bc91"
        ],
        "Name": "daaadd",
        "ArtikelNummer": "232",
        "__v": 0,
        "_stores": []
      }
    ]
  }
}

如何直接获取对象而不是id "56ab6b03352a43725a21bc91"?

EN

回答 1

Stack Overflow用户

发布于 2016-04-24 01:28:07

我必须显式地在插件选项中添加填充选项,以索引填充的嵌套文档。在你的例子中,像这样定义mongoosastic插件可能会起作用:

代码语言:javascript
复制
storeSchema.plugin(mongoosastic, {
    esClient: esclient,
    populate: [
        { path: '_articles', select: '_id Name Kategorie' }
    ]
});
articleSchema.plugin(mongoosastic, {
    esClient: esclient,
    populate: [
        { path: '_stores', select: '_id Name Email' }
    ]
});

此外,您还应该指定es_schema内部字段选项,如下所示:

代码语言:javascript
复制
var articleSchema = new Schema({       
    Name: {type: String, es_indexed: true},
    Kategorie: String,
    ....
    _stores: {type: [storeSchema],
        es_indexed: true,
        es_type: 'nested',
        es_include_in_parent: true,
        es_schema: storeSchema
   }
});

请参阅此处的示例:https://github.com/mongoosastic/mongoosastic#indexing-mongoose-references

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35100129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档