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

The following examples show how to use org.elasticsearch.action.update.UpdateRequest#doc() . 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: ElasticSearchRestDAOV5.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(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 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 2
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 3
Source File: UpdateQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private void parseUpdate(ElasticDslContext dslContext) {
    dslContext.getParseResult().setSqlOperation(SqlOperation.UPDATE);
    ElasticsearchParser.UpdateOperationContext updateOperationContext = dslContext.getSqlContext().updateOperation();
    String indexName = updateOperationContext.tableRef().indexName.getText();
    String id = StringManager.removeStringSymbol(updateOperationContext.identifyClause().id.getText());
    UpdateRequest updateRequest = new UpdateRequest(indexName, id);
    int size = updateOperationContext.ID().size();
    Map<String, Object> doc = new HashMap<>(0);
    for (int i = 0; i < size; i++) {
        if (updateOperationContext.identity(i).identityList() != null) {
            FlatMapUtils.flatPut(updateOperationContext.ID(i).getText(), parseIdentityList(updateOperationContext.identity(i).identityList().identity()), doc);
        } else if (updateOperationContext.identity(i).STRING() != null) {
            FlatMapUtils.flatPut(updateOperationContext.ID(i).getText(), StringManager.removeStringSymbol(updateOperationContext.identity(i).getText()), doc);
        } else {
            FlatMapUtils.flatPut(updateOperationContext.ID(i).getText(), updateOperationContext.identity(i).getText(), doc);
        }
    }
    updateRequest.doc(doc);
    if (updateOperationContext.routingClause() != null) {
        updateRequest.routing(StringManager.removeStringSymbol(updateOperationContext.routingClause().STRING(0).getText()));
    }
    dslContext.getParseResult().setUpdateRequest(updateRequest);
}
 
Example 4
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * 更新操作
 * 
 * @throws IOException
 */
private static void update() throws IOException {
	String type = "_doc";
	String index = "test1";
	// 唯一编号
	String id = "1";
	UpdateRequest upateRequest = new UpdateRequest();
	upateRequest.id(id);
	upateRequest.index(index);
	upateRequest.type(type);

	// 依旧可以使用Map这种集合作为更新条件
	Map<String, Object> jsonMap = new HashMap<>();
	jsonMap.put("uid", 12345);
	jsonMap.put("phone", 123456789019L);
	jsonMap.put("msgcode", 2);
	jsonMap.put("sendtime", "2019-03-14 01:57:04");
	jsonMap.put("message", "xuwujing study Elasticsearch");
	upateRequest.doc(jsonMap);
	// upsert 方法表示如果数据不存在,那么就新增一条
	upateRequest.docAsUpsert(true);
	client.update(upateRequest, RequestOptions.DEFAULT);
	System.out.println("更新成功!");

}
 
Example 5
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 6
Source File: ElasticSearchService.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
/**
 * 修改文章数据
 */
public void updateArticle(Article article) throws IOException {
    UpdateRequest updateRequest = new UpdateRequest(BLOG_INDEX, ARTICLE_TYPE, article.getId().toString());
    updateRequest.doc(JSON.toJSONString(article), XContentType.JSON);
    UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

    logger.info("修改数据 | {}", JSON.toJSONString(updateResponse));
}
 
Example 7
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 8
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 9
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 10
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();
}
 
Example 11
Source File: EsPublisher.java    From tunnel with Apache License 2.0 5 votes vote down vote up
private DocWriteRequest eventToRequest(EsConfig esConfig, EventType eventType, Map<String, String> values) {

        DocWriteRequest req = null;

        // column_name,column_name
        String id = esConfig.getEsIdFieldNames()
                .stream()
                .map(esId -> String.valueOf(values.get(esId)))
                .reduce((s1, s2) -> s1 + esConfig.getSeparator() + s2)
                .orElse("");

        if (StringUtils.isBlank(id)) {
            return null;
        }
        String type = esConfig.getType();
        String index = esConfig.getIndex();


        switch (eventType) {
            case INSERT:
            case UPDATE:
                UpdateRequest ur = new UpdateRequest(index, type, id);
                ur.doc(values);
                ur.docAsUpsert(true);
                req = ur;
                break;
            case DELETE:
                DeleteRequest dr = new DeleteRequest(index, type, id);
                dr.id(id);
                req = dr;
                break;
            default:
                break;
        }
        return req;
    }
 
