Skip to content

Instantly share code, notes, and snippets.

@thesurlydev
Created October 24, 2011 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesurlydev/1309649 to your computer and use it in GitHub Desktop.
Save thesurlydev/1309649 to your computer and use it in GitHub Desktop.
@Override
public int reIndexPosts(int minPostID, int maxPostID) {
if (minPostID < 1 || maxPostID < 1 || (minPostID > maxPostID)) {
log.warn("invalid args, skipping re-indexing of posts; minPostID=" + minPostID + ", maxPostID=" + maxPostID);
return 0;
}
List<Post> posts = getPosts(minPostID, maxPostID);
if (posts == null || posts.isEmpty()) return 0;
TransportClient client = clientFactory.getClient();
int reIndexCount = 0;
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (Post post : posts) {
bulkRequest.add(
client.prepareIndex(
Index.POST.getName(),
Index.POST.getType(), String.valueOf(post.getPostID())).setSource(getPostContentBuilder(post))
);
++reIndexCount;
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
for (BulkItemResponse item : bulkResponse.items()) {
if (item.isFailed()) {
--reIndexCount;
log.warn("Post re-indexing error: " + item.getFailureMessage());
}
}
}
client.close();
log.info("re-indexed " + reIndexCount + " posts in " + bulkResponse.getTookInMillis() + "ms");
countDocs(Index.POST.getName());
return reIndexCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment