Easy-es

1.介绍

Easy-Es是一款由国内开发者打造并完全开源的ElasticSearch-ORM框架
easy-es-to-data-es.png

2.设计理念-简单优于复杂

easy-es-2.png

easy-es-2-1.png

3.使用

1.maven pom

elasticsearch.high.version高版本坑:org.elasticsearch.common.xcontent.XContentType类找不到根据官方回应是ES官方将XContentType这个类的路径调整了 目前2.0.0 未适配 建议使用7.10.1版本的elasticsearch.high.version

   <properties>
        <elasticsearch.high.version>7.10.1</elasticsearch.high.version>
        <easy-es.version>2.0.0</easy-es.version>
        <hutool-all.version>5.8.25</hutool-all.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool-all.version}</version>
        </dependency>
        <!--注意要与运行的elasticsearch版本一致-->
        <dependency>
            <groupId>org.dromara.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
            <version>${easy-es.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elasticsearch.high.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.high.version}</version>
        </dependency>
      </dependencies>

2.代码

@Data
@EqualsAndHashCode
@IndexName(value = "pms")
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class EsProduct implements Serializable {
    private static final long serialVersionUID = -1L;
    @IndexId(type = IdType.CUSTOMIZE)
    private Long id;
    @IndexField(fieldType = FieldType.KEYWORD)
    private String productSn;
    private Long brandId;
    @IndexField(fieldType = FieldType.KEYWORD)
    private String brandName;
    private Long productCategoryId;
    @IndexField(fieldType = FieldType.KEYWORD)
    private String productCategoryName;
    private String pic;
    @IndexField(fieldType = FieldType.TEXT, analyzer = "ik_max_word")
    private String name;
    @IndexField(fieldType = FieldType.TEXT, analyzer = "ik_max_word")
    private String subTitle;
    @IndexField(fieldType = FieldType.TEXT, analyzer = "ik_max_word")
    private String keywords;
    private BigDecimal price;
    private Integer sale;
    private Integer newStatus;
    private Integer recommandStatus;
    private Integer stock;
    private Integer promotionType;
    private Integer sort;
    @IndexField(fieldType = FieldType.NESTED, nestedClass = EsProductAttributeValue.class)
    private List<EsProductAttributeValue> attrValueList;
    @Score
    private Float score;
}
/**
 * @Author yan
 * @Date 2024/8/13 0013 13:54:02
 * @Description
 */
@Data
@EqualsAndHashCode
public class EsProductAttributeValue implements Serializable {
    private static final long serialVersionUID = 1L;
    @IndexField(fieldType = FieldType.LONG)
    private Long id;
    @IndexField(fieldType = FieldType.KEYWORD)
    private Long productAttributeId;
    //属性值
    @IndexField(fieldType = FieldType.KEYWORD)
    private String value;
    //属性参数:0->规格;1->参数
    @IndexField(fieldType = FieldType.INTEGER)
    private Integer type;
    //属性名称
    @IndexField(fieldType=FieldType.KEYWORD)
    private String name;
}
/**
 * 商品ES操作类
 * Created by macro on 2018/6/19.
 */
@Repository
public interface EsProductMapper extends BaseEsMapper<EsProduct> {

}
/**
 * @Author yan
 * @Date 2024/8/13 0013 13:59:04
 * @Description
 */
public interface EsProductService {
    int importAll();

    void delete(Long id);

    EsProduct create(Long id);

    void delete(List<Long> ids);

    List<EsProduct> select();

    EsPageInfo<EsProduct> recommend(Long id, Integer pageNum, Integer pageSize);
}

/**
 * 搜索商品管理Service实现类
 * Created by macro on 2018/6/19.
 */
@Service
public class EsProductServiceImpl implements EsProductService {
    @Resource
    private EsProductMapper esProductMapper;
    @Override
    public int importAll() {

        List<EsProduct> esProductList = CollUtil.newArrayList(
                new EsProduct().setId(1L).setName("iphone1").setPrice(new BigDecimal("10000")),
                new EsProduct().setId(2L).setName("iphone2").setPrice(new BigDecimal("20000")),
                new EsProduct().setId(3L).setName("iphone3").setPrice(new BigDecimal("30000")),
                new EsProduct().setId(4L).setName("iphone4").setPrice(new BigDecimal("40000")),
                new EsProduct().setId(5L).setName("iphone5").setPrice(new BigDecimal("50000"))
        );

        return esProductMapper.insertBatch(esProductList);
    }

    @Override
    public void delete(Long id) {
        esProductMapper.deleteById(id);
    }

    @Override
    public EsProduct create(Long id) {
        EsProduct result = null;
        List<EsProduct> esProductList = CollUtil.newArrayList(
                new EsProduct().setId(1L).setName("iphone1").setPrice(new BigDecimal("10000")),
                new EsProduct().setId(2L).setName("iphone2").setPrice(new BigDecimal("20000")),
                new EsProduct().setId(3L).setName("iphone3").setPrice(new BigDecimal("30000")),
                new EsProduct().setId(4L).setName("iphone4").setPrice(new BigDecimal("40000")),
                new EsProduct().setId(5L).setName("iphone5").setPrice(new BigDecimal("50000"))
        );
        if (esProductList.size() > 0) {
            result = esProductList.get(0);
            esProductMapper.insert(result);
        }
        return result;
    }

    @Override
    public void delete(List<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            esProductMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public List<EsProduct> select() {
        LambdaEsQueryWrapper<EsProduct> wrapper = new LambdaEsQueryWrapper<>();
        return esProductMapper.selectList(wrapper);
    }

    @Override
    public EsPageInfo<EsProduct> recommend(Long id, Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<EsProduct> wrapper = new LambdaEsQueryWrapper<>();
    /*    List<EsProduct> esProductList = esProductMapper.getAllEsProductList(id);
        if (esProductList.size() > 0) {
            EsProduct esProduct = esProductList.get(0);
            String keyword = esProduct.getName();
            Long brandId = esProduct.getBrandId();
            Long productCategoryId = esProduct.getProductCategoryId();
            //用于过滤掉相同的商品
            wrapper.not(EsProduct::getId,id);
            //根据商品标题、品牌、分类进行搜索
            wrapper.and(i -> i.match(EsProduct::getName, keyword, 8f)
                    .or().match(EsProduct::getSubTitle, keyword, 2f)
                    .or().match(EsProduct::getKeywords, keyword, 2f)
                    .or().match(EsProduct::getBrandId, brandId, 5f)
                    .or().match(EsProduct::getProductCategoryId, productCategoryId, 3f));
            return esProductMapper.pageQuery(wrapper, pageNum, pageSize);
        }*/
        return esProductMapper.pageQuery(wrapper, pageNum, pageSize);
    }
}

@RestController 
@RequestMapping(value = "/product")
public class ProductController {
 
    @GetMapping(value = "/importAll")
    public String getProduct(){
        EsProductService bean = SpringUtil.getBean(EsProductService.class);
        bean.importAll();
        return "product";
    }
    @GetMapping(value = "/getProductAll")
    @ResponseBody
    public JSONObject getProductAll(){
        EsProductService bean = SpringUtil.getBean(EsProductService.class);
        List<EsProduct> select = bean.select();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",select);
        jsonObject.put("code",200);
        jsonObject.put("msg","ok");
        return jsonObject;
    }
}