在请求里加这个参数
request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
例如:
public boolean saveOrUpdate(String indexName, EsEntity entity, boolean isRefresh) {
boolean isSuccess = false;
IndexRequest request = new IndexRequest(indexName);
request.id(entity.getId());
request.source(JSONUtil.toJsonStr(entity.getData()), XContentType.JSON);
try {
request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
IndexResponse response = highLevelClient.index(request, RequestOptions.DEFAULT);
response.setForcedRefresh(isRefresh);
isSuccess = response.status() == RestStatus.CREATED || response.status() == RestStatus.OK;
} catch (IOException e) {
log.error("saveOrUpdate error:" + e.getMessage(), e);
}
return isSuccess;
}
进行增删改会及时更新es中数据