首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongoosastic不会在保存时建立索引

Mongoosastic不会在保存时建立索引
EN

Stack Overflow用户
提问于 2019-02-06 23:08:09
回答 1查看 657关注 0票数 0

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

代码语言:javascript
复制
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。

我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2019-10-10 09:00:52

我为此苦苦挣扎了一段时间,直到我发现这个问题与ElasticSearch从v5升级到v6有关,其中“类型”成为弃用的候选(在v7中完全删除),并且不再允许超过一种类型。该库很可能会强制使用它自己的“类型”(在您的例子中,除了"customer“之外,还很可能是"_doc”),但我们需要手动将mongoosastic中的一个类型设置为“_doc”(这对于ElasticSearch v6是必要的)。

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

然后,在您的模型中,更改

代码语言:javascript
复制
custSchema.plugin(mongoosastic);

代码语言:javascript
复制
custSchema.plugin(mongoosastic, {
  type: '_doc'
});

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

代码语言:javascript
复制
stream.on("data", function(err, doc) {
  client.indices.create({
    index: 'customers',
    body: {
      doc
    }
  }, function (error, response) {
    console.log(response);
  });

是一个很难解决的问题,希望这也能帮助其他一些人避免头痛。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54556691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档