直接存储JSON类数据
public boolean save(JSonObject object, String index, String id) {
boolean result;
Assert.notNull(index, INDEX_NULL);
IndexRequest request = new IndexRequest(index).source(object);
if(isNotEmpty(id)){
request.id(id);
}
try {
client.index(request, RequestOptions.DEFAULT);
result = true;
} catch (IOException e) {
log.error("ES保存JSON数据失败:", e);
result = false;
}
return result;
}
public boolean bulk(JSonArray list, String index, String routing) {
boolean result;
Assert.notEmpty(list, BULK_LIST_NULL);
Assert.notNull(index, INDEX_NULL);
BulkRequest bulkRequest = new BulkRequest();
list.forEach(o -> {
JSonObject obj = (JSONObject) o;
IndexRequest indexRequest = new IndexRequest(index).source(obj);
if(isNotEmpty(obj.getString("_id"))){
indexRequest.id(obj.getString("_id"));
}
if(isNotEmpty(routing)){
indexRequest.routing(routing);
}
bulkRequest.add(indexRequest);
});
try {
client.bulk(bulkRequest, RequestOptions.DEFAULT);
result = true;
} catch (IOException e) {
log.error("ES批量插入JSON数据失败:", e);
result = false;
}
return result;
}
public boolean updateById(String id, String index, JSonObject object) {
return updateById(id, index, object, null);
}
public boolean updateById(String id, String index, JSonObject object, String routing) {
boolean result;
Assert.notNull(id, PARAM_ID_NULL);
Assert.notNull(index, INDEX_NULL);
UpdateRequest request = new UpdateRequest(index, id).doc(object);
if (null != routing) {
request.routing(routing);
}
try {
client.update(request, RequestOptions.DEFAULT);
result = true;
} catch (Exception e) {
log.error("指定索引更新JSON失败,Exception:", e);
result = false;
}
return result;
}
}



