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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #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: 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 #9
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 #10
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 #11
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 #12
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 #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: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean tagUntag(Project prj, String documentId, String rootDocument, Script untagScript) throws IOException {
    UpdateRequest update = new UpdateRequest(prj.getId(), esCfg.indexType, documentId).routing(rootDocument);
    update.script(untagScript);
    update.setRefreshPolicy(esCfg.refreshPolicy);
    UpdateResponse updateResponse = client.update(update);
    return updateResponse.status() == RestStatus.OK && updateResponse.getResult() == DocWriteResponse.Result.UPDATED;
}
 
Example #15
Source File: ElasticsearchLockProvider.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    try {
        Map<String, Object> lockObject = lockObject(lockConfiguration.getName(),
            lockConfiguration.getLockAtMostUntil(),
            now());
        UpdateRequest ur = new UpdateRequest()
            .index(index)
            .type(type)
            .id(lockConfiguration.getName())
            .script(new Script(ScriptType.INLINE,
                "painless",
                UPDATE_SCRIPT,
                lockObject)
            )
            .upsert(lockObject)
            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        UpdateResponse res = highLevelClient.update(ur, RequestOptions.DEFAULT);
        if (res.getResult() != DocWriteResponse.Result.NOOP) {
            return Optional.of(new ElasticsearchSimpleLock(lockConfiguration));
        } else {
            return Optional.empty();
        }
    } catch (IOException | ElasticsearchException e) {
        if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) {
            return Optional.empty();
        } else {
            throw new LockException("Unexpected exception occurred", e);
        }
    }
}
 
Example #16
Source File: EsStore.java    From soundwave with Apache License 2.0 5 votes vote down vote up
protected <E extends EsDocument> EsBulkResponseSummary bulkUpdate(List<E> updateDoc,
                                                                  int batchSize,
                                                                  ObjectMapper mapper)
    throws Exception {
  Preconditions.checkArgument(batchSize > 0);
  List<BulkResponse> responses = new ArrayList<>();
  int count = 0;
  BulkRequestExecutor executor = new BulkRequestExecutor(this.esClient);
  for (E doc : updateDoc) {
    if (doc.getVersion() > 0) {
      executor
          .add(new UpdateRequest(getIndexName(), getDocTypeName(), doc.getId())
              .doc(mapper.writeValueAsBytes(doc)).version(doc.getVersion()));
    } else {
      executor
          .add(new UpdateRequest(getIndexName(), getDocTypeName(), doc.getId())
              .doc(mapper.writeValueAsBytes(doc)));
    }
    count++;
    if (count >= batchSize) {
      responses.add(executor.execute());
      count = 0;
      executor = new BulkRequestExecutor(this.esClient);
    }
  }

  if (count > 0) {
    responses.add(executor.execute());
  }

  return new EsBulkResponseSummary(responses);
}
 
Example #17
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 5 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();
    String docType = StringUtils.isBlank(docTypeOverride) ? WORKFLOW_DOC_TYPE : docTypeOverride;
    UpdateRequest request = new UpdateRequest(workflowIndexName, docType, 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 workflow document: " + workflowInstanceId, "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 #18
Source File: ElasticSearch.java    From hsweb-learning with Apache License 2.0 5 votes vote down vote up
private static void UpdateRequest(Client client) throws IOException, InterruptedException, ExecutionException {

        //当更新的内容不存在的时候会添加  
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("twitter");
        updateRequest.type("tweet");
        updateRequest.id("3");
        XContentBuilder jsonBuilder =XContentFactory.jsonBuilder();
        updateRequest.doc(jsonBuilder
                .startObject()
                .field("message","我是林志颖啊")
                .field("user","lin")
                .endObject());
        UpdateResponse response= client.update(updateRequest).get();  
           
    /*   
     *  //当更新的内容不存在时候,不会添加 
        UpdateResponse response = client.prepareUpdate("twitter","tweet","1") 
                        .setDoc(jsonBuilder() 
                                 .startObject() 
                                   .field("users4","xue") 
                                 .endObject()) 
                                 .get();*/

        System.out.println(response.getVersion());


    }
 
Example #19
Source File: ElasticsearchRequestUtils.java    From vertexium with Apache License 2.0 5 votes vote down vote up
public static int getSize(ActionRequest request) {
    if (request instanceof UpdateRequest) {
        return getSizeOfUpdateRequest((UpdateRequest) request);
    } else if (request instanceof DeleteRequest) {
        return getSizeOfDeleteRequest((DeleteRequest) request);
    } else {
        throw new VertexiumException("unhandled action request type: " + request.getClass().getName());
    }
}
 
Example #20
Source File: ElasticsearchChannel.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void handle404(SyncWrapper<WriteRequest> wrapper, BulkItemResponse item) {
  if (item.getFailure().getCause() instanceof DocumentMissingException) {
    logger.warn("Make update request upsert to resolve DocumentMissingException");
    UpdateRequest request = ((UpdateRequest) wrapper.getData());
    if (request.doc() != null) {
      request.docAsUpsert(true);
    } else {
      request.upsert(ESRequestMapper.getUpsert(wrapper.getEvent())).scriptedUpsert(true);
    }
  }
}
 
Example #21
Source File: BatchProcessEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void synchronous(List<PrepareRequest> prepareRequests) {
    if (CollectionUtils.isNotEmpty(prepareRequests)) {
        BulkRequest request = new BulkRequest();

        for (PrepareRequest prepareRequest : prepareRequests) {
            if (prepareRequest instanceof InsertRequest) {
                request.add((IndexRequest) prepareRequest);
            } else {
                request.add((UpdateRequest) prepareRequest);
            }
        }
        getClient().synchronousBulk(request);
    }
}
 
Example #22
Source File: BulkRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
BulkRequest internalAdd(UpdateRequest request, @Nullable Object payload) {
    requests.add(request);
    addPayload(payload);
    if (request.doc() != null) {
        sizeInBytes += request.doc().source().length();
    }
    if (request.upsertRequest() != null) {
        sizeInBytes += request.upsertRequest().source().length();
    }
    if (request.script() != null) {
        sizeInBytes += request.script().getScript().length() * 2;
    }
    return this;
}
 
Example #23
Source File: UpdateDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void testForClient() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("index");
    updateRequest.type("type");
    updateRequest.id("1");
    updateRequest.doc(XContentFactory.jsonBuilder()
            .startObject()
            .field("gender", "male")
            .endObject());
    client.update(updateRequest).get();
}
 
