【问题标题】:Mongoosastic Does not Index On SaveMongoosastic 不保存索引
【发布时间】:2019-06-30 14:05:32
【问题描述】:

我有一个这样定义的猫鼬模型。

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。

我该如何解决这个问题?

【问题讨论】:

标签: node.js elasticsearch mongoose mongoosastic


【解决方案1】:

为此苦苦挣扎了一段时间,直到我发现这个问题与从 v5 到 v6 的 ElasticSearch 升级有关,其中“类型”成为弃用的候选对象(在 v7 中完全删除)并且不再允许多个类型。该库很可能会强制使用自己的“类型”(在您的情况下可能是“客户”之外的“_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);
  });

解决起来很痛苦,希望这也能帮助其他一些人避免头痛。

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 2018-09-22
    相关资源
    最近更新 更多