首页 > 解决方案 > 使用nodejs将索引和字段从couchbase复制到elasticsearch

问题描述

我想使用 nodejs 将 couchbase 中可用的文档复制到 elasticsearch。

以下是 couchbase 中可用的索引和字段

const destinationIndexes = {
indexName: 'idx_dest'
fields: ["id", "name"]
options: { ignoreIfExists: true }
}

const testIndexes = {
indexName: 'idx_test',
fields: ["testName", "test", "testId"]
options: { ignoreIfExists: true }
}

const statusIndexes = {
indexName: 'idx_status',
fields: ["statusSchema"]
options: { ignoreIfExists: true }
}

我想用上面提到的字段创建索引(因为 couchbase 有这些字段)。

我只能使用以下代码创建索引

const createIndex = async function(indexName){
    return await client.indices.create({
       index: indexName
    });
}
indexes.forEach((item) =>{
           console.log('..........item'+item)
           client.indices.exists({
             index: item.indexName
           }, function(err, resp) {
             if(err){
              console.log(err);
              return;
             }
             if(!resp){
              console.log(item.indexName + ' does not exist');
              createIndex(item.indexName);
             }
             else {
              console.log(item.indexName + ' already exists');
             }
           })
        })

但我不确定如何创建/添加字段。我尝试添加字段,如下所示

const createIndex = async function(indexName,data){
        return await client.indices.create({
            index: indexName,
            body: data
            
        });
    }

indexes.forEach((item) =>{
           console.log('..........item'+item)
           client.indices.exists({
             index: item.indexName
           }, function(err, resp) {
             if(err){
              console.log(err);
              return;
             }
             if(!resp){
              console.log(item.indexName + ' does not exist');
              createIndex(item.indexName,item.fields);
             }
             else {
              console.log(item.indexName + ' already exists');
             }
           })
        })

它抛出以下错误: StatusCodeError: [not_x_content_exception] 压缩器检测只能在某些 xcontent 字节或压缩的 xcontent 字节上调用

谁能帮我这个?

标签: node.jselasticsearch

解决方案


推荐阅读