我在MEAN stack程序中使用了mongoosastic设置。一切正常,除了我从mongodb中删除一个文档时,它没有在elasticsearch索引中被删除。因此,每次我执行包含删除项目的搜索时,都会返回已删除的项目,但当它被加水时,它将为null。mongoosastic是否处理从ES索引中删除?我必须编写索引刷新程序吗?
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);
}
});发布于 2015-11-19 04:05:12
我通过改变删除数据的方式解决了这个问题。
我使用的是:
Quantum.findByIdAndRemove(quantumid)我将其切换为:
Quantum.findById(quantumid, function(err, quantum) {
quantum.remove(function(err, quantum) {
if (err) {
console.log(err);
return;
}
});
});我没有研究这种工作的原因,但它解决了问题,我继续前进。
发布于 2016-01-09 14:50:22
我不知道你使用的是哪个版本的mongoosastic,但我使用的是mongoosastic@3.6.0,每当我使用Model.findByIdAndRemove或Model.remove删除我的索引文档时,它就会被删除。因此,试着用删除文档的方式来交叉检查。
发布于 2021-06-27 02:24:43
我也犯了同样的错误。如果您查看Documentation,就会发现您必须在删除文档后显式删除该文档。这就是我现在做删除的方式。
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)
}https://stackoverflow.com/questions/32615918
复制相似问题