mongoose-elasticsearch-xp
mongoose-elasticsearch-xp copied to clipboard
esSynchronize with populate
trafficstars
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