Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data not being delete from elasticsearch index via mongoosastic?

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);
  }
});
like image 316
DropAcid Avatar asked Sep 16 '15 18:09

DropAcid


1 Answers

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)
}
like image 93
Marcus Lanvers Avatar answered Oct 16 '22 21:10

Marcus Lanvers