org.elasticsearch.action.update.UpdateRequest Java Examples

The following examples show how to use org.elasticsearch.action.update.UpdateRequest. 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: 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 #2
Source File: RowElasticsearchSinkFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
private void processUpsert(RowData row, RequestIndexer indexer) {
	final byte[] document = serializationSchema.serialize(row);
	final String key = createKey.apply(row);
	if (key != null) {
		final UpdateRequest updateRequest = requestFactory.createUpdateRequest(
			indexGenerator.generate(row),
			docType,
			key,
			contentType,
			document);
		indexer.add(updateRequest);
	} else {
		final IndexRequest indexRequest = requestFactory.createIndexRequest(
			indexGenerator.generate(row),
			docType,
			key,
			contentType,
			document);
		indexer.add(indexRequest);
	}
}
 
Example #3
Source File: ElasticsearchUpsertTableSinkBase.java    From flink with Apache License 2.0 6 votes vote down vote up
private void processUpsert(Row row, RequestIndexer indexer) {
	final byte[] document = serializationSchema.serialize(row);
	if (keyFieldIndices.length == 0) {
		final IndexRequest indexRequest = requestFactory.createIndexRequest(
			index,
			docType,
			contentType,
			document);
		indexer.add(indexRequest);
	} else {
		final String key = createKey(row);
		final UpdateRequest updateRequest = requestFactory.createUpdateRequest(
			index,
			docType,
			key,
			contentType,
			document);
		indexer.add(updateRequest);
	}
}
 
Example #4
Source File: Elasticsearch6SinkFunction.java    From alchemy with Apache License 2.0 6 votes vote down vote up
private void processUpsert(Row row, RequestIndexer indexer) {
    final byte[] document = serializationSchema.serialize(row);
    if (keyFieldIndices.length == 0) {
        final IndexRequest indexRequest = requestFactory.createIndexRequest(
                getIndex(row),
                docType,
                contentType,
                document);
        indexer.add(indexRequest);
    } else {
        final String key = createKey(row);
        final UpdateRequest updateRequest = requestFactory.createUpdateRequest(
                getIndex(row),
                docType,
                key,
                contentType,
                document);
        indexer.add(updateRequest);
    }
}
 
Example #5
Source File: ElasticSearcher.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void updateArticle(Article article) {
    UpdateRequest updateRequest = new UpdateRequest(index, type, article.getId().toString());
    Map<String, Object> map = new HashMap<>();
    map.putAll(CPI.getAttrs(article));
    updateRequest.doc(map);

    try {
        UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);
        if (LogKit.isDebugEnabled()) {
            LogKit.debug(response.toString());
        }
    } catch (Exception e) {
        LOG.error(e.toString(), e);
    }
}
 
Example #6
Source File: BulkRequest.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeByte(consistencyLevel.id());
    out.writeVInt(requests.size());
    for (ActionRequest request : requests) {
        if (request instanceof IndexRequest) {
            out.writeByte((byte) 0);
        } else if (request instanceof DeleteRequest) {
            out.writeByte((byte) 1);
        } else if (request instanceof UpdateRequest) {
            out.writeByte((byte) 2);
        }
        request.writeTo(out);
    }
    out.writeBoolean(refresh);
    timeout.writeTo(out);
}
 
Example #7
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 #8
Source File: LegacyDetectorRepositoryImpl.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Override
public void toggleDetector(String uuid, Boolean enabled) {
    MDC.put("DetectorUuid", uuid);
    val updateRequest = new UpdateRequest(DETECTOR_INDEX, DETECTOR_DOC_TYPE, uuid);
    Date nowDate = DateUtil.now();
    String nowValue = DateUtil.toDateString(nowDate.toInstant());

    Map<String, Object> jsonMap = new HashMap<>();
    Map<String, Object> metaMap = new HashMap<>();
    metaMap.put("dateUpdated", nowDate);
    jsonMap.put("lastUpdateTimestamp", nowValue);
    jsonMap.put("enabled", enabled);
    jsonMap.put("meta", metaMap);
    updateRequest.doc(jsonMap);
    try {
        val updateResponse = legacyElasticSearchClient.update(updateRequest, RequestOptions.DEFAULT);
        if (elasticsearchUtil.checkNullResponse(updateResponse.getResult())) {
            throw new RecordNotFoundException("Invalid request: " + uuid);
        }
    } catch (IOException e) {
        log.error("Updating elastic search failed", e);
        throw new RuntimeException(e);
    } finally {
        MDC.remove("DetectorUuid");
    }
}
 
