Skip to content

Instantly share code, notes, and snippets.

@thesurlydev
Created March 30, 2012 16:02
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/2252482 to your computer and use it in GitHub Desktop.
Save thesurlydev/2252482 to your computer and use it in GitHub Desktop.
indexAttachment
byte[] contentBytes;
String attachmentContent = null;
try {
contentBytes = Streams.copyToByteArray(downloadedFile);
attachmentContent = Base64.encodeBytes(contentBytes);
} catch (IOException e) {
log.error("error reading downloaded file", e);
return false;
}
Map<String, Object> fileProperties = new HashMap<String, Object>();
fileProperties.put("fileID", file.getId());
final int CONTENT_LENGTH_LIMIT = -1;
Map<String, Object> attachment = new HashMap<String, Object>();
attachment.put("content", attachmentContent);
attachment.put("_indexed_chars", CONTENT_LENGTH_LIMIT); // no limit
fileProperties.put("file", attachment);
fileProperties.put("postID", postID);
fileProperties.put("filename", file.getFilename());
fileProperties.put("path", file.getPath());
fileProperties.put("fileSize", file.getFileSize());
/*
setRetryOnConflict - avoids VersionConflictEngineException when a document is being updated by more then
one consumer at a time
*/
try {
new UpdateRequestBuilder(transportClient,
Index.THREAD.getName(),
Index.THREAD.getType(),
String.valueOf(threadID))
.setScript("ctx._source.files += file")
.addScriptParam("file", fileProperties)
.setRetryOnConflict(20)
.execute()
.actionGet();
} catch (ElasticSearchException ese) {
log.error(String.format("error updating threadID %d with attachment fileID %d", threadID, file.getId()), ese);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment