@Override public UpdateRequest createUpdateRequest( String index, String docType, String key, XContentType contentType, byte[] document) { return new UpdateRequest(index, docType, key) .doc(document, contentType) .upsert(document, contentType); }
private UpdateRequest prepareUpdate(UpdateQuery query) { String indexName = hasText(query.getIndexName()) ? query.getIndexName() : getPersistentEntityFor(query.getClazz()).getIndexName(); String type = hasText(query.getType()) ? query.getType() : getPersistentEntityFor(query.getClazz()).getIndexType(); Assert.notNull(indexName, "No index defined for Query"); Assert.notNull(type, "No type define for Query"); Assert.notNull(query.getId(), "No Id define for Query"); Assert.notNull(query.getUpdateRequest(), "No IndexRequest define for Query"); UpdateRequest updateRequest = new UpdateRequest(indexName, type, query.getId()); updateRequest.routing(query.getUpdateRequest().routing()); if (query.getUpdateRequest().script() == null) { // doc if (query.DoUpsert()) { updateRequest.docAsUpsert(true).doc(query.getUpdateRequest().doc()); } else { updateRequest.doc(query.getUpdateRequest().doc()); } } else { // or script updateRequest.script(query.getUpdateRequest().script()); } return updateRequest; }
private static UpdateRequest createUpdateRequest(Tuple2<String, String> element, ParameterTool parameterTool) { Map<String, Object> json = new HashMap<>(); json.put("data", element.f1); return new UpdateRequest( parameterTool.getRequired("index"), parameterTool.getRequired("type"), element.f0) .doc(json) .upsert(json); } }
private static UpdateRequest createUpdateRequest(Tuple2<String, String> element, ParameterTool parameterTool) { Map<String, Object> json = new HashMap<>(); json.put("data", element.f1); return new UpdateRequest( parameterTool.getRequired("index"), parameterTool.getRequired("type"), element.f0) .doc(json) .upsert(json); } }
private static UpdateRequest createUpdateRequest(Tuple2<String, String> element, ParameterTool parameterTool) { Map<String, Object> json = new HashMap<>(); json.put("data", element.f1); return new UpdateRequest( parameterTool.getRequired("index"), parameterTool.getRequired("type"), element.f0) .doc(json) .upsert(json); } }
private static UpdateRequest createUpdateRequest(Tuple2<String, String> element, ParameterTool parameterTool) { Map<String, Object> json = new HashMap<>(); json.put("data", element.f1); return new UpdateRequest( parameterTool.getRequired("index"), parameterTool.getRequired("type"), element.f0) .doc(json) .upsert(json); } }
@Override public void updateWorkflow(String workflowInstanceId, String[] keys, Object[] values) { if (keys.length != values.length) { throw new ApplicationException(ApplicationException.Code.INVALID_INPUT, "Number of keys and values do not match"); } UpdateRequest request = new UpdateRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId); Map<String, Object> source = IntStream.range(0, keys.length).boxed() .collect(Collectors.toMap(i -> keys[i], i -> values[i])); request.doc(source); logger.debug("Updating workflow {} with {}", workflowInstanceId, source); new RetryUtil<UpdateResponse>().retryOnException(() -> { try { return elasticSearchClient.update(request); } catch (IOException e) { throw new RuntimeException(e); } }, null, null, RETRY_COUNT, "Updating index for doc_type workflow", "updateWorkflow"); }
@Override public void updateWorkflow(String workflowInstanceId, String[] keys, Object[] values) { if (keys.length != values.length) { throw new ApplicationException(Code.INVALID_INPUT, "Number of keys and values do not match"); } UpdateRequest request = new UpdateRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId); Map<String, Object> source = IntStream.range(0, keys.length) .boxed() .collect(Collectors.toMap(i -> keys[i], i -> values[i])); request.doc(source); logger.debug("Updating workflow {} with {}", workflowInstanceId, source); new RetryUtil<>().retryOnException( () -> elasticSearchClient.update(request), null, null, RETRY_COUNT, "Updating index for doc_type workflow", "updateWorkflow" ); }
@Override public void indexTask(Task task) { try { 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); updateWithRetry(req, "Index workflow into doc_type workflow"); } catch (Exception e) { logger.error("Failed to index task: {}", task.getTaskId(), e); } }
@Override public void indexWorkflow(Workflow workflow) { try { String id = workflow.getWorkflowId(); WorkflowSummary summary = new WorkflowSummary(workflow); byte[] doc = objectMapper.writeValueAsBytes(summary); UpdateRequest req = new UpdateRequest(indexName, WORKFLOW_DOC_TYPE, id); req.doc(doc, XContentType.JSON); req.upsert(doc, XContentType.JSON); req.retryOnConflict(5); updateWithRetry(req, "Index workflow into doc_type workflow"); } catch (Exception e) { logger.error("Failed to index workflow: {}", workflow.getWorkflowId(), e); } }
@Override public void addEventExecution(EventExecution eventExecution) { try { 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); req.retryOnConflict(5); updateWithRetry(req, "Update Event execution for doc_type event"); } catch (Exception e) { logger.error("Failed to index event execution: {}", eventExecution.getId(), e); } }
public UpdateQuery build() { UpdateQuery updateQuery = new UpdateQuery(); updateQuery.setId(id); updateQuery.setIndexName(indexName); updateQuery.setType(type); updateQuery.setClazz(clazz); if (this.indexRequest != null) { if (this.updateRequest == null) { updateRequest = new UpdateRequest(); } updateRequest.doc(indexRequest); } updateQuery.setUpdateRequest(updateRequest); updateQuery.setDoUpsert(doUpsert); return updateQuery; } }
/** * Sets the doc to use for updates when a script is not specified. */ public UpdateRequestBuilder setDoc(Map source) { request.doc(source); return this; }
/** * Sets the doc to use for updates when a script is not specified. */ public UpdateRequestBuilder setDoc(byte[] source, int offset, int length, XContentType xContentType) { request.doc(source, offset, length, xContentType); return this; }
/** * Sets the doc to use for updates when a script is not specified, the doc provided * is a field and value pairs. */ public UpdateRequestBuilder setDoc(Object... source) { request.doc(source); return this; }
/** * Sets the doc to use for updates when a script is not specified. */ public UpdateRequestBuilder setDoc(IndexRequest indexRequest) { request.doc(indexRequest); return this; }
/** * Sets the doc to use for updates when a script is not specified. */ public UpdateRequestBuilder setDoc(XContentBuilder source) { request.doc(source); return this; }
/** * Sets the doc to use for updates when a script is not specified. */ public UpdateRequestBuilder setDoc(String source, XContentType xContentType) { request.doc(source, xContentType); return this; }