Easy-Es(简称EE)是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的低码开发框架,在 RestHighLevelClient 的基础上,只做增强不做改变,为简化开发、提高效率而生。

pom引入Easy-Es依赖

<dependency>
   <groupId>cn.easy-es</groupId>
   <artifactId>easy-es-boot-starter</artifactId>
   <version>2.0.0-beta1</version>
</dependency>

application.yml配置

easy-es:
  enable: true
#ES地址
  address: 192.168.11.12:9200
  # username: 有设置才填写,非必须
  # password: 有设置才填写,非必须
  keep-alive-millis: 18000
  global-config:
    process-index-mode: smoothly
    async-process-index-blocking: true
    #打印查询dsl
    print-dsl: false

创建实体对象

import com.xpc.easyes.core.anno.HighLightMappingField;
import com.xpc.easyes.core.anno.TableField;
import com.xpc.easyes.core.anno.TableId;
import com.xpc.easyes.core.anno.TableName;
import com.xpc.easyes.core.enums.Analyzer;
import com.xpc.easyes.core.enums.FieldStrategy;
import com.xpc.easyes.core.enums.FieldType;
import com.xpc.easyes.core.enums.IdType;
import lombok.Data;

@Data
@TableName(shardsNum = 3,replicasNum = 2) // 可指定分片数,副本数,若缺省则默认均为1
public class Document implements Serializable {
    /**
     * es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize,如此id便支持任意数据类型)
     */
    @TableId(type = IdType.CUSTOMIZE)
    private Long id;
    /**
     * 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询
     */
    private String title;
    /**
     * 文档内容,指定了类型及存储/查询分词器
     */
    @TableField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
    private String content;
    /**
     * 作者 加@TableField注解,并指明strategy = FieldStrategy.NOT_EMPTY 表示更新的时候的策略为 创建者不为空字符串时才更新
     */
    @TableField(strategy = FieldStrategy.NOT_EMPTY)
    private String creator;
    /**
     * 创建时间
     */
    @TableField(fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
    private String gmtCreate;
    /**
     * es中实际不存在的字段,但模型中加了,为了不和es映射,可以在此类型字段上加上 注解@TableField,并指明exist=false
     */
    @TableField(exist = false)
    private String notExistsField;
    /**
     * 地理位置经纬度坐标 例如: "40.13933715136454,116.63441990026217"
     */
    @TableField(fieldType = FieldType.GEO_POINT)
    private String location;
    /**
     * 图形(例如圆心,矩形)
     */
    @TableField(fieldType = FieldType.GEO_SHAPE)
    private String geoLocation;
    /**
     * 自定义字段名称
     */
    @TableField(value = "wu-la")
    private String customField;

    /**
     * 高亮返回值被映射的字段
     */
    @HighLightMappingField("content")
    private String highlightContent;
}

创建查询mapper接口

import com.example.demo.entity.Document;
import com.xpc.easyes.core.conditions.interfaces.BaseEsMapper;


/**
 * mapper 相当于Mybatis-plus的mapper
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
public interface DocumentMapper extends BaseEsMapper<Document> {
}

 启动类添加mapper扫描注解,@EsMapperScan

 测试Controller

package com.example.demo.controller;

import com.example.demo.entity.Document;
import com.example.demo.mapper.DocumentMapper;
import com.xpc.easyes.core.conditions.LambdaEsIndexWrapper;
import com.xpc.easyes.core.conditions.LambdaEsQueryWrapper;
import com.xpc.easyes.core.enums.FieldType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.List;



@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUseEeController {
    private final DocumentMapper documentMapper;

    @GetMapping("/index")
    public Boolean index() {
        // 初始化-> 创建索引,相当于MySQL建表 | 此接口须首先调用,只调用一次即可
        LambdaEsIndexWrapper<Document> indexWrapper = new LambdaEsIndexWrapper<>();
        indexWrapper.indexName(Document.class.getSimpleName().toLowerCase());
        indexWrapper.mapping(Document::getTitle, FieldType.KEYWORD)
                .mapping(Document::getContent, FieldType.TEXT);
        documentMapper.createIndex(indexWrapper);
        return Boolean.TRUE;
    }
    @GetMapping("/insert")

    public Integer insert() {
        // 初始化-> 新增数据
        Document document = new Document();
        document.setId(1L);
        document.setTitle("老汉");
        document.setContent("推*技术过硬");
        return documentMapper.insert(document);
    }

    @GetMapping("/search")
    @PostConstruct
    public List<Document> search() {
        // 查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, "老汉");
        return documentMapper.selectList(wrapper);
    }
}

更多ES使用方法,参考官网Easy-Es

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