【问题标题】:Ingnore index if already exist and create/add new index in elasticsearch with nodejs如果已经存在则忽略索引并使用 nodejs 在 elasticsearch 中创建/添加新索引
【发布时间】:2021-11-30 06:45:45
【问题描述】:

我是 elasticsearch 和 couchbase 的新手,我想使用 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 }
}

我尝试使用以下代码在 elasticsearch 中创建类似的索引

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

indexes.forEach((item) =>{
    console.log('..........item'+item)
    const resp =  createIndex(item.indexName);
    console.log('...........resp..............'+JSON.stringify(resp))
})

我可以创建索引,但是如果我重新运行代码,它会显示错误: [resource_already_exists_exception] 索引 [idx_dest/0iR-fZLdSty0oLVaQhNTXA] 已经存在,{ index_uuid="0iR-fZLdSty0oLVaQhNTXA" & index="idx​​_dest" }

我希望它忽略现有索引并添加新索引(如果有)。

谁能帮帮我?

【问题讨论】:

    标签: node.js elasticsearch


    【解决方案1】:

    您只想在索引尚不存在时创建它。这样做 在使用client.indices.exists() 创建它之前首先检查它是否存在。

    所以可以修改createIndex()

    const createIndex = async function(indexName){
        if(await client.indices.exists({index: indexName}) {
            // returning false since no index was created.. 
            console.log('Index', indexName, 'does already exist')
            return false
        }
        return await client.indices.create({
           index: indexName
        });
    }
    
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2013-09-08
      相关资源
      最近更新 更多