Example #24
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 更新网页
 *
 * @param webpage 网页
 * @return
 */
public boolean update(Webpage webpage) throws ExecutionException, InterruptedException {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index(INDEX_NAME);
    updateRequest.type(TYPE_NAME);
    updateRequest.id(webpage.getId());
    updateRequest.doc(gson.toJson(webpage));
    UpdateResponse response = client.update(updateRequest).get();
    return response.getResult() == UpdateResponse.Result.UPDATED;
}
 
Example #25
Source File: TransportActionNodeProxyExecuteMethodsInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void parseRequestInfo(ActionRequest request, AbstractSpan span) {
    // search request
    if (request instanceof SearchRequest) {
        parseSearchRequest((SearchRequest) request, span);
        return;
    }
    // get request
    if (request instanceof GetRequest) {
        parseGetRequest((GetRequest) request, span);
        return;
    }
    // index request
    if (request instanceof IndexRequest) {
        parseIndexRequest((IndexRequest) request, span);
        return;
    }
    // update request
    if (request instanceof UpdateRequest) {
        parseUpdateRequest((UpdateRequest) request, span);
        return;
    }
    // delete request
    if (request instanceof DeleteRequest) {
        parseDeleteRequest((DeleteRequest) request, span);
        return;
    }
    // delete index request
    if (request instanceof DeleteIndexRequest) {
        parseDeleteIndexRequest((DeleteIndexRequest) request, span);
        return;
    }
}
 
Example #26
Source File: ElasticsearchPersistUpdater.java    From streams with Apache License 2.0 5 votes vote down vote up
/**
 * Prepare and en-queue.
 * @see org.elasticsearch.action.update.UpdateRequest
 * @param indexName indexName
 * @param type type
 * @param id id
 * @param parent parent
 * @param routing routing
 * @param json json
 */
public void update(String indexName, String type, String id, String parent, String routing, String json) {
  UpdateRequest updateRequest;

  Objects.requireNonNull(id);
  Objects.requireNonNull(json);

  // They didn't specify an ID, so we will create one for them.
  updateRequest = new UpdateRequest()
      .index(indexName)
      .type(type)
      .id(id)
      .doc(json);

  if (StringUtils.isNotBlank(parent)) {
    updateRequest = updateRequest.parent(parent);
  }

  if (StringUtils.isNotBlank(routing)) {
    updateRequest = updateRequest.routing(routing);
  }

  // add fields
  //updateRequest.docAsUpsert(true);

  add(updateRequest);

}
 
Example #27
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 #28
Source File: Elasticsearch6SinkExample.java    From flink with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #29
Source File: ElasticsearchRestClientInstrumentationIT_RealReporter.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentScenario() throws IOException {
    // Index a document
    IndexResponse ir = client.index(new IndexRequest(INDEX, DOC_TYPE, DOC_ID).source(
        jsonBuilder()
            .startObject()
            .field(FOO, BAR)
            .endObject()
    ).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
    assertThat(ir.status().getStatus()).isEqualTo(201);

    SearchRequest searchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery(FOO, BAR));
    sourceBuilder.from(0);
    sourceBuilder.size(5);
    searchRequest.source(sourceBuilder);
    SearchResponse sr = client.search(searchRequest, RequestOptions.DEFAULT);
    assertThat(sr.getHits().totalHits).isEqualTo(1L);
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAR);

    // Now update and re-search
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put(FOO, BAZ);
    UpdateResponse ur = client.update(new UpdateRequest(INDEX, DOC_TYPE, DOC_ID).doc(jsonMap)
        .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
    assertThat(ur.status().getStatus()).isEqualTo(200);
    sr = client.search(new SearchRequest(INDEX), RequestOptions.DEFAULT);
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAZ);

    // Finally - delete the document
    DeleteResponse dr = client.delete(new DeleteRequest(INDEX, DOC_TYPE, DOC_ID), RequestOptions.DEFAULT);
    assertThat(dr.status().getStatus()).isEqualTo(200);
}
 
Example #30
Source File: TestTransportClient.java    From jframe with Apache License 2.0 5 votes vote down vote up
public void testUpdate() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("index");
    updateRequest.type("type");
    updateRequest.id("1");
    updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("gender", "male").endObject());
    UpdateResponse response = client.update(updateRequest).get();
    System.out.println(response.toString());

    client.prepareUpdate("ttl", "doc", "1").setScript(new Script("ctx._source.gender = \"male\"", ScriptService.ScriptType.INLINE, null, null))
            .get();

    client.prepareUpdate("ttl", "doc", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("gender", "male").endObject()).get();
}