Example #9
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 #10
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@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");
    }

    long startTime = Instant.now().toEpochMilli();
    UpdateRequest request = new UpdateRequest(workflowIndexName, 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 {} in elasticsearch index: {}", workflowInstanceId, workflowIndexName);
    new RetryUtil<>().retryOnException(
        () -> elasticSearchClient.update(request).actionGet(),
        null,
        null,
        RETRY_COUNT,
        "Updating index for doc_type workflow",
        "updateWorkflow"
    );
    long endTime = Instant.now().toEpochMilli();
    LOGGER.debug("Time taken {} for updating workflow: {}", endTime - startTime, workflowInstanceId);
    Monitors.recordESIndexTime("update_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
    Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
}
 
Example #11
Source File: UpdateApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
        try{
            RestHighLevelClient client = HighLevelClient.getInstance();
            UpdateRequest updateRequest = new UpdateRequest("jingma2_20180716", "testlog", "1");
            updateRequest.doc(buildIndexData());//传递Map结构数据
            //updateRequest.doc(jsonString, XContentType.JSON);//传递json字符串

//            updateRequest.version(1);//设置待更新文档的版本,防止高并发下误更新
//            updateRequest.upsert(buildIndexData());//没有文档时插入该文档
//            updateRequest.upsert(jsonString, XContentType.JSON);

            UpdateResponse updateResponse = client.update(updateRequest);
                    System.out.println(updateResponse);
        }finally{
            HighLevelClient.close();
        }
    }
 
Example #12
Source File: BulkApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    try{
        RestHighLevelClient client = HighLevelClient.getInstance();
        BulkRequest bulkRequest = new BulkRequest();
        for(int i=1;i<4;i++) {
            bulkRequest.add(new IndexRequest("jingma2_20180716", "testlog", String.valueOf(i)).source(buildIndexData()));
        }
        bulkRequest.add(new DeleteRequest("jingma2_20180716", "testlog", "1"));
        bulkRequest.add(new UpdateRequest("jingma2_20180716", "testlog", "2").doc(XContentType.JSON,"name","马靖2"));

        BulkResponse bulkResponse = client.bulk(bulkRequest);
        System.out.println(bulkResponse);

    }finally{
        HighLevelClient.close();
    }
}
 
Example #13
Source File: Indexer.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Maps document Object to Json and creates and new index request
 * 
 * @param indexName
 *            - index name
 * @param documentType
 *            - document type
 * @param uid
 *            - unique identifier
 * @param object
 *            - object that represents the structure of a document for indexing
 * @return
 * @return IndexResponse
 * @throws IOException
 */
private static void index(String indexName, String documentType, String uid, String document) {

	try {
		
		GetRequest getRequest = new GetRequest(indexName, documentType, uid).fetchSourceContext(fetchSourceContext).storedFields("_none_");
		
		if(highLevelClient.exists(getRequest, getReadHeaders()))
		{
			UpdateRequest request = new UpdateRequest(indexName, documentType, uid);
			request.doc(document, XContentType.JSON);
			logger.info("Document (uid: " + uid + ") has been updated");
		}
		else
		{
			IndexRequest indexRequest = new IndexRequest();
			indexRequest .index(indexName).type(documentType).id(uid).source(document,XContentType.JSON);
				
			logger.info("Document (uid: " + uid + ") has been "	+
										highLevelClient.index(indexRequest, getWriteHeaders()).getResult().toString().toLowerCase());
		}
		
	} catch (IOException io) {
		logger.error("Method index has experienced an IO error\n" + io);
	}
}
 
Example #14
Source File: BulkNodeClient.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Override
public BulkNodeClient bulkUpdate(UpdateRequest updateRequest) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    try {
        if (metric != null) {
            metric.getCurrentIngest().inc(updateRequest.index(), updateRequest.type(), updateRequest.id());
        }
        bulkProcessor.add(updateRequest);
    } catch (Exception e) {
        throwable = e;
        closed = true;
        logger.error("bulk add of update request failed: " + e.getMessage(), e);
    }
    return this;
}
 
Example #15
Source File: PreElasticsearch6BulkProcessorIndexer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void add(UpdateRequest... updateRequests) {
	for (UpdateRequest updateRequest : updateRequests) {
		if (flushOnCheckpoint) {
			numPendingRequestsRef.getAndIncrement();
		}
		this.bulkProcessor.add(updateRequest);
	}
}
 
Example #16
Source File: Elasticsearch6UpsertTableSink.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: Elasticsearch6BulkProcessorIndexer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void add(UpdateRequest... updateRequests) {
	for (UpdateRequest updateRequest : updateRequests) {
		if (flushOnCheckpoint) {
			numPendingRequestsRef.getAndIncrement();
		}
		this.bulkProcessor.add(updateRequest);
	}
}
 
Example #18
Source File: ElasticsearchRequestUtils.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private static int getSizeOfUpdateRequest(UpdateRequest updateRequest) {
    int sizeInBytes = 0;
    if (updateRequest.doc() != null) {
        sizeInBytes += updateRequest.doc().source().length();
    }
    if (updateRequest.upsertRequest() != null) {
        sizeInBytes += updateRequest.upsertRequest().source().length();
    }
    if (updateRequest.script() != null) {
        sizeInBytes += updateRequest.script().getIdOrCode().length() * 2;
    }
    return sizeInBytes;
}
 
