《安装Elasticsearch教程》
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.5version>
<relativePath/>
parent>
<groupId>com.aoxqwlgroupId>
<artifactId>eedemoartifactId>
<version>1version>
<name>eedemoname>
<description>eedemodescription>
<properties>
<java.version>17java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>cn.easy-esgroupId>
<artifactId>easy-es-boot-starterartifactId>
<version>1.1.1version>
<exclusions>
<exclusion>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclusion>
<exclusion>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
exclusion>
<exclusion>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
<version>7.14.0version>
dependency>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>7.14.0version>
dependency>
dependencies>
easy-es:
enable: true #默认为true,若为false则认为不启用本框架
banner: false # 默认为true 打印banner 若您不期望打印banner,可配置为false
address : 127.0.0.1:9200 # es的连接地址,必须含端口 若为集群,则可以用逗号隔开 例如:127.0.0.1:9200,127.0.0.2:9200
username: elastic #若无 则可省略此行配置
password: WG7WVmuNMtM4GwNYkyWH #若无 则可省略此行配置
# 请求通信超时时间 单位:ms 在平滑模式下,由于要迁移数据,用户可根据数据量大小调整此参数值大小,否则请求容易超时导致索引托管失败,建议您尽量给大不给小
socketTimeout: 5000
global-config:
# 项目是否分布式环境部署,默认为true, 如果是单机运行可填false,将不加分布式锁,效率更高.
distributed: false
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.FieldType;
import cn.easyes.annotation.rely.IdType;
import lombok.Data;
@Data
@IndexName(value = "document")
public class Document {
/**
* es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize,如此id便支持任意数据类型)
*/
@IndexId(type = IdType.NONE)
private String id;
/**
* 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询
*/
@IndexField(value = "title")
private String title;
/**
* 文档内容,指定了类型及存储/查询分词器
*/
@HighLight(mappingField = "highlightContent") //对应下面的highlightContent变量名
@IndexField(value = "content", fieldType = FieldType.TEXT)
private String content;
/**
* 创建时间
*/
@IndexField(value = "create_time", fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss")
private String createTime;
/**
* 不存在的字段
*/
@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;
}
和MyBatis-Plus差不多都是继承mapper,但是Easy-Es不需要继承Service和ServiceImpl
import cn.easyes.core.conditions.interfaces.BaseEsMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface DocumentMapper extends BaseEsMapper<Document> {
}
人家没有帮我们写,那我们就自己写
public interface DocumentService {
Integer insert(Document document);
Document selectById(String id);
Integer updateById(Document document);
Integer deleteById(String id);
List<Document> selectList();
EsPageInfo<Document> pageQuery(Integer pageIndex, Integer pageSize);
Integer deleteBatchIds(List<String> ids);
}
@Service
public class DocumentServiceImpl implements DocumentService {
@Resource
private DocumentMapper documentMapper;
@Override
public Integer insert(Document document) {
return this.documentMapper.insert(document);
}
@Override
public Document selectById(String id) {
return this.documentMapper.selectById(id);
}
@Override
public Integer updateById(Document document) {
return this.documentMapper.updateById(document);
}
@Override
public Integer deleteById(String id) {
return this.documentMapper.deleteById(id);
}
@Override
public List<Document> selectList() {
LambdaEsQueryWrapper<Document> leqw = new LambdaEsQueryWrapper<>();
return this.documentMapper.selectList(leqw);
}
@Override
public EsPageInfo<Document> pageQuery(Integer pageIndex, Integer pageSize) {
LambdaEsQueryWrapper<Document> leqw = new LambdaEsQueryWrapper<>();
return this.documentMapper.pageQuery(leqw, pageIndex, pageSize);
}
@Override
public Integer deleteBatchIds(List<String> ids) {
return this.documentMapper.deleteBatchIds(ids);
}
}
@RestController
@RequestMapping("document")
public class DocumentController {
@Resource
private DocumentService documentService;
/**
* 测试程序是否正常运行
*/
@GetMapping("hello")
public String hello() {
return "hello world!";
}
/**
* 新增
*/
@PostMapping
public Integer insert(@RequestBody Document document) {
return this.documentService.insert(document);
}
/**
* 查询
*/
@GetMapping("{id}")
public Document selectById(@PathVariable String id) {
return this.documentService.selectById(id);
}
/**
* 更新
*/
@PutMapping
public Integer updateById(@RequestBody Document document) {
return this.documentService.updateById(document);
}
/**
* 删除
*/
@DeleteMapping("{id}")
public Integer deleteById(@PathVariable String id) {
return this.documentService.deleteById(id);
}
/**
* 查询列表
*/
@GetMapping
public List<Document> selectList() {
return this.documentService.selectList();
}
/**
* 分页查询
*/
@GetMapping("paging")
public EsPageInfo<Document> pageQuery(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
@RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize) {
return this.documentService.pageQuery(pageIndex, pageSize);
}
/**
* 批量删除
*/
@DeleteMapping
public Integer deleteBatchIds(@RequestBody List<String> ids) {
return this.documentService.deleteBatchIds(ids);
}
}
@SpringBootApplication
@EsMapperScan("com.aoxqwl.eedemo.mapper.ee") //还是那句话,建议和MyBatis-Plus的mapper分开存放
public class EedemoApplication {
public static void main(String[] args) {
SpringApplication.run(EedemoApplication.class, args);
}
}
整体下来和MyBatis-Plus是非常贴合的,如果你的项目中使用到了MyBatis-Plus,那么首选是Easy-Es!同样的Easy-Es目前占卜支持SpringBoot3.X,也是个美中不足吧。但是我想目前大家的生产环境都没上SpringBoot3.X吧!接口那些就自己用Postman测一下吧,都是没有问题的。