网站太花哨,外贸网站建站要多少钱,wordpress个人支付宝,网站建设的网页怎么做一.什么是Easy-Es Easy-Es#xff08;简称EE#xff09;是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的ORM开发框架#xff0c;在 RestHighLevelClient 的基础上,只做增强不做改变#xff0c;为简化开发、提高效率而生,您如果有用过Mybatis-Plus(简称…一.什么是Easy-Es
Easy-Es简称EE是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的ORM开发框架在 RestHighLevelClient 的基础上,只做增强不做改变为简化开发、提高效率而生,您如果有用过Mybatis-Plus(简称MP),那么您基本可以零学习成本直接上手EE,EE是MP的Es平替版,在有些方面甚至比MP更简单,同时也融入了更多Es独有的功能,助力您快速实现各种场景的开发. 二.为什么要使用Easy-Es
EasyEs是一个基于 ElasticSearch官方提供的 RestHighLevelClient开发的 ORM框架它的设计理念类似于 Mybatis-Plus旨在简化开发、提高效率。使用EasyEs的主要原因包括 简化操作EasyEs让开发者无需掌握复杂的DSL语句MySQLElasticsearch提高效率对于熟悉Mybatis-Plus易于集成EasyEs作为一款轻量级的ORM框架对现有工程的影响小几乎无侵入性启动时会自动注入基本的CRUD操作性能基本无损耗使得开发者能够直接面向对象操作提高了开发效率。降低成本在特定的应用场景下如大型数据的存储和检索EasyEs通过优化存储和索引方式如使用ZSTD压缩功能快照备份S3存储 综上所述使用EasyEs可以显著简化Elasticsearch的操作提高开发效率同时降低数据存储和检索的成本是开发和运维人员值得考虑的选择. 三.Spring整合Easy-Es
1.依赖
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!-- lombok插件依赖 --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdscopeprovided/scope/dependency!-- Easy-Es暂不支持SpringBoot3.X且推荐Elasticsearch版本为7.14.0 --dependencygroupIdcn.easy-es/groupIdartifactIdeasy-es-boot-starter/artifactIdversion1.1.1/versionexclusionsexclusiongroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclusionexclusiongroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactId/exclusionexclusiongroupIdorg.elasticsearch/groupIdartifactIdelasticsearch/artifactId/exclusion/exclusions/dependencydependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.14.0/version/dependencydependencygroupIdorg.elasticsearch/groupIdartifactIdelasticsearch/artifactIdversion7.14.0/version/dependency
/dependencies
2.配置
easy-es:enable: trueaddress : 服务器地址:9200global-config:process_index_mode: manual
3.启动类
SpringBootApplication
EsMapperScan(com.easyes.mapper)
public class EasyEsApplication {public static void main(String[] args) {SpringApplication.run(EasyEsApplication.class, args);}}
4.mapper public interface DocumentMapper extends BaseEsMapperDocument {}
5.service
package com.easyes.service;import com.easyes.entity.Document;import java.util.List;public interface IDocumentService{/*** 查询ES所有数据* return 查询Document结果对象集合*/ListDocument findAllData();/*** 创建索引* return 结果信息* throws Exception*/String createIndex() throws Exception;/*** 删除索引* return 结果信息*/String deleteIndex();/*** ES新增数据* param document 新增数据实体类* return 结果信息* throws Exception*/String addData(Document document) throws Exception;/*** 根据id删除ES数据* param id 需要删除的数据的id* return*/String deleteDataById(String id);/*** 修改ES数据* param document 修改数据对象*/String updateData(Document document);/*** 分词匹配查询content字段* param value 查询内容* return*/ListDocument findMatch(String value);/*** 根据id查询数据* param id 查询id* return*/Document findById(String id);
}
6.serviceImpl
package com.easyes.service.impl;import cn.easyes.common.utils.StringUtils;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.easyes.entity.Document;
import com.easyes.mapper.DocumentMapper;
import com.easyes.service.IDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Date;
import java.util.List;Service
public class DocumentServiceImpl implements IDocumentService {Autowiredprivate DocumentMapper documentMapper;/*** 查询ES所有数据* return 查询Document结果对象集合*/Overridepublic ListDocument findAllData() {LambdaEsQueryWrapperDocument wrapper new LambdaEsQueryWrapper();wrapper.matchAllQuery();return documentMapper.selectList(wrapper);}/*** 创建索引* return 结果信息* throws Exception*/Overridepublic String createIndex() throws Exception {StringBuilder msg new StringBuilder();String indexName Document.class.getSimpleName().toLowerCase();boolean existsIndex documentMapper.existsIndex(indexName);if (existsIndex){throw new Exception(Document实体对应索引已存在,删除索引接口deleteIndex);}boolean success documentMapper.createIndex();if (success){msg.append(Document索引创建成功);}else {msg.append(索引创建失败);}return msg.toString();}/*** 删除索引* return 结果信息*/Overridepublic String deleteIndex() {StringBuilder msg new StringBuilder();String indexName Document.class.getSimpleName().toLowerCase();if (documentMapper.deleteIndex(indexName)){msg.append(删除成功);}else {msg.append(删除失败);}return msg.toString();}/*** ES新增数据* param document 新增数据实体类* return 结果信息* throws Exception*/Overridepublic String addData(Document document) throws Exception {if (StringUtils.isEmpty(document.getTitle()) || StringUtils.isEmpty(document.getContent())) {throw new Exception(请补全title及content数据);}document.setCreateTime(new Date());documentMapper.insert(document);return Added successfully;}/*** 根据id删除ES数据* param id 需要删除的数据的id* return*/Overridepublic String deleteDataById(String id) {documentMapper.deleteById(id);return Success;}/*** 修改ES数据* param document 修改数据对象*/Overridepublic String updateData(Document document) {documentMapper.updateById(document);return Success;}/*** 分词匹配查询content字段* param value 查询内容* return*/Overridepublic ListDocument findMatch(String value) {LambdaEsQueryWrapperDocument wrapper new LambdaEsQueryWrapper();wrapper.match(Document::getContent,value);wrapper.orderByDesc(Document::getCreateTime);ListDocument documents documentMapper.selectList(wrapper);return documents;}/*** 根据id查询数据* param id* return*/Overridepublic Document findById(String id) {return null;}
}7.controller
package com.easyes.controller;import com.easyes.entity.Document;
import com.easyes.service.IDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import java.util.List;RestController
public class DocumentController {Autowiredprivate IDocumentService iDocumentService;/*** 创建索引* return 结果信息* throws Exception*/GetMapping(/createIndex)public String createIndex() throws Exception {return iDocumentService.createIndex();}/*** 删除索引* return 结果信息*/GetMapping(/deleteIndex)public String deleteIndex(){return iDocumentService.deleteIndex();}/*** 查询ES所有数据* return 查询Document结果对象集合*/GetMapping(/findAll)public ListDocument findAll(){return iDocumentService.findAllData();}/*** 根据id查询数据* param id 查询数据的id* return 查询Document结果对象*/GetMapping(/findById)public Document findById(String id){return iDocumentService.findById(id);}/*** ES新增数据* param document 新增数据对象* return 结果信息* throws Exception*/GetMapping(/add)public String addData(RequestBody Document document) throws Exception {return iDocumentService.addData(document);}/*** 修改ES数据* param document 修改数据对象*/GetMapping(/update)public String updateData(RequestBody Document document){return iDocumentService.updateData(document);}/*** 根据id删除ES数据* param id 需要删除的数据的id* return*/GetMapping(/delete)public String deleteData(String id){return iDocumentService.deleteDataById(id);}/*** 分词匹配查询content字段* param value 查询内容* return*/GetMapping(/match)public ListDocument findMatch(String value){return iDocumentService.findMatch(value);}
}
实体类注解
// Lombok 注解自动为类生成 getter 和 setter 方法
Data
// 指定 Elasticsearch 索引名及分片数
IndexName(value user_es, shardsNum 3)
public class User {/*** 用户ID* 使用自定义ID类型*/IndexId(type IdType.CUSTOMIZE)private Integer id;/*** 用户姓名* 字段类型为 TEXT使用 IK 最大词元分词器进行索引和搜索* 在搜索结果中匹配的部分将被高亮显示*/IndexField(value userName, fieldType FieldType.TEXT, analyzer Analyzer.IK_MAX_WORD, searchAnalyzer Analyzer.IK_MAX_WORD)HighLight(preTag span style\color:red\, postTag /span)private String name;/*** 用户年龄* 字段类型为 INTEGER*/IndexField(fieldType FieldType.INTEGER)private Integer age;/*** 用户工资* 字段类型为 DOUBLE*/IndexField(fieldType FieldType.DOUBLE)private BigDecimal salary;/*** 用户生日* 字段类型为 DATE*/IndexField(fieldType FieldType.DATE)private Date birthday;
}