easyes 是一个国产的开源软件,提供类似于mybatis-plus查询数据的方式来查询es数据,极大提高了实现es增删改查功能的开发效率
使用方式如下
1.工程导入依赖
<dependency> <groupId>cn.easy-es</groupId> <artifactId>easy-es-boot-starter</artifactId> <!-- <version>1.0.2</version>--> <version>1.1.1</version> </dependency>
2.新增数据模型(和索引对应)
package com.example.eeuse.model; import cn.easyes.annotation.HighLight; import cn.easyes.annotation.IndexField; import cn.easyes.annotation.IndexId; import cn.easyes.annotation.IndexName; import cn.easyes.annotation.rely.Analyzer; import cn.easyes.annotation.rely.FieldStrategy; import cn.easyes.annotation.rely.FieldType; import cn.easyes.annotation.rely.IdType; import lombok.Data; /** * ES数据模型 * <p> * Copyright © 2021 xpc1024 All Rights Reserved **/ @Data @IndexName("document2") public class Document { /** * es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize,如此id便支持任意数据类型) */ @IndexId(type = IdType.CUSTOMIZE) private Long id; /** * 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询 */ private String title; /** * 文档内容,指定了类型及存储/查询分词器 */ @HighLight(mappingField="highlightContent") @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD) private String content; /** * 作者 加@TableField注解,并指明strategy = FieldStrategy.NOT_EMPTY 表示更新的时候的策略为 创建者不为空字符串时才更新 */ @IndexField(strategy = FieldStrategy.NOT_EMPTY) private String creator; /** * 创建时间 */ @IndexField(fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis") private String gmtCreate; /** * es中实际不存在的字段,但模型中加了,为了不和es映射,可以在此类型字段上加上 注解@TableField,并指明exist=false */ @IndexField(exist = false) private String notExistsField; /** * 地理位置经纬度坐标 例如: "40.13933715136454,116.63441990026217" */ @IndexField(fieldType = FieldType.GEO_POINT) private String location; /** * 图形(例如圆心,矩形) */ @IndexField(fieldType = FieldType.GEO_SHAPE) private String geoLocation; /** * 自定义字段名称 */ @IndexField(value = "wu-la") private String customField; /** * 高亮返回值被映射的字段 */ private String highlightContent; }
3.查询接口dao
package com.example.eeuse.mapper; import cn.easyes.core.conditions.interfaces.BaseEsMapper; import com.example.eeuse.model.Document; /** * Mapper * <p> * Copyright © 2021 xpc1024 All Rights Reserved **/ public interface DocumentMapper extends BaseEsMapper<Document> { }
4.springboot启动类加注解扫描
package com.example.eeuse; import cn.easyes.starter.register.EsMapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 启动类 * <p> * Copyright © 2021 xpc1024 All Rights Reserved **/ @SpringBootApplication @EsMapperScan("com.example.eeuse.mapper") public class EeUseApplication { public static void main(String[] args) { SpringApplication.run(EeUseApplication.class, args); } }
5.基础配置yml
easy-es: # address: 10.20.64.228:9200 address: ip:9200 # username: elastic #es用户名,若无则删去此行配置 # password: WG7WVmuNMtM4GwNYkyWH #es密码,若无则删去此行配置 spring: jackson: date-format: yyyy-MM-dd HH:mm:ss server: port: 8080
6.插入与查询测试
package com.example.eeuse.controller; import cn.easyes.core.conditions.LambdaEsQueryWrapper; import com.example.eeuse.mapper.DocumentMapper; import com.example.eeuse.model.Document; 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 java.util.List; /** * 测试使用Easy-ES * <p> * Copyright © 2021 xpc1024 All Rights Reserved **/ @RestController @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class TestUseEeController { private final DocumentMapper documentMapper; @GetMapping("/insert") public Integer insert() { // 初始化-> 新增数据 Document document = new Document(); document.setId(System.currentTimeMillis()); document.setTitle("老汉"); document.setContent("武汉市长江大桥"); document.setCreator("7157"); document.setLocation("40.13933715136454,116.63441990026217"); return documentMapper.insert(document); } @GetMapping("/search") public List<Document> search() { LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>(); wrapper.like(Document::getContent, "长江");//40288fe7859f95720185a04964890015 return documentMapper.selectList(wrapper); } }
调用insert接口测试
调用search接口测试
标签:cn,easyes,private,使用,import,annotation,es From: https://www.cnblogs.com/jiaqirumeng/p/17147931.html