簡體   English   中英

在后台重新編制索引時,應在何處放置重新編制索引的別名?

[英]Where to put index-re-aliasing when re-indexing in the background?

我嘗試用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啟動期間運行),則將重設別名並刪除舊索引。

當且僅當所有批量請求均成功時,如何才能重新設置別名?

您無需等待批量請求完成。 如果不帶actionGet()調用execute(),則最終將異步運行。 這意味着您將在完全構建新索引之前開始更改別名並刪除索引。

也:

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

這應該以execute()。actionGet()而不是get()結尾。 這可能就是為什么未設置別名的原因

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM