Skip to content

Instantly share code, notes, and snippets.

Created February 26, 2012 14:47
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 anonymous/56a1587a8384fc5ca364 to your computer and use it in GitHub Desktop.
Save anonymous/56a1587a8384fc5ca364 to your computer and use it in GitHub Desktop.
creating index source
private void index() throws Exception {
IndexRequest indexRequest = createIndexRequest(index, type);
LOGGER.info("Going to create index for {}", indexRequest.id());
final StopWatch stopWatch = new Slf4JStopWatch("ESIndexer.index...",
LOGGER);
client.index(indexRequest);
client.admin().indices().refresh(new RefreshRequest(index));
stopWatch.stop();
}
private IndexRequest createIndexRequest(String index, String type) throws Exception {
LOGGER.trace("Entering call {}",
Thread.currentThread().getStackTrace()[1].getMethodName());
// get some mock doc in json format
String source = JSONVelocityGenerator.generateResource();
String id = JSONVelocityGenerator.id(source);
if (id == null || id.isEmpty()) {
throw new Exception("Id cannot be null");
}
IndexRequest indexRequest = new IndexRequest();
indexRequest.index(index);
indexRequest.type(type);
indexRequest.source(source);
indexRequest.id(id);
LOGGER.trace("Exiting call {}",
Thread.currentThread().getStackTrace()[1].getMethodName());
return indexRequest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment