我有一个这样定义的mongoose模型。
const custSchema = new mongoose.Schema({
name: {
type: String,
es_indexed: true,
es_type: text
},
phoneNumber: {
type: String,
es_indexed: true,
es_type: String
},
email: String
})
custSchema.plugin(mongoosastic);
const Cust = module.exports = mongoose.model('Cust', custSchema);
Cust.createMapping(function(err, mapping) {
if(err) {
console.log(err)
} else {
console.log(mapping);
}
});
let count = 0;
const stream = Cust.synchronize();
stream.on('data', () => {
count = count + 1;
})
stream.on('close', () => {
console.log("Total " + count + " documents indexed");
})
stream.on('error', (err) => {
console.log(err)
});当我向Cust添加新集合时,除非重新启动服务器,否则不会将新文档添加到elasticsearch。
我该如何解决这个问题?
发布于 2019-10-10 09:00:52
我为此苦苦挣扎了一段时间,直到我发现这个问题与ElasticSearch从v5升级到v6有关,其中“类型”成为弃用的候选(在v7中完全删除),并且不再允许超过一种类型。该库很可能会强制使用它自己的“类型”(在您的例子中,除了"customer“之外,还很可能是"_doc”),但我们需要手动将mongoosastic中的一个类型设置为“_doc”(这对于ElasticSearch v6是必要的)。
首先,删除索引和类型可能导致冲突的原始索引。
然后,在您的模型中,更改
custSchema.plugin(mongoosastic);至
custSchema.plugin(mongoosastic, {
type: '_doc'
});并在插件部分中更改此选项后创建一个新索引
stream.on("data", function(err, doc) {
client.indices.create({
index: 'customers',
body: {
doc
}
}, function (error, response) {
console.log(response);
});是一个很难解决的问题,希望这也能帮助其他一些人避免头痛。
https://stackoverflow.com/questions/54556691
复制相似问题