partial Update、Upsert API————(更新文档)
1. partial Update (更新文档)
1.1. 更新文档——jsonString
private static void updateByJsonString(RestHighLevelClient client) throws Exception {
UpdateRequest updateRequest = new UpdateRequest("my_index", "my_type", "5");
Map<String, Object> paramsMap = new HashMap<>(16);
List<String> list = new ArrayList<>();
paramsMap.put("first_name", "冰河世纪");
String jsonString = JSON.toJSONString(paramsMap);
updateRequest.fetchSource(true);
updateRequest.doc(jsonString, XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() {
@Override
public void onResponse(UpdateResponse updateResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
GetResult getResult = updateResponse.getGetResult();
System.out.println(getResult.sourceAsString());
String index = updateResponse.getIndex();
String type = updateResponse.getType();
String id = updateResponse.getId();
long version = updateResponse.getVersion();
if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {
}
}
1.2. 更新文档——jsonMap
private static void updateByJsonMap(RestHighLevelClient client) throws Exception {
UpdateRequest updateRequest = new UpdateRequest("my_index", "my_type", "5");
Map<String, Object> paramsMap = new HashMap<>(16);
List<String> list = new ArrayList<>();
paramsMap.put("first_name", "冰河");
updateRequest.fetchSource(true);
updateRequest.doc(paramsMap);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() {
@Override
public void onResponse(UpdateResponse updateResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
GetResult getResult = updateResponse.getGetResult();
System.out.println(getResult.sourceAsString());
String index = updateResponse.getIndex();
String type = updateResponse.getType();
String id = updateResponse.getId();
long version = updateResponse.getVersion();
if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {
}
}
1.3. 更新文档——XContentBuilder
private static void updateByXContentBuilder(RestHighLevelClient client) throws Exception {
UpdateRequest updateRequest = new UpdateRequest("my_index", "my_type", "5");
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("first_name", "builder");
builder.endObject();
updateRequest.fetchSource(true);
updateRequest.doc(builder);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() {
@Override
public void onResponse(UpdateResponse updateResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
GetResult getResult = updateResponse.getGetResult();
System.out.println(getResult.sourceAsString());
String index = updateResponse.getIndex();
String type = updateResponse.getType();
String id = updateResponse.getId();
long version = updateResponse.getVersion();
if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {
}
}
1.4. 更新文档——Object
private static void updateByObject(RestHighLevelClient client) throws Exception {
UpdateRequest updateRequest = new UpdateRequest("my_index", "my_type", "5");
updateRequest.fetchSource(true);
updateRequest.doc("first_name", "object","last_name", "object");
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() {
@Override
public void onResponse(UpdateResponse updateResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
GetResult getResult = updateResponse.getGetResult();
System.out.println(getResult.sourceAsString());
String index = updateResponse.getIndex();
String type = updateResponse.getType();
String id = updateResponse.getId();
long version = updateResponse.getVersion();
if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {
}
}
2.Upsert—文档存在则更新,否则新增
- jsonString
- jsonMap
- XContentBuilder
- Object
备注:2、3、4请参照上述更新操作
2.1. jsonString
private static void upsertByJsonString(RestHighLevelClient client) throws Exception {
UpdateRequest updateRequest = new UpdateRequest("my_index", "my_type", "7");
updateRequest.docAsUpsert(true);
updateRequest.detectNoop(false);
updateRequest.fetchSource(true);
Map<String, Object> paramsMap = new HashMap<>(16);
paramsMap.put("about", "如果document存在就更新,否则新增");
String jsonString = JSON.toJSONString(paramsMap);
updateRequest.upsert(jsonString, XContentType.JSON);
updateRequest.doc(jsonString,XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() {
@Override
public void onResponse(UpdateResponse updateResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
GetResult getResult = updateResponse.getGetResult();
System.out.println(getResult.sourceAsString());
String index = updateResponse.getIndex();
String type = updateResponse.getType();
String id = updateResponse.getId();
long version = updateResponse.getVersion();
if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {
} else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {
}
}