最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Monososaostic在保存时未编制索引

IT培训 admin 4浏览 0评论

Monososaostic在保存时未编制索引

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

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中。

我该如何解决这个问题?

回答如下:

经过一段时间的努力,直到我发现问题与ElasticSearch从v5升级到v6有关,“类型”成为弃用的候选对象(在v7中完全删除),并且不再允许使用多个类型。该库很可能会强制使用自己的“类型”(在您的情况下,可能是“客户”之外还有“ _doc”),但是我们需要手动将Monososastic中的一种类型设置为“ _doc”(对于ElasticSearch v6是必需的)。

首先,删除可能导致索引和类型冲突的原始索引。

然后,在您的模型中更改

custSchema.plugin(mongoosastic);

to

custSchema.plugin(mongoosastic, {
  type: '_doc'
});

并在插件部分更改此选项后创建新索引

stream.on("data", function(err, doc) {
  client.indices.create({
    index: 'customers',
    body: {
      doc
    }
  }, function (error, response) {
    console.log(response);
  });

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

Monososaostic在保存时未编制索引

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

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中。

我该如何解决这个问题?

回答如下:

经过一段时间的努力,直到我发现问题与ElasticSearch从v5升级到v6有关,“类型”成为弃用的候选对象(在v7中完全删除),并且不再允许使用多个类型。该库很可能会强制使用自己的“类型”(在您的情况下,可能是“客户”之外还有“ _doc”),但是我们需要手动将Monososastic中的一种类型设置为“ _doc”(对于ElasticSearch v6是必需的)。

首先,删除可能导致索引和类型冲突的原始索引。

然后,在您的模型中更改

custSchema.plugin(mongoosastic);

to

custSchema.plugin(mongoosastic, {
  type: '_doc'
});

并在插件部分更改此选项后创建新索引

stream.on("data", function(err, doc) {
  client.indices.create({
    index: 'customers',
    body: {
      doc
    }
  }, function (error, response) {
    console.log(response);
  });

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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论