6

我在 MEAN 堆栈程序中有 mongoosastic 设置。一切正常,除了当我从 mongodb 中删除一个文档时,它没有在弹性搜索索引中删除。因此,每次我进行包含删除项目的搜索时,都会返回已删除的项目,但在水合时为空。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);
  }
});
4

3 回答 3

0

我通过改变删除数据的方式解决了这个问题。

我正在使用:

  Quantum.findByIdAndRemove(quantumid)

我将其切换为:

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

            return;
         }                
       });
   });

我没有研究这个工作的原因,但它解决了问题,我继续前进。

于 2015-11-18T20:05:11.677 回答
0

我不知道您使用的是什么版本的 mongoosastic,但我使用 mongoosastic@3.6.0 并且每当我使用Model.findByIdAndRemove或删除它时,我的索引文档都会被删除Model.remove。因此,请尝试交叉检查您删除文档的方式。

于 2016-01-09T06:50:22.363 回答
0

我有同样的错误。如果您查看文档,它指出您必须在删除文档后显式删除文档。这就是我现在进行删除的方式。

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)
}
于 2021-06-26T18:24:42.910 回答