Search in sources :

Example 1 with ElasticEntity

use of com.wayn.data.elastic.manager.ElasticEntity in project waynboot-mall by wayn111.

the class GoodsServiceImpl method syncGoods2Es.

/**
 * 同步商品信息到es中
 *
 * @param goods 商品信息
 */
public boolean syncGoods2Es(Goods goods) throws IOException {
    // 同步es
    ElasticEntity elasticEntity = new ElasticEntity();
    elasticEntity.setId(goods.getId().toString());
    Map<String, Object> map = new HashMap<>();
    map.put("id", goods.getId());
    map.put("name", goods.getName());
    map.put("countPrice", goods.getCounterPrice());
    map.put("retailPrice", goods.getRetailPrice());
    map.put("keyword", Objects.isNull(goods.getKeywords()) ? Collections.emptyList() : goods.getKeywords().split(","));
    map.put("isOnSale", goods.getIsOnSale());
    map.put("createTime", goods.getCreateTime());
    elasticEntity.setData(map);
    if (!elasticDocument.insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity)) {
        throw new BusinessException("商品同步es失败");
    }
    return true;
}
Also used : BusinessException(com.wayn.common.exception.BusinessException) ElasticEntity(com.wayn.data.elastic.manager.ElasticEntity)

Example 2 with ElasticEntity

use of com.wayn.data.elastic.manager.ElasticEntity in project waynboot-mall by wayn111.

the class GoodsController method syncEs.

@PostMapping("syncEs")
public R syncEs() {
    if (redisCache.getCacheObject(SysConstants.ES_GOODS_INDEX_KEY) != null) {
        return R.error(ReturnCodeEnum.CUSTOM_ERROR.setMsg("正在同步,请稍等"));
    }
    boolean flag = false;
    redisCache.setCacheObject(SysConstants.ES_GOODS_INDEX_KEY, true, 3, TimeUnit.MINUTES);
    try {
        elasticDocument.deleteIndex(SysConstants.ES_GOODS_INDEX);
        InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME);
        if (elasticDocument.createIndex(SysConstants.ES_GOODS_INDEX, FileUtils.getContent(inputStream))) {
            List<Goods> list = iGoodsService.list();
            List<ElasticEntity> entities = new ArrayList<>();
            for (Goods goods : list) {
                ElasticEntity elasticEntity = new ElasticEntity();
                Map<String, Object> map = new HashMap<>();
                elasticEntity.setId(goods.getId().toString());
                map.put("id", goods.getId());
                map.put("name", goods.getName());
                map.put("sales", goods.getActualSales() + goods.getVirtualSales());
                map.put("isHot", goods.getIsHot());
                map.put("isNew", goods.getIsNew());
                map.put("countPrice", goods.getCounterPrice());
                map.put("retailPrice", goods.getRetailPrice());
                map.put("keyword", goods.getKeywords().split(","));
                map.put("isOnSale", goods.getIsOnSale());
                map.put("createTime", goods.getCreateTime());
                elasticEntity.setData(map);
                entities.add(elasticEntity);
            }
            flag = elasticDocument.insertBatch("goods", entities);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        redisCache.deleteObject(SysConstants.ES_GOODS_INDEX_KEY);
    }
    return R.result(flag);
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ElasticEntity(com.wayn.data.elastic.manager.ElasticEntity) Goods(com.wayn.common.core.domain.shop.Goods) IOException(java.io.IOException)

Aggregations

ElasticEntity (com.wayn.data.elastic.manager.ElasticEntity)2 Goods (com.wayn.common.core.domain.shop.Goods)1 BusinessException (com.wayn.common.exception.BusinessException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1