首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在后台重新建立索引时,将索引重新混叠放在哪里?

在后台重新建立索引时,将索引重新混叠放在哪里?
EN

Stack Overflow用户
提问于 2014-08-29 00:18:01
回答 1查看 62关注 0票数 0

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

代码语言:javascript
复制
// 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启动期间运行),则会重新设置别名,并且仍然会删除旧的索引。

如果且仅当所有批量请求都成功时,我如何才能重新设置别名?

EN

回答 1

Stack Overflow用户

发布于 2014-08-29 07:13:16

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

另外:

代码语言:javascript
复制
client.admin().indices().aliases(new IndicesAliasesRequest().removeAlias(currentIndexName, "my_index").addAlias("my_index", newIndexName)).get();

这应该以execute()和.actionGet()结束,而不是get()。这可能就是为什么没有设置别名的原因

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25553494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档