<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- 基于restful的es java客户端jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
</dependency>
package com.bjcib.xbb.guxiao.abs.insurance.process.services.impl;
import com.bjcib.xbb.guxiao.abs.insurance.apis.bean.InsuranceClause;
import com.bjcib.xbb.guxiao.abs.insurance.apis.bean.InsuranceClauseEsEntity;
import com.bjcib.xbb.guxiao.abs.insurance.apis.bean.InsuranceProduct;
import com.bjcib.xbb.guxiao.abs.insurance.apis.bean.InsuranceProductVo;
import com.bjcib.xbb.guxiao.abs.ums.InsurancePlatform;
import com.bjcib.xbb.guxiao.abs.ums.InsuranceType;
import com.bjcib.xbb.guxiao.abs.insurance.apis.service.InsuranceClauseDubboService;
import com.bjcib.xbb.ls.staticTool.ConstantsEs;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
le.gson.JsonElement;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import *;
import org.apachemons.lang3.tuple.Pair;
import org.fig.annotation.Service;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import ansaction.annotation.Transactional;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @Description: $description
* @Author: FanBo
* @Date: 2020/2/26
*/
@Service
public class InsuranceClauseDubboServiceImpl extends AbstractService implements InsuranceClauseDubboService {
private Logger logger = Logger(InsuranceClauseDubboServiceImpl.class);
@Autowired
private JestClient jestClient;
@Override
public Pair<List<InsuranceClauseEsEntity>, Long> searchClauseFromEs(String searchKey, int pageSize, int pageNo, fields) {
try {
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags(ConstantsEs.HIGHLIGHT_PREFIX); //高亮前缀
highlightBuilder.postTags(ConstantsEs.HIGHLIGHT_SUFFIX); //高亮后缀
highlightBuilder.field(fields[0]); //设置高亮字段
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(pageSize).from(pageNo); //分页
searchSourceBuilder.query(QueryBuilders.multiMatchQuery(searchKey, fields)); //多个字段搜索。如果单个字段用matchQuery
searchSourceBuilder.highlighter(highlightBuilder); //设置高亮信息
Search search = new Search.String()).addIndex(ConstantsEs.INDEX).addType(ConstantsEs.TYPE).build();
JestResult result = ute(search);
JsonElement jsonElement = ("hits");
JsonElement totalJsonElement = ("total");
JsonElement totalValue = ("value");
//总数量
Long totalNum = AsLong();
logger.info("totalNum: " + totalNum);
List<SearchResult.Hit<InsuranceClauseEsEntity, Void>> hits = ((SearchResult) result).getHits(InsuranceClauseEsEntity.class);
List<InsuranceClauseEsEntity> resultList = new LinkedList<>();
hits.forEach(hit -> {
//ES数据
InsuranceClauseEsEntity esEntity = hit.source;
//高亮数据
Map<String, List<String>> highlightMap = hit.highlight;
if (highlightMap != null) {
List<String> highlightList = (fields[0]);
//将高亮数据返给前端
esEntity.(0));
}
resultList.add(esEntity);
});
logger.info("resultList: " + resultList);
return Pair.of(resultList, totalNum);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public int addClauseToEs(List<InsuranceClauseEsEntity> entities) {
try {
Bulk.Builder bulk = new Bulk.Builder();
for (InsuranceClauseEsEntity esEntity : entities) {
Index index = new Index.Builder(esEntity).index(ConstantsEs.INDEX).type(ConstantsEs.TYPE).build();
bulk.addAction(index);
}
JestResult result = ute(bulk.build());
System.out.println("向ES添加数据结果: " + result);
boolean isSucceeded = result.isSucceeded();
return isSucceeded ? 1 : 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
@Override
public int addClauseToEsByClauseId(Long clauseId) {
//查询数据库数据
InsuranceClauseEsEntity esEntity = insuranceClauseMapper.selectByClauseId(clauseId);
//条款类型转成中文
esEntity.Value(Integer.InsuranceType())));
//条款平台转成中文
esEntity.Value(Integer.Platform())));
List<InsuranceClauseEsEntity> esEntities = new LinkedList<>();
esEntities.add(esEntity);
//写入ES
int result = addClauseToEs(esEntities);
return result;
}
@Override
public int id) {
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.sQuery("id", id)); //通过id字段删除
DeleteByQuery deleteByQuery = new DeleteByQuery.String()).addIndex(ConstantsEs.INDEX).addType(ConstantsEs.TYPE).build();
JestResult result = ute(deleteByQuery);
System.out.println("向ES删除数据结果: " + result);
Double object = (Value("deleted");
return object.intValue();
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
@Override
public int flushEs(String index, String type) {
return 0;
}
@Override
public int updateClauseFromEs(Long id) {
//先从ES删除数据
int delResult = delClauseFromEs(id);
int addResult = 0;
if (delResult > 0) {
//从数据库查询最新的数据
InsuranceClauseEsEntity c = insuranceClauseMapper.selectByClauseId(id);
c.Value(Integer.InsuranceType())));
c.Value(Integer.Platform())));
List<InsuranceClauseEsEntity> esEntities = new LinkedList<>();
esEntities.add(c);
//将最新的数据重新写入ES
addResult = addClauseToEs(esEntities);
}
//删除和新增都成功算成功
return (delResult > 0 && addResult > 0) ? 1 : 0;
}
@Override
public PageInfo<Map<String, Object>> insuranceProductList(Map<String, Object> params) {
logger.info("insuranceProductList:"+params);
PageHelper.startPage(Integer.("pageNo").toString()), Integer.("pageSize").toString()));
List<Map<String,Object>> list = insuranceProductMapper.insuranceProductList(params);
PageInfo<Map<String,Object>> orderVoPageInfo = new PageInfo<>(list);
return orderVoPageInfo;
}
@Override
public InsuranceProduct addInsuranceProduct(InsuranceProduct insuranceProduct) {
logger.info("addInsuranceProduct:"+insuranceProduct);
insuranceProductMapper.insertSelective(insuranceProduct);
return insuranceProduct;
}
@Override
public int updateInsuranceProduct(InsuranceProduct insuranceProduct) {
logger.info("updateInsuranceProduct:"+insuranceProduct);
return insuranceProductMapper.updateByPrimaryKeySelective(insuranceProduct);
}
@Override
public InsuranceClause addInsuranceClause(InsuranceClause insuranceClause) {
logger.info("addInsuranceClause:"+insuranceClause);
insuranceClauseMapper.insertSelective(insuranceClause);
return insuranceClause;
}
@Override
public int deleteClause(Map<String, Object> paramMap) {
logger.info("deleteClause:"+paramMap);
//当前产品所有的条款
List<InsuranceClause> clauses = insuranceClauseMapper.selectByMap(paramMap);
//条款id
Long[] ids = clauses.stream().map(c -> c.getId()).toArray(Long[] :: new);
//批量删除
int delRes = delClauseFromEs(ids);
logger.info("ES条款ids:" + ids + "删除结果:" + delRes);
//删除数据库条款
int delResult = insuranceClauseMapper.deleteClause(paramMap);
return delResult;
}
@Override
public InsuranceProductVo getProductAndClauses(Map<String, String> params) {
logger.info("getProductAndClauses:"+params);
ProductAndClauses(params);
}
@Override
public List<InsuranceProductVo> getProductList(Map<String, Object> params) {
ProductList(params);
}
@Override
public int getProductListTotalCount(Map<String, Object> params) {
ProductListTotalCount(params);
}
@Override
public List<InsuranceProductVo> getProductListFromXbb(Map<String, Object> params) {
ProductListFromXbb(params);
}
@Override
public int getProductListTotalCountFromXbb(Map<String, Object> params) {
ProductListTotalCountFromXbb(params);
}
@Override
public List<InsuranceProductVo> getProductListDefault(Map<String, Object> params) {
ProductListDefault(params);
}
@Override
public int getProductListDefaultCount(Map<String, Object> params) {
ProductListDefaultCount(params);
}
@Override
public List<InsuranceClause> getClauseList(Long productId) {
Map<String, Object> param = new HashMap<>();
param.put("product_id", productId);
param.put("is_delete", 0);
List<InsuranceClause> clauses = insuranceClauseMapper.selectByMap(param);
return clauses;
}
@Override
@Transactional
public int deletePro(Map<String, String> params) {
logger.info("deletePro "+params);
int flag = insuranceProductMapper.delPro(params);
if(flag == 0){
return 0;
}
Map<String,Object> map = new HashMap<>();
map.put("product_id",("id"));
logger.info("deleteClause "+map);
insuranceClauseMapper.deleteClause(map);
return 1;
}
}
本文发布于:2024-01-28 23:22:35,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170645535811036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |