String newName = oldName + "_" + Utils.timestamp();
logger.info("rebuildIndex(): {}", appid);
BulkRequestBuilder brb = getClient().prepareBulk();
BulkResponse resp = null;
Pager pager = new Pager();
List<ParaObject> list = dao.readPage(appid, null);
if (!list.isEmpty()) {
do {
for (ParaObject obj : list) {
brb.add(getClient().prepareIndex(appid, obj.getType(), obj.getId()).
setSource(Utils.getAnnotatedFields(obj)));
pager.setLastKey(obj.getId());
}
// bulk index 1000 objects
if (brb.numberOfActions() > 100) {
resp = brb.execute().actionGet();
logger.info("rebuildIndex(): indexed {}, hasFailures: {}",
brb.numberOfActions(), resp.hasFailures());
}
} while(!(list = dao.readPage(appid, pager)).isEmpty());
}
// anything left after loop? index that too
if (brb.numberOfActions() > 0) {
resp = brb.execute().actionGet();
logger.info("rebuildIndex(): indexed {}, hasFailures: {}",
brb.numberOfActions(), resp.hasFailures());
}
// switch to alias NEW_INDEX -> ALIAS, OLD_INDEX -> X
getClient().admin().indices().prepareAliases().
addAlias(newName, appid).