Example 12
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 13
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 14
Source File: TestESRichClientImp.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
@Test
public void update() throws IOException {
    ESRichClientImp esRCI = new ESRichClientImp(DEF_RHLCLIENT);
    List<Range> ranges = new ArrayList<>();
    Range r1 = new Range("2018-01-05", "2018-01-05", "0", "23");
    ranges.add(r1);
    Booking bk1 = new Booking(new TargetingChannel(), ranges, 0, "advId", 0);
    Map source = new HashMap();
    source.put("days", new ArrayList());
    source.put("bk_id", bk1.getBookingId());
    Map query = new HashMap();
    query.put("g", "g_m");
    source.put("query", query);
    source.put("del", false);
    bk1 = new Booking("bookingID", source);

    SearchRequest sReq = new SearchRequest("");
    ESResponse res1 = esRCI.search(sReq);
    assertNotNull(res1);
    assertNotNull(res1.getSourceMap());

    UpdateRequest uReq = new UpdateRequest("", "doc", bk1.getId());
    Map<String, Object> requestMap = new HashMap<>();
    requestMap.put("del", true);
    uReq.doc(requestMap);
    ESResponse res = esRCI.update(uReq, WriteRequest.RefreshPolicy.IMMEDIATE.getValue());

    assertNotNull(res);
    assertEquals(UpdateResponse.Result.UPDATED, res.getUpdateResponse().getResult());
}
 
Example 15
Source File: BookingDaoESImp.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
@Override
public void updateBookingBucketWithAllocatedAmount(String bookingBucketId, Map<String, Double> newAllocatedAmount) throws IOException
{
    UpdateRequest updateRequest = new UpdateRequest(this.bookingBucketsIndex, ES_TYPE, bookingBucketId);
    Map<String, Object> requestMap = new HashMap<>();
    requestMap.put("allocated_amount", newAllocatedAmount);
    updateRequest.doc(requestMap);
    esclient.update(updateRequest, WriteRequest.RefreshPolicy.WAIT_UNTIL.getValue());
}
 
