因此,该项目由下一个堆栈组成:NodeJS,Express,Mongodb,ElasticSearch。为了索引到 flex 搜索,我们使用了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');

提前致谢!

最佳答案

添加参数es_schema例如应该可以解决您的问题。

es_schema:User.schema

关于node.js - Mongoosastic插件不会更新嵌套对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52717000/

10-11 09:17