Tec*_*arn 2 node.js elasticsearch
我是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 }
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用下面的代码在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))
})
Run Code Online (Sandbox Code Playgroud)
我可以创建索引,但如果重新运行代码,它会显示错误: [resource_already_exists_exception] 索引 [idx_dest/0iR-fZLdSty0oLVaQhNTXA] 已存在,其中 { index_uuid="0iR-fZLdSty0oLVaQhNTXA" & index="idx_dest" }
我希望它忽略现有索引并添加新索引(如果有)。
有人可以帮我吗?
您只想在索引尚不存在时创建索引。通过在使用创建它之前首先检查它是否存在来执行此操作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
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2156 次 |
| 最近记录: |