Example #19
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void update(RestHighLevelClient client, String indexName) throws IOException {
    UpdateRequest request = new UpdateRequest(indexName, "_doc", "1");
    Map<String, Object> parameters = singletonMap("title", "c++ programing.");
    Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.title = params.title", parameters);
    request.script(inline);

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
    if (updateResponse.getVersion() != 2) {
        String message = "elasticsearch update data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #20
Source File: BufferingNoOpRequestIndexer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
void processBufferedRequests(RequestIndexer actualIndexer) {
	for (ActionRequest request : bufferedRequests) {
		if (request instanceof IndexRequest) {
			actualIndexer.add((IndexRequest) request);
		} else if (request instanceof DeleteRequest) {
			actualIndexer.add((DeleteRequest) request);
		} else if (request instanceof UpdateRequest) {
			actualIndexer.add((UpdateRequest) request);
		}
	}

	bufferedRequests.clear();
}
 
Example #21
Source File: RequestIndexer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Add multiple {@link ActionRequest} to the indexer to prepare for sending requests to Elasticsearch.
 *
 * @param actionRequests The multiple {@link ActionRequest} to add.
 * @deprecated use the {@link DeleteRequest}, {@link IndexRequest} or {@link UpdateRequest}
 */
@Deprecated
default void add(ActionRequest... actionRequests) {
	for (ActionRequest actionRequest : actionRequests) {
		if (actionRequest instanceof IndexRequest) {
			add((IndexRequest) actionRequest);
		} else if (actionRequest instanceof DeleteRequest) {
			add((DeleteRequest) actionRequest);
		} else if (actionRequest instanceof UpdateRequest) {
			add((UpdateRequest) actionRequest);
		} else {
			throw new IllegalArgumentException("RequestIndexer only supports Index, Delete and Update requests");
		}
	}
}
 
Example #22
Source File: ElasticsearchUpsertTableSinkBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an update request to be added to a {@link RequestIndexer}.
 */
UpdateRequest createUpdateRequest(
	String index,
	String docType,
	String key,
	XContentType contentType,
	byte[] document);
 
Example #23
Source File: TestTransportClient.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpsert() throws Exception {
    IndexRequest indexRequest = new IndexRequest("index", "type", "1")
            .source(XContentFactory.jsonBuilder().startObject().field("name", "Joe Smith").field("gender", "male").endObject());
    UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
            .doc(XContentFactory.jsonBuilder().startObject().field("gender", "male").endObject()).upsert(indexRequest);
    UpdateResponse response = client.update(updateRequest).get();
    System.out.println(response.getGetResult().sourceAsString());
}
 
Example #24
Source File: ElasticsearchRequestUtils.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private static int getSizeOfUpdateRequest(UpdateRequest updateRequest) {
    int sizeInBytes = 0;
    if (updateRequest.doc() != null) {
        sizeInBytes += updateRequest.doc().source().length();
    }
    if (updateRequest.upsertRequest() != null) {
        sizeInBytes += updateRequest.upsertRequest().source().length();
    }
    if (updateRequest.script() != null) {
        sizeInBytes += updateRequest.script().getIdOrCode().length() * 2;
    }
    return sizeInBytes;
}
 
Example #25
Source File: BulkRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Add a request to the current BulkRequest.
 * @param request Request to add
 * @param payload Optional payload
 * @return the current bulk request
 */
public BulkRequest add(ActionRequest request, @Nullable Object payload) {
    if (request instanceof IndexRequest) {
        add((IndexRequest) request, payload);
    } else if (request instanceof DeleteRequest) {
        add((DeleteRequest) request, payload);
    } else if (request instanceof UpdateRequest) {
        add((UpdateRequest) request, payload);
    } else {
        throw new IllegalArgumentException("No support for request [" + request + "]");
    }
    return this;
}
 
Example #26
Source File: BulkTransportClient.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Override
public BulkTransportClient update(String index, String type, String id, String source) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    try {
        metric.getCurrentIngest().inc(index, type, id);
        bulkProcessor.add(new UpdateRequest().index(index).type(type).id(id).upsert(source));
    } catch (Exception e) {
        throwable = e;
        closed = true;
        logger.error("bulk add of update request failed: " + e.getMessage(), e);
    }
    return this;
}
 
Example #27
Source File: BulkTransportClient.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Override
public BulkTransportClient bulkUpdate(UpdateRequest updateRequest) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    try {
        metric.getCurrentIngest().inc(updateRequest.index(), updateRequest.type(), updateRequest.id());
        bulkProcessor.add(updateRequest);
    } catch (Exception e) {
        throwable = e;
        closed = true;
        logger.error("bulk add of update request failed: " + e.getMessage(), e);
    }
    return this;
}
 
Example #28
Source File: Elasticsearch6UpsertTableSink.java    From flink with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #29
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private UpdateRequest createUpdateRequest(String index, String type, String id, Map<String, Object> json, String parent, String root) {
    UpdateRequest req = new UpdateRequest(index, esCfg.indexType, id);

    setJoinFields(json, type, parent, root);
    req = req.doc(json);
    return (parent != null) ? req.routing(root) : req;
}
 
Example #30
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void update(String indexName) throws IOException {
    UpdateRequest request = new UpdateRequest(indexName, "1");
    Map<String, Object> parameters = singletonMap("title", "c++ programing.");
    Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.title = params.title", parameters);
    request.script(inline);

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
    if (updateResponse.getVersion() != 2) {
        String message = "elasticsearch update data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}