jbdemonte / mongoose-elasticsearch-xp

A mongoose plugin that indexes models into Elasticsearch 2 / 5 / 6 and 7
93 stars 34 forks source link

esSynchronize with populate #45

Open vavarun opened 6 years ago

vavarun commented 6 years ago

Currently this is my setup and I wish to store a value from Invoice mongoose collection to Transaction Elastic document using populate at esSynchronize. I used es_extend to map a new property invoice_number.

const TransactionSchema = new mongoose.Schema(
  {
    currency: { type:  Number },
    amount: { type: Number },

    invoice_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Invoice' },
  },
  {
    timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
    es_extend: {
      invoice_number: {
        es_type: 'string',
        es_value: ''
      },
    },
  }
);
Transaction.find({})
  .populate('invoice_id', 'invoice_number')
  .lean()
  .then((res) => {
    const query = res.map((transaction) => {
      transaction.invoice_number = transaction.invoice_id.invoice_number;
      transaction.invoice_id = transaction.invoice_id._id;
      return transaction;
    });
    console.log(query);
    Transaction.esSynchronize(query)
      .then(() => console.log('Transaction sync end.'))
      .catch((err) => {
        console.log(err);
      });
  });

The query is populated correctly but when I check after the esSync is complete, invoice_number field is still an empty string in the ES document. If someone could assist me