public ResultMessage get(String messageId, String index) throws DocumentNotFoundException, IOException { final Get get = new Get.Builder(index, messageId).type(IndexMapping.TYPE_MESSAGE).build(); final DocumentResult result = client.execute(get); if (!result.isSucceeded()) { throw new DocumentNotFoundException(index, messageId); } @SuppressWarnings("unchecked") final Map<String, Object> message = (Map<String, Object>) result.getSourceAsObject(Map.class, false); return ResultMessage.parseFromSource(result.getId(), result.getIndex(), message); }
private static void checkExistenceOfDocument(Get request, DocumentResult dataOfDocumentResponse) { if (!dataOfDocumentResponse.isSucceeded()) { throw FailureHandler.createFailure( "Document with index: %s - type: %s - id: %s has not returned any document.", request.getIndex(), request.getType(), request.getId()); } }
/** * Cache a result * * @param content */ private void cacheResult(final String content) { final JestClient client = getElasticSearchClient(); if (client != null) { final Index contentIndex = new Index.Builder(content).index(elasticIndex).type("logEntry").build(); try { final DocumentResult result = client.execute(contentIndex); log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded()); } catch (IOException e) { log.error("Error indexing content {}: {}", content, e.getMessage(), e); } //TODO: move to async at some point /*client.executeAsync(contentIndex, new JestResultHandler<JestResult>() { @Override public void completed(JestResult result) { log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded()); } @Override public void failed(Exception e) { log.error("Error indexing content {}: {}", content, e.getMessage(), e); } });*/ } else { log.warn("Content {} won't be cached, there is not target bucket", content); } }
public ResultMessage get(String messageId, String index) throws DocumentNotFoundException, IOException { final Get get = new Get.Builder(index, messageId).type(IndexMapping.TYPE_MESSAGE).build(); final DocumentResult result = client.execute(get); if (!result.isSucceeded()) { throw new DocumentNotFoundException(index, messageId); } @SuppressWarnings("unchecked") final Map<String, Object> message = (Map<String, Object>) result.getSourceAsObject(Map.class, false); return ResultMessage.parseFromSource(result.getId(), result.getIndex(), message); }
/** * This executes single Elasticsearch actions, creating indices as needed. * * @param client * @param action * @throws IOException */ private static void executeSingleAction(JestClient client, BulkableAction<DocumentResult> action) throws IOException { DocumentResult result = client.execute(action); if(result == null || result.getResponseCode() == 404){ // index doesn't exist for upsert command so create new index and try again if(LOG.isDebugEnabled()) { if (result == null) { logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), null, -1, null); } else { logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage()); } LOG.debug("index name "+action.getIndex() + " doesn't exist, creating new index"); } createIndex(client, action.getIndex(), action.getType()); result = client.execute(action); } if (result == null) { logEsError(action.getRestMethodName(), action.getIndex(), action.getType(), null, -1, null); } else if (!result.isSucceeded()){ logEsError(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage()); } else if(LOG.isDebugEnabled()) { logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage()); } }