Created
October 24, 2011 17:56
-
-
Save thesurlydev/1309649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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