简体   繁体   English

在 Elasticsearch 上按顺序重新索引和删除

[英]Reindex and delete in sequence on Elasticsearch

I will need to reindex, delete the old index and rename to the name from the old deleted index, it will execute in a random ordem in NodeJS, trying to delete and rename values doesn't exist.我将需要重新索引,删除旧索引并从旧的已删除索引重命名为名称,它将在 NodeJS 中以随机顺序执行,尝试删除和重命名值不存在。

I did try to run with promises without success and using.then(function(resp) and the same problem happens.我确实尝试使用 promise 运行但没有成功并 using.then(function(resp) 并且发生了同样的问题。

es.reindex({
    refresh: true,
    body: {
        "source": {
            "index": current_index
        },
        "dest": {
            "index": current_index + ".tmp"
        }
    }
});
es.indices.delete({
    index: current_index,
    ignore: [404]
}, function(err, resp, respcode) {
    if (err) {
        console.log("Error deleting index " + current_index);
        console.log("   Error: " + err);
        console.log("   Resp code: " + respcode);
    }
});
es.reindex({
    refresh: true,
    body: {
        "source": {
            "index": current_index + ".tmp"
        },
        "dest": {
            "index": current_index
        }
    }
});
es.indices.delete({
    index: current_index + ".tmp",
    ignore: [404]
}, function(err, resp, respcode) {
    if (err) {
        console.log("Error deleting index " + current_index);
        console.log("   Error: " + err);
        console.log("   Resp code: " + respcode);
    }
});

Here is the right way of doing it:这是正确的做法:

const {reindex1Body} = await es.reindex({
    refresh: true,
    body: {
        "source": {
            "index": current_index
        },
        "dest": {
            "index": current_index + ".tmp"
        }
    }
});

try {
    await es.indices.delete({
        index: current_index
    }, {
        ignore: [404]
    });
} catch (err) {
    console.log("Error deleting index " + current_index);
    console.log("   Error: " + err);
    console.log("   Resp code: " + err.statusCode);
}

const {reindex2Body} = await es.reindex({
    refresh: true,
    body: {
        "source": {
            "index": current_index + ".tmp"
        },
        "dest": {
            "index": current_index
        }
    }
});

try {
    await es.indices.delete({
        index: current_index + ".tmp"
    }, {
        ignore: [404]
    });
} catch (err) {
    console.log("Error deleting index " + current_index);
    console.log("   Error: " + err);
    console.log("   Resp code: " + err.statusCode);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM