Mongoosastic插件不会更新嵌套对象

时间:2018-10-09 08:53:45

标签: node.js mongodb elasticsearch mongoose mongoosastic

因此,该项目由下一个堆栈组成,即NodeJS,Express,Mongodb,ElasticSearch。为了索引到弹性搜索,我们使用了mongoosastic插件。

我有以下模型,它们之间具有1:M的关系,每当更新组时,我也希望在用户对象中也更新组。 因此,如果我更新组,它只会在ElasticSearch的组索引中更新,而不会在用户索引中更新。但是,如果我随后再次更新用户,则可以正确实现组对象。 应该是带有es_type和es_include_in_parent的东西,但无论哪种方式都无法正常工作。

const UserSchema: mongoose.Schema = new mongoose.Schema({
  email: String,
  password: String,
  group: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'groups',
    es_indexed: true,
    es_type: 'nested',
    es_include_in_parent: true
  }
  }, { timestamps: true } as SchemaOptions);

UserSchema.plugin(mongoosastic, {..elasticSearchConfig,
  index: 'users',
  populate: [
    {path: 'group', model: 'groups'}
  ]}
);
export const UserModel = mongoose.model('users', UserSchema, 'users');

export const GroupSchema: mongoose.Schema = extendBaseSchema(new mongoose.Schema({
  name: { type: String, es_indexed: true },
  web: { type: String, es_indexed: true },
  users: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'users',
      es_indexed: true,
      es_type: 'nested',
      es_include_in_parent: true
    }
  ],
}));

GroupSchema.plugin(mongoosastic, {...elasticSearchConfig,
  index: 'groups'
});
export const GroupModel = mongoose.model('groups', GroupSchema, 'groups');

谢谢!

1 个答案:

答案 0 :(得分:0)

添加参数fit(),例如应该可以解决您的问题。

es_schema