Java Code Examples for org.elasticsearch.action.update.UpdateRequest#docAsUpsert()
The following examples show how to use
org.elasticsearch.action.update.UpdateRequest#docAsUpsert() .
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: EsHighLevelRestTest1.java From java-study with Apache License 2.0 | 6 votes |
/** * 更新操作 * * @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 2
Source File: ElasticsearchChannel.java From syncer with BSD 3-Clause "New" or "Revised" License | 6 votes |
static String toString(UpdateRequest request) { String res = "update {[" + request.index() + "][" + request.type() + "][" + request.id() + "]"; if (request.docAsUpsert()) { res += (", doc_as_upsert[" + request.docAsUpsert() + "]"); } if (request.doc() != null) { res += (", doc[" + request.doc() + "]"); } if (request.script() != null) { res += (", script[" + request.script() + "]"); } if (request.upsertRequest() != null) { res += (", upsert[" + request.upsertRequest() + "]"); } if (request.scriptedUpsert()) { res += (", scripted_upsert[" + request.scriptedUpsert() + "]"); } if (request.detectNoop()) { res += (", detect_noop[" + request.detectNoop() + "]"); } if (request.fields() != null) { res += (", fields[" + Arrays.toString(request.fields()) + "]"); } return res + "}"; }
Example 3
Source File: EsPublisher.java From tunnel with Apache License 2.0 | 5 votes |
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 4
Source File: EsPublisher.java From tunnel with Apache License 2.0 | 5 votes |
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 5
Source File: ElasticsearchChannel.java From syncer with BSD 3-Clause "New" or "Revised" License | 5 votes |
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 6
Source File: EsTask.java From tunnel with Apache License 2.0 | 4 votes |
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 7
Source File: EsTask.java From tunnel with Apache License 2.0 | 4 votes |
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 8
Source File: RestUpdateAction.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception { UpdateRequest updateRequest = new UpdateRequest(request.param("index"), request.param("type"), request.param("id")); updateRequest.routing(request.param("routing")); updateRequest.parent(request.param("parent")); updateRequest.timeout(request.paramAsTime("timeout", updateRequest.timeout())); updateRequest.refresh(request.paramAsBoolean("refresh", updateRequest.refresh())); String consistencyLevel = request.param("consistency"); if (consistencyLevel != null) { updateRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel)); } updateRequest.docAsUpsert(request.paramAsBoolean("doc_as_upsert", updateRequest.docAsUpsert())); ScriptParameterParser scriptParameterParser = new ScriptParameterParser(); scriptParameterParser.parseParams(request); ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue(); if (scriptValue != null) { Map<String, Object> scriptParams = new HashMap<>(); for (Map.Entry<String, String> entry : request.params().entrySet()) { if (entry.getKey().startsWith("sp_")) { scriptParams.put(entry.getKey().substring(3), entry.getValue()); } } updateRequest.script(new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), scriptParams)); } String sField = request.param("fields"); if (sField != null) { String[] sFields = Strings.splitStringByCommaToArray(sField); if (sFields != null) { updateRequest.fields(sFields); } } updateRequest.retryOnConflict(request.paramAsInt("retry_on_conflict", updateRequest.retryOnConflict())); updateRequest.version(RestActions.parseVersion(request)); updateRequest.versionType(VersionType.fromString(request.param("version_type"), updateRequest.versionType())); // see if we have it in the body if (request.hasContent()) { updateRequest.source(request.content()); IndexRequest upsertRequest = updateRequest.upsertRequest(); if (upsertRequest != null) { upsertRequest.routing(request.param("routing")); upsertRequest.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing upsertRequest.timestamp(request.param("timestamp")); if (request.hasParam("ttl")) { upsertRequest.ttl(request.param("ttl")); } upsertRequest.version(RestActions.parseVersion(request)); upsertRequest.versionType(VersionType.fromString(request.param("version_type"), upsertRequest.versionType())); } IndexRequest doc = updateRequest.doc(); if (doc != null) { doc.routing(request.param("routing")); doc.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing doc.timestamp(request.param("timestamp")); if (request.hasParam("ttl")) { doc.ttl(request.param("ttl")); } doc.version(RestActions.parseVersion(request)); doc.versionType(VersionType.fromString(request.param("version_type"), doc.versionType())); } } client.update(updateRequest, new RestBuilderListener<UpdateResponse>(channel) { @Override public RestResponse buildResponse(UpdateResponse response, XContentBuilder builder) throws Exception { builder.startObject(); ActionWriteResponse.ShardInfo shardInfo = response.getShardInfo(); builder.field(Fields._INDEX, response.getIndex()) .field(Fields._TYPE, response.getType()) .field(Fields._ID, response.getId()) .field(Fields._VERSION, response.getVersion()); shardInfo.toXContent(builder, request); if (response.getGetResult() != null) { builder.startObject(Fields.GET); response.getGetResult().toXContentEmbedded(builder, request); builder.endObject(); } builder.endObject(); RestStatus status = shardInfo.status(); if (response.isCreated()) { status = CREATED; } return new BytesRestResponse(status, builder); } }); }