Easy-Es(简称EE)是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的低码开发框架,在 RestHighLevelClient 的基础上,只做增强不做改变,为简化开发、提高效率而生。
pom引入Easy-Es依赖
io.github.xpc1024
easy-es-boot-starter
0.9.6
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 {
@TableId(type = IdType.CUSTOMIZE)
private Long id;
private String title;
@TableField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
private String content;
@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;
@TableField(exist = false)
private String notExistsField;
@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; public interface DocumentMapper extends BaseEsMapper{ }
启动类添加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 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 search() {
// 查询出所有标题为老汉的文档列表
LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getTitle, "老汉");
return documentMapper.selectList(wrapper);
}
}
更多ES使用方法,参考官网Easy-Es



