use of cn.hutool.json.JSONConfig in project kms by mahonelau.
the class KmDocServiceImpl method saveDocToEs.
private Result<?> saveDocToEs(KmDocEsVO kmDocEsVO, String indexId) {
try {
boolean indexExistFlag = true;
if (indexId != null && !indexId.isEmpty()) {
// 通过索引id查询
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds(indexId);
searchSourceBuilder.query(queryBuilder);
// 超时 10S
searchSourceBuilder.timeout(new TimeValue(KMConstant.SearchTimeOutSeconds, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
searchRequest.indices(KMConstant.DocIndexAliasName);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
if (searchResponse.status() != RestStatus.OK) {
return Result.error("从ES查询文档索引失败");
} else {
long c = searchResponse.getHits().getTotalHits().value;
if (c == 0) {
indexExistFlag = false;
} else {
// 更新ES记录
UpdateRequest updateRequest = new UpdateRequest(KMConstant.DocIndexAliasName, indexId);
updateRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
updateRequest.doc(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
if (!updateResponse.status().equals(RestStatus.OK)) {
return Result.error("更新ES发生错误,返回码[" + updateResponse.status().toString() + "]");
} else {
return Result.OK();
}
}
}
} else {
indexExistFlag = false;
}
if (!indexExistFlag) {
// 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
IndexRequest indexRequest = new IndexRequest(KMConstant.DocIndexName);
// 考虑大文件,允许1小时超时时间,前提是异步执行入库ES
indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
indexRequest.source(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
if (!response.status().equals(RestStatus.CREATED)) {
return Result.error("入库ES发生错误,返回码[" + response.status().toString() + "]");
} else {
return Result.OK(response.getId());
}
} else {
return Result.error("未知错误");
}
} catch (Exception e) {
return Result.error("操作ES发生异常:" + e.getMessage());
}
}
use of cn.hutool.json.JSONConfig in project kykms by mahonelau.
the class KmSearchRecordServiceImpl method saveToEs.
private void saveToEs(KmSearchRecordEsVO kmSearchRecordEsVO) {
try {
// 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
String indexSuffix = KMDateUtils.formatDateyyyyMM(DateUtils.getDate());
IndexRequest indexRequest = new IndexRequest(KMConstant.KMSearchRecordIndexName + "_" + indexSuffix);
indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutMinutes));
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
indexRequest.source(new JSONObject(kmSearchRecordEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATETIME_PATTERN)).toString(), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
if (!response.status().equals(RestStatus.CREATED)) {
log.error("入库ES发生错误,返回码:" + response.status().toString());
} else
log.debug("搜索记录入库ES成功");
} catch (Exception e) {
log.error("入库ES发生错误", e);
}
}
use of cn.hutool.json.JSONConfig in project kms by mahonelau.
the class KmDocVisitRecordServiceImpl method saveToEs.
private void saveToEs(KmDocVisitRecordEsVO kmDocVisitRecordEsVO) {
try {
// 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
String indexSuffix = KMDateUtils.formatDateyyyyMM(DateUtils.getDate());
IndexRequest indexRequest = new IndexRequest(KMConstant.DocVisitIndexName + "_" + indexSuffix);
indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutMinutes));
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
indexRequest.source(new JSONObject(kmDocVisitRecordEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATETIME_PATTERN)).toString(), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
if (!response.status().equals(RestStatus.CREATED)) {
log.error("入库ES发生错误,返回码:" + response.status().toString());
} else
log.debug("访问记录入库ES成功");
} catch (Exception e) {
log.error("入库ES发生错误", e);
}
}
use of cn.hutool.json.JSONConfig in project kms by mahonelau.
the class KmSearchRecordServiceImpl method saveToEs.
private void saveToEs(KmSearchRecordEsVO kmSearchRecordEsVO) {
try {
// 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
String indexSuffix = KMDateUtils.formatDateyyyyMM(DateUtils.getDate());
IndexRequest indexRequest = new IndexRequest(KMConstant.KMSearchRecordIndexName + "_" + indexSuffix);
indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutMinutes));
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
indexRequest.source(new JSONObject(kmSearchRecordEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATETIME_PATTERN)).toString(), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
if (!response.status().equals(RestStatus.CREATED)) {
log.error("入库ES发生错误,返回码:" + response.status().toString());
} else
log.debug("搜索记录入库ES成功");
} catch (Exception e) {
log.error("入库ES发生错误", e);
}
}
use of cn.hutool.json.JSONConfig in project kykms by mahonelau.
the class KmDocServiceImpl method saveDocToEs.
private Result<?> saveDocToEs(KmDocEsVO kmDocEsVO, String indexId) {
try {
boolean indexExistFlag = true;
if (indexId != null && !indexId.isEmpty()) {
// 通过索引id查询
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds(indexId);
searchSourceBuilder.query(queryBuilder);
// 超时 10S
searchSourceBuilder.timeout(new TimeValue(KMConstant.SearchTimeOutSeconds, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
searchRequest.indices(KMConstant.DocIndexAliasName);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
if (searchResponse.status() != RestStatus.OK) {
return Result.error("从ES查询文档索引失败");
} else {
long c = searchResponse.getHits().getTotalHits().value;
if (c == 0) {
indexExistFlag = false;
} else {
// 更新ES记录
UpdateRequest updateRequest = new UpdateRequest(KMConstant.DocIndexAliasName, indexId);
updateRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
updateRequest.doc(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
if (!updateResponse.status().equals(RestStatus.OK)) {
return Result.error("更新ES发生错误,返回码[" + updateResponse.status().toString() + "]");
} else {
return Result.OK();
}
}
}
} else {
indexExistFlag = false;
}
if (!indexExistFlag) {
// 插入数据,index不存在则自动根据匹配到的template创建。index没必要每天创建一个,如果是为了灵活管理,最低建议每月一个 yyyyMM。
IndexRequest indexRequest = new IndexRequest(KMConstant.DocIndexName);
// 考虑大文件,允许1小时超时时间,前提是异步执行入库ES
indexRequest.timeout(TimeValue.timeValueHours(KMConstant.SaveTimeOutHours));
indexRequest.source(new JSONObject(kmDocEsVO, new JSONConfig().setDateFormat(DatePattern.NORM_DATE_PATTERN)).toString(), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
if (!response.status().equals(RestStatus.CREATED)) {
return Result.error("入库ES发生错误,返回码[" + response.status().toString() + "]");
} else {
return Result.OK(response.getId());
}
} else {
return Result.error("未知错误");
}
} catch (Exception e) {
return Result.error("操作ES发生异常:" + e.getMessage());
}
}
Aggregations