Example 16
Source File: ElasticSearchService.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
public void updateDocument(String type, String id, Map<String, Object> source) {
    try {
        if (this.instance() == null) return;
        UpdateRequest request = new UpdateRequest(name, type, id);
        request.doc(source);
        client.update(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error(e.getMessage());
    }
}
 
Example 17
Source File: HighLevelRestController.java    From ProjectStudy with MIT License 5 votes vote down vote up
/**
 * 修改文档
 *
 * @param bookDto
 * @return com.example.common.ResponseBean
 * @throws
 * @author wliduo[[email protected]]
 * @date 2019/8/15 16:02
 */
@PutMapping("/book")
public ResponseBean update(@RequestBody BookDto bookDto) throws IOException {
    // UpdateRequest
    UpdateRequest updateRequest = new UpdateRequest(Constant.INDEX, bookDto.getId().toString());
    updateRequest.doc(JSON.toJSONString(bookDto), XContentType.JSON);
    // 操作ES
    UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
    return new ResponseBean(HttpStatus.OK.value(), "修改成功", updateResponse);
}
 
Example 18
Source File: ElasticSearchRestHighImpl.java    From sunbird-lms-service with MIT License 4 votes vote down vote up
/**
 * This method will update data based on identifier.take the data based on identifier and merge
 * with incoming data then update it.
 *
 * @param index String
 * @param type String
 * @param identifier String
 * @param data Map<String,Object>
 * @return boolean
 */
@Override
public Future<Boolean> upsert(String index, String identifier, Map<String, Object> data) {
  long startTime = System.currentTimeMillis();
  Promise<Boolean> promise = Futures.promise();
  ProjectLogger.log(
      "ElasticSearchRestHighImpl:upsert: method started at =="
          + startTime
          + " for INdex "
          + index,
      LoggerEnum.PERF_LOG.name());
  if (!StringUtils.isBlank(index)
      && !StringUtils.isBlank(identifier)
      && data != null
      && data.size() > 0) {

    IndexRequest indexRequest = new IndexRequest(index, _DOC, identifier).source(data);

    UpdateRequest updateRequest = new UpdateRequest(index, _DOC, identifier).upsert(indexRequest);
    updateRequest.doc(indexRequest);
    ActionListener<UpdateResponse> listener =
        new ActionListener<UpdateResponse>() {
          @Override
          public void onResponse(UpdateResponse updateResponse) {
            promise.success(true);
            ProjectLogger.log(
                "ElasticSearchRestHighImpl:upsert:  Response for index : "
                    + updateResponse.getResult()
                    + ","
                    + index
                    + ",identifier : "
                    + identifier,
                LoggerEnum.INFO.name());
            ProjectLogger.log(
                "ElasticSearchRestHighImpl:upsert: method end =="
                    + " for Index "
                    + index
                    + " ,Total time elapsed = "
                    + calculateEndTime(startTime),
                LoggerEnum.PERF_LOG.name());
          }

          @Override
          public void onFailure(Exception e) {
            ProjectLogger.log(
                "ElasticSearchRestHighImpl:upsert: exception occured:" + e.getMessage(),
                LoggerEnum.ERROR.name());
            promise.failure(e);
          }
        };
    ConnectionManager.getRestClient().updateAsync(updateRequest, listener);
    return promise.future();
  } else {
    ProjectLogger.log(
        "ElasticSearchRestHighImpl:upsert: Requested data is invalid.", LoggerEnum.ERROR.name());
    promise.failure(ProjectUtil.createClientException(ResponseCode.invalidData));
    return promise.future();
  }
}
 
Example 19
Source File: EsTask.java    From tunnel with Apache License 2.0 4 votes vote down vote up
private DocWriteRequest toRequest(Invocation invocation) {
    DocWriteRequest req = null;

    EsConfig esConfig = invocation.getParameter(Constants.CONFIG_NAME);

    Map<String, String> values = invocation.getEvent().getDataList()
            .stream()
            .collect(Collectors.toMap(CellData::getName, CellData::getValue));

    // column_name,column_name
    String id = esConfig.getEsId()
            .stream()
            .map(esId -> String.valueOf(values.get(esId)))
            .reduce((s1, s2) -> s1 + esConfig.getSeparator() + s2)
            .orElse("");

    if (StringUtils.isBlank(id)) {
        return null;
    }

    String type = esConfig.getType();
    String index = esConfig.getIndex();

    switch (invocation.getEvent().getWalType()) {
        case INSERT:
        case UPDATE:
            UpdateRequest ur = new UpdateRequest(index, type, id);
            ur.doc(values);
            ur.docAsUpsert(true);
            req = ur;
            break;
        case DELETE:
            DeleteRequest dr = new DeleteRequest(index, type, id);
            dr.id(id);
            req = dr;
            break;
        default:
            break;
    }
    return req;
}
 
Example 20
Source File: SpiderInfoDAO.java    From Gather-Platform with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 更新爬虫模板
 *
 * @param spiderInfo 爬虫模板实体
 * @return 爬虫模板id
 * @throws ExecutionException
 * @throws InterruptedException
 */
public String update(SpiderInfo spiderInfo) throws ExecutionException, InterruptedException {
    Preconditions.checkArgument(StringUtils.isNotBlank(spiderInfo.getId()), "待更新爬虫模板id不可为空");
    UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, TYPE_NAME, spiderInfo.getId());
    updateRequest.doc(gson.toJson(spiderInfo));
    UpdateResponse updateResponse = client.update(updateRequest).get();
    return updateResponse.getId();
}