Data not being delete from elasticsearch index via mongoosastic?
Asked Answered
P

3

6

I have mongoosastic setup within a MEAN stack program. Everything works correctly except when I delete a document from mongodb it is not deleted in the elasticsearch index. So every time I do a search that includes delete items, the deleted item is returned but is null when it is hydrated. Does mongoosastic handle deleting from the ES index? Do I have to program an index refresh?

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

var quantumSchema = new mongoose.Schema({
    note: {
        type: String,
        require: true,
        es_indexed: true
   }        
});

quantumSchema.plugin(mongoosastic);

var Quantum = mongoose.model('Quantum', quantumSchema);

Quantum.createMapping(function(err, mapping){
  if(err){
    console.log('error creating mapping (you can safely ignore this)');
    console.log(err);
  }else{
    console.log('mapping created!');
    console.log(mapping);
  }
});
Piotr answered 16/9, 2015 at 18:21 Comment(1)
Have you had any luck with this since you posted? I am having the same problem.Intaglio
S
1

I had the same error. If you look in the Documentation it states that you have to explicit remove the document after deleting it. This is the way i am doing a deletion now.

const deleteOne = Model => async (id)=> {
const document = await Model.findByIdAndDelete(id);

if (!document) {
    return new Result()
    .setSuccess(false)
    .setError('Unable to delete Entity with ID: ' + id + '.')
}
//this ensures the deletion from the elasticsearch index
document.remove();
return new Result()
.setSuccess(true)
.setData(document)
}
Sappanwood answered 26/6, 2021 at 18:24 Comment(0)
P
0

I solved the problem by changing the way I delete the data.

I was using:

  Quantum.findByIdAndRemove(quantumid)

I switched it to:

  Quantum.findById(quantumid, function(err, quantum) {
      quantum.remove(function(err, quantum) {
         if (err) {
            console.log(err);

            return;
         }                
       });
   });

I did not research the reason for this working, but it solved the problem and I moved on.

Piotr answered 18/11, 2015 at 20:5 Comment(0)
S
0

I dont know what version of mongoosastic you're using but i use [email protected] and my indexed doc get deleted whenever i remove it either using Model.findByIdAndRemove or Model.remove. Therefore try to cross check the way you delete you're docs.

Sheng answered 9/1, 2016 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.