EelasticSearch使用!!!

1. Easy-ES介绍

Easy-Es傻瓜级ElasticSearch搜索引擎ORM框架https://www.easy-es.cn/

2. 导入依赖包

XML 复制代码
<dependencies>
        <!--easy-es-boot-starter -->
        <dependency>
            <groupId>cn.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
            <version>2.0.0-beta1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除springboot中自带的es依赖-->
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--es-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.14.0</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3. 在对像属性上添加注解

注解 | Easy-Es傻瓜级ElasticSearch搜索引擎ORM框架https://www.easy-es.cn/pages/v1.x/4eb7db/#esmapperscan

java 复制代码
@IndexName( aliasName = "es_product")
public class EsProduct {

    @IndexId(type = IdType.CUSTOMIZE)
    private Integer id;
    @IndexField( fieldType= FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD,searchAnalyzer = Analyzer.IK_MAX_WORD)
    private String name;
    @IndexField( fieldType= FieldType.INTEGER)
    private Integer categoryId;
    @IndexField( fieldType= FieldType.DOUBLE) // 12.56
    private BigDecimal price;
    @IndexField( fieldType= FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD)
    private String brief;
    @IndexField( fieldType= FieldType.KEYWORD)
    private String img;
    @IndexField( fieldType= FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD)
    private List<String> tags;
    @IndexField( fieldType= FieldType.INTEGER) //198
    private Integer highOpinion;
    @IndexField( fieldType= FieldType.INTEGER)
    private Integer salesVolume;
    @IndexField( fieldType= FieldType.DATE)
    private LocalDateTime productionDate;
}

4.新建Mapper类,类似Mybatis的dao

java 复制代码
import cn.easyes.core.core.BaseEsMapper;

public interface ProductMapper  extends BaseEsMapper<Product> {
}

5.配置ES

bash 复制代码
# Easy-Es配置部分
easy-es:
  # 启用Easy-Es功能
  enable: true
  # 设置Elasticsearch服务器地址和端口
  address: 192.168.23.27:9200
  # 全局配置项,设置是否打印执行的DSL语句(便于调试)
  global-config:
    print-dsl: true

5. 创建、删除、查询索引

java 复制代码
 @Test
    void insert() {
        EsProduct esProduct = EsProduct.builder()
                .id(1)
                .name("小米")
                .img("图片地址")
                .brief("小米(MI)Redmi Note12 5G 120Hz OLED屏幕 骁龙4移动平台 5000mAh长续航 8GB+128GB子夜黑 小米红米")
                .price(new BigDecimal(18))
                .categoryId(1)
                .highOpinion(20)
                .productionDate(LocalDateTime.now())
                .salesVolume(99)
                .tags(CollUtil.newArrayList("120高刷","舒适护眼"))
                .build();
        esMapper.insert(esProduct);
    }
    @Test
    void insert2() {
        delete();
        List<EsProduct> esProducts = CollUtil.newArrayList();
        for (int i = 1; i <= 10; i++) {
            EsProduct esProduct = EsProduct.builder()
                    .id(i)
                    .name("小米"+i)
                    .img("图片地址"+i)
                    .brief("小米(MI)Redmi Note"+i+" 5G 120Hz OLED屏幕 骁龙4移动平台 5000mAh长续航 8GB+128GB子夜黑 小米红米")
                    .price(new BigDecimal(500.36+i))
                    .categoryId(1)
                    .highOpinion(100+i)
                    .productionDate(LocalDateTime.now())
                    .salesVolume(200+i)
                    .tags(CollUtil.newArrayList("12"+i+"高刷","舒适护眼"))
                    .build();
            esProducts.add(esProduct);
                }

        esMapper.insertBatch(esProducts);
    }

    @Test
    void update() {
        EsProduct esProduct = EsProduct.builder()
                .id(1)
                .name("su7")
                .img("图片地址")
                .brief("小米汽车")
                .price(new BigDecimal(18))
                .categoryId(9)
                .highOpinion(20)
                .productionDate(LocalDateTime.now())
                .salesVolume(99)
                .tags(CollUtil.newArrayList("120高刷","舒适护眼"))
                .build();
        Integer integer = esMapper.updateById(esProduct);
    }
    @Test
    void delete() {
        Integer batchIds = esMapper.deleteBatchIds(CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    }

    @Test
    void select() {
        LambdaEsQueryWrapper  queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
        queryWrapper.eq("id",1);
        List list = esMapper.selectList(queryWrapper);
    }
    @Test
    void select2() {
        LambdaEsQueryWrapper  queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
        queryWrapper.queryStringQuery("汽车之家"); //所有字段都去匹配
        List list = esMapper.selectList(queryWrapper);
    }
    @Test
    void select3() {
        LambdaEsQueryWrapper  queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
        queryWrapper.eq("categoryId",1);
        List list = esMapper.selectList(queryWrapper);
    }
    @Test
    void select4() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();
        queryWrapper.in(EsProduct::getCategoryId,1,9);
      
        List list = esMapper.selectList(queryWrapper);
    }

    @Test
    void select5() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();
        //queryWrapper.match(EsProduct::getBrief,"汽车",1.0F);
        //queryWrapper.match(EsProduct::getName,"汽车",1.0F);
        //queryWrapper.multiMatchQuery("汽车", Operator.OR, EsProduct::getName,EsProduct::getBrief);
        queryWrapper.in("categoryId",1,9); //where categroyId in (1,9) and ( name like '%汽车%' or brief like '%汽车%')
        queryWrapper.and(
                w->w.match(EsProduct::getBrief,"汽车",1.0F)
                        .or().match(EsProduct::getName,"汽车",2.0F));
        List list = esMapper.selectList(queryWrapper);
        String dsl = esMapper.getSource(queryWrapper);
        System.out.println(dsl);
    }
    @Test
    void select6() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();

        queryWrapper.in("categoryId",1,9); //where categroyId in (1,9) and ( name like '%汽车%' or brief like '%汽车%')
        queryWrapper.and(
                w->w.match(EsProduct::getBrief,"高刷",1.0F)
                        .or().match(EsProduct::getName,"高刷",2.0F)
                        .or().match(EsProduct::getTags,"高刷",1.0F));
        queryWrapper.orderByDesc(EsProduct::getSalesVolume);
        EsPageInfo<EsProduct> esProductEsPageInfo = esMapper.pageQuery(queryWrapper, 2, 3);

        String dsl = esMapper.getSource(queryWrapper);
        System.out.println(dsl);
    }

    @Test
    void select7() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();


        queryWrapper.match(EsProduct::getName,"水汽车门");
        List<EsProduct> esProducts = esMapper.selectList(queryWrapper);


    }

6. 原生Api调用

6.1查看索引mapping关系

GET /es_product/_mapping

6.2查看某个文档,具体字段的分词

GET /product/_doc/2/_termvectors?fields=brief

相关推荐
李的阿洁1 小时前
OSPF的不规则区域
运维·服务器·网络
网安老伯1 小时前
【2024版】最新kali linux入门及常用简单工具介绍(非常详细)零基础入门到精通,收藏这一篇就够了_kalilinux
linux·运维·服务器·开发语言·web安全·网络安全·xss
是安迪吖2 小时前
基于VirtualBox和Ubuntu的虚拟环境搭建
运维·ubuntu·云计算
skywalk81632 小时前
install fcitx chinese input at FreeBSD14.1
运维·服务器·freebsd
小羊在奋斗2 小时前
【Linux】包管理器、vim详解及简单配置
linux·运维·vim
CS_素锦少年2 小时前
Linux_kernel字符设备驱动12
linux·运维·服务器
醉颜凉3 小时前
银河麒麟服务器操作系统中查询服务器主板型号
运维·操作系统·国产化·kylin os·银河麒麟桌面操作系统
mysql学习中3 小时前
Linux的环境变量
linux·运维·服务器·tcp/ip·centos
BXS_null3 小时前
JWT集成Keycloak
运维·服务器