在后台重新索引时放置索引重新别名的位置?

时间:2014-08-28 16:18:01

标签: java elasticsearch

我尝试用Java重新索引ES索引:

// reindex all documents from the old into the new index
QueryBuilder qb = QueryBuilders.matchAllQuery();
SearchResponse scrollResp = client.prepareSearch("my_index").setSearchType(SearchType.SCAN).setScroll(new TimeValue(600000)).setQuery(qb).setSize(100).execute().actionGet();
while (true) {
    scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();

    final int documentFoundCount = scrollResp.getHits().getHits().length;

    // Break condition: No hits are returned
    if (documentFoundCount == 0) {
        break;
    }

    // otherwise add all documents which are found (in this scroll-search) to a bulk operation for reindexing.
    logger.info("Found {} documents in the scroll search, re-indexing them via bulk now.", documentFoundCount);
    BulkRequestBuilder bulk = client.prepareBulk();
    for (SearchHit hit : scrollResp.getHits()) {
        bulk.add(new IndexRequest(newIndexName, hit.getType()).source(hit.getSource()));
    }

    bulk.execute(new ActionListener<BulkResponse>() {
        @Override public void onResponse(BulkResponse bulkItemResponses) {
            logger.info("Reindexed {} documents from '{}' to '{}'.", bulkItemResponses.getItems().length, currentIndexName, newIndexName);
        }

        @Override public void onFailure(Throwable e) {
            logger.error("Could not complete the index re-aliasing.", e);
        }
    });
}

// these following lines should only be executed if the re-indexing was successful for _all_ documents.
logger.info("Finished re-indexing all documents, now setting the aliases from the old to the new index.");
try {
    client.admin().indices().aliases(new IndicesAliasesRequest().removeAlias(currentIndexName, "my_index").addAlias("my_index", newIndexName)).get();
    // finally, delete the old index
    client.admin().indices().delete(new DeleteIndexRequest(currentIndexName)).actionGet();
} catch (InterruptedException | ExecutionException e) {
    logger.error("Could not complete the index re-aliasing.", e);
}

一般情况下,这有效,但方法有一个问题:

如果在重新编制索引期间出现故障,例如它花了太长时间并被一些事务监视器停止(它在EJB启动期间运行),别名被重新设置,旧的索引仍被删除。

当且仅当所有批量请求都成功时,如何进行别名重新设置?

1 个答案:

答案 0 :(得分:0)

在批量请求完成之前,您不会等待。如果在没有actionGet()的情况下调用execute(),则最终会异步运行。这意味着您将在完全构建新索引之前开始更改别名和删除索引。

此外:

client.admin().indices().aliases(new IndicesAliasesRequest().removeAlias(currentIndexName, "my_index").addAlias("my_index", newIndexName)).get();

这应该以execute()。actionGet()而不是get()结束。这可能就是你的别名没有设置的原因