Java Code Examples for org.elasticsearch.action.update.UpdateRequest#upsert()

The following examples show how to use org.elasticsearch.action.update.UpdateRequest#upsert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchHighRestFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String upsert(String index,String type,String id,Object json){
		try {
			if(xclient==null){
				init();
			}
//			IndexRequest indexRequest = new IndexRequest(index, type, id).source(json,XContentType.JSON);
//			UpdateRequest updateRequest = new UpdateRequest(index, type, id).doc(json,XContentType.JSON).upsert(indexRequest);
			UpdateRequest request = new UpdateRequest(index, type, id);
			request.upsert(JSON.parseObject(JSON.toJSONString(json)),XContentType.JSON);
			UpdateResponse response = xclient.update(request);
			return response.toString();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
 
Example 2
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void indexTask(Task task) {
    try {
        long startTime = Instant.now().toEpochMilli();
        String id = task.getTaskId();
        TaskSummary summary = new TaskSummary(task);
        byte[] doc = objectMapper.writeValueAsBytes(summary);

        UpdateRequest req = new UpdateRequest(indexName, TASK_DOC_TYPE, id);
        req.doc(doc, XContentType.JSON);
        req.upsert(doc, XContentType.JSON);
        logger.debug("Indexing task document: {} for workflow: {}" + id, task.getWorkflowInstanceId());
        indexObject(req, TASK_DOC_TYPE);
        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for  indexing task:{} in workflow: {}", endTime - startTime, task.getTaskId(), task.getWorkflowInstanceId());
        Monitors.recordESIndexTime("index_task", TASK_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Exception e) {
        logger.error("Failed to index task: {}", task.getTaskId(), e);
    }
}
 
Example 3
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void addMessage(String queue, Message message) {
    try {
        long startTime = Instant.now().toEpochMilli();
        Map<String, Object> doc = new HashMap<>();
        doc.put("messageId", message.getId());
        doc.put("payload", message.getPayload());
        doc.put("queue", queue);
        doc.put("created", System.currentTimeMillis());

        UpdateRequest req = new UpdateRequest(logIndexName, MSG_DOC_TYPE, message.getId());
        req.doc(doc, XContentType.JSON);
        req.upsert(doc, XContentType.JSON);
        indexObject(req, MSG_DOC_TYPE);
        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for  indexing message: {}", endTime - startTime, message.getId());
        Monitors.recordESIndexTime("add_message", MSG_DOC_TYPE, endTime - startTime);
    } catch (Exception e) {
        logger.error("Failed to index message: {}", message.getId(), e);
    }
}
 
Example 4
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void addEventExecution(EventExecution eventExecution) {
    try {
        long startTime = Instant.now().toEpochMilli();
        byte[] doc = objectMapper.writeValueAsBytes(eventExecution);
        String id =
            eventExecution.getName() + "." + eventExecution.getEvent() + "." + eventExecution
                .getMessageId() + "." + eventExecution.getId();

        UpdateRequest req = new UpdateRequest(logIndexName, EVENT_DOC_TYPE, id);
        req.doc(doc, XContentType.JSON);
        req.upsert(doc, XContentType.JSON);
        indexObject(req, EVENT_DOC_TYPE);
        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for indexing event execution: {}", endTime - startTime, eventExecution.getId());
        Monitors.recordESIndexTime("add_event_execution", EVENT_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("logQueue", ((ThreadPoolExecutor) logExecutorService).getQueue().size());
    } catch (Exception e) {
        logger.error("Failed to index event execution: {}", eventExecution.getId(), e);
    }
}
 
Example 5
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void indexTask(Task task) {
    try {
        long startTime = Instant.now().toEpochMilli();
        String id = task.getTaskId();
        TaskSummary summary = new TaskSummary(task);
        byte[] doc = objectMapper.writeValueAsBytes(summary);
        String docType = StringUtils.isBlank(docTypeOverride) ? TASK_DOC_TYPE : docTypeOverride;

        UpdateRequest req = new UpdateRequest(taskIndexName, docType, id);
        req.doc(doc, XContentType.JSON);
        req.upsert(doc, XContentType.JSON);
        indexObject(req, TASK_DOC_TYPE);
        long endTime = Instant.now().toEpochMilli();
        LOGGER.debug("Time taken {} for  indexing task:{} in workflow: {}", endTime - startTime, task.getTaskId(), task.getWorkflowInstanceId());
        Monitors.recordESIndexTime("index_task", TASK_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Exception e) {
        LOGGER.error("Failed to index task: {}", task.getTaskId(), e);
    }
}
 
Example 6
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void addMessage(String queue, Message message) {
    try {
        long startTime = Instant.now().toEpochMilli();
        Map<String, Object> doc = new HashMap<>();
        doc.put("messageId", message.getId());
        doc.put("payload", message.getPayload());
        doc.put("queue", queue);
        doc.put("created", System.currentTimeMillis());

        String docType = StringUtils.isBlank(docTypeOverride) ? MSG_DOC_TYPE : docTypeOverride;
        UpdateRequest req = new UpdateRequest(messageIndexName, docType, message.getId());
        req.doc(doc, XContentType.JSON);
        req.upsert(doc, XContentType.JSON);
        indexObject(req, MSG_DOC_TYPE);
        long endTime = Instant.now().toEpochMilli();
        LOGGER.debug("Time taken {} for  indexing message: {}", endTime - startTime, message.getId());
        Monitors.recordESIndexTime("add_message", MSG_DOC_TYPE, endTime - startTime);
    } catch (Exception e) {
        LOGGER.error("Failed to index message: {}", message.getId(), e);
    }
}
 
Example 7
Source File: ElasticsearchClientTransport.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean updateIndex(IndexMetadata indexMetadata, String data) {
  UpdateRequest updateRequest = new UpdateRequest(indexMetadata.getName(), TYPE, indexMetadata.getId());
  updateRequest.doc(data, XContentType.JSON);
  updateRequest.routing(indexMetadata.getId());

  IndexRequest indexRequest = new IndexRequest(indexMetadata.getName(), TYPE, indexMetadata.getId());
  indexRequest.source(data, XContentType.JSON);
  indexRequest.routing(indexMetadata.getId());

  updateRequest.upsert(indexRequest);

  try {
    UpdateResponse updateResponse = client.update(updateRequest).get();
    return updateResponse.status().equals(RestStatus.OK);
  } catch (InterruptedException | ExecutionException e) {
    log.error("Error updating index '{}'.", indexMetadata.getName(), e);
  }

  return false;
}
 
Example 8
Source File: ElasticsearchClientRest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean updateIndex(IndexMetadata indexMetadata, String data) {
  UpdateRequest updateRequest = new UpdateRequest(indexMetadata.getName(), TYPE, indexMetadata.getId());
  updateRequest.doc(data, XContentType.JSON);
  updateRequest.routing(indexMetadata.getId());

  IndexRequest indexRequest = new IndexRequest(indexMetadata.getName(), TYPE);
  if (indexMetadata.getId() != null && !indexMetadata.getId().isEmpty()) {
    indexRequest.id(indexMetadata.getId());
  }
  indexRequest.source(data, XContentType.JSON);

  updateRequest.upsert(indexRequest);

  try {
    UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
    return updateResponse.status().equals(RestStatus.OK);
  } catch (IOException e) {
    log.error("Error updating index '{}'.", indexMetadata.getName(), e);
  }
  return false;
}
 
Example 9
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
@Override
public void indexWorkflow(Workflow workflow) {
    try {
        long startTime = Instant.now().toEpochMilli();
        String id = workflow.getWorkflowId();
        WorkflowSummary summary = new WorkflowSummary(workflow);
        byte[] doc = objectMapper.writeValueAsBytes(summary);

        UpdateRequest request = new UpdateRequest(indexName, WORKFLOW_DOC_TYPE, id);
        request.doc(doc, XContentType.JSON);
        request.upsert(doc, XContentType.JSON);
        request.retryOnConflict(5);

        new RetryUtil<UpdateResponse>().retryOnException(
            () -> elasticSearchClient.update(request).actionGet(),
            null,
            null,
            RETRY_COUNT,
            "Indexing workflow document: " + workflow.getWorkflowId(),
            "indexWorkflow"
        );

        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for indexing workflow: {}", endTime - startTime, workflow.getWorkflowId());
        Monitors.recordESIndexTime("index_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Exception e) {
        Monitors.error(className, "indexWorkflow");
        logger.error("Failed to index workflow: {}", workflow.getWorkflowId(), e);
    }
}
 
Example 10
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 5 votes vote down vote up
private UpdateRequest buildUpdateRequest(String id, byte[] doc, String indexName, String docType) {
    UpdateRequest req = new UpdateRequest(indexName, docType, id);
    req.doc(doc, XContentType.JSON);
    req.upsert(doc, XContentType.JSON);
    req.retryOnConflict(UPDATE_REQUEST_RETRY_COUNT);
    return req;
}