文章目录
- SpringBoot集成Easy-Es
- 一、集成demo
- 二、索引CRUD
-
- 三、数据CURD
- 四、参数文档
SpringBoot集成Easy-Es
Easy-Es(简称EE)是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的ORM开发框架,在 RestHighLevelClient 的基础上,只做增强不做改变,为简化开发、提高效率而生
一、集成demo
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.14.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.14.1</version>
</dependency>
<dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2、配置信息
# 默认为true,若为false时,则认为不启用本框架
easy-es.enable: true
#填你的es连接地址
easy-es.address : 127.0.0.1:9200
# username: 有设置才填写,非必须
easy-es.username : elastic
# password: 有设置才填写,非必须
easy-es.password : 123456
3、启动类中添加 @EsMapperScan 注解,扫描 Mapper 文件夹
@SpringBootApplication
@EsMapperScan("com.example.elasticsearch.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4、实体类和mapper
@Data
public class Document {
@IndexId(type = IdType.NONE)
private String id;
private String title;
@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
private String content;
}
public interface DocumentMapper extends BaseEsMapper<Document> {
}
5、测试
@RestController
public class EasyEsController {
@Autowired
private DocumentMapper documentMapper;
@GetMapping("/insert")
public Integer insert() {
Document document = new Document();
document.setTitle("老汉");
document.setContent("推*技术过硬");
return documentMapper.insert(document);
}
@GetMapping("/search")
public List<Document> search() {
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getTitle, "老汉");
return documentMapper.selectList(wrapper);
}
}
http://localhost:8080/insert(插入数据)
http://localhost:8080/search(查询数据)
二、索引CRUD
首先说一下索引的托管模式,EE这里有三种托管模式
- 自动托管之平滑模式(默认):在此模式下,索引的创建更新数据迁移等全生命周期用户均不需要任何操作即可完成
- 自动托管之非平滑模式:在此模式下,索引额创建及更新由EE全自动异步完成,但不处理数据迁移工作
- 手动模式:在此模式下,索引的所有维护工作EE框架均不介入,由用户自行处理,EE提供了开箱即用的索引CRUD相关API
前置条件
索引CRUD相关的API都属于手动挡范畴,因此我们执行下述所有API前必须先配置开启手动挡,以免和自动挡冲突
easy-es:
global-config:
process_index_mode: manul
创建索引
@Test
void createIndex01(){
documentMapper.createIndex();
}
@Test
void createIndex02(){
String indexName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
documentMapper.createIndex(indexName);
}
@Test
void createIndex03() {
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
wrapper.indexName(Document.class.getSimpleName().toLowerCase());
wrapper.mapping(Document::getTitle, FieldType.KEYWORD, 2.0f)
.mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);
wrapper.settings(3, 2);
boolean isOk = documentMapper.createIndex(wrapper);
}
查询索引
@Test
public void testExistsIndex() {
String indexName = Document.class.getSimpleName().toLowerCase();
boolean existsIndex = documentMapper.existsIndex(indexName);
Assertions.assertTrue(existsIndex);
}
@Test
public void testGetIndex() {
GetIndexResponse indexResponse = documentMapper.getIndex();
indexResponse.getMappings().forEach((k, v) -> System.out.println(v.getSourceAsMap()));
}
更新索引
@Test
public void testUpdateIndex() {
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
String indexName = Document.class.getSimpleName().toLowerCase();
wrapper.indexName(indexName);
wrapper.mapping(Document::getTitle, FieldType.KEYWORD);
wrapper.mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);
wrapper.mapping(Document::getInfo, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);
boolean isOk = documentMapper.updateIndex(wrapper);
Assertions.assertTrue(isOk);
}
删除索引
@Test
public void testDeleteIndex() {
String indexName = Document.class.getSimpleName().toLowerCase();
boolean isOk = documentMapper.deleteIndex(indexName);
Assertions.assertTrue(isOk);
}
三、数据CURD
Integer insert(T entity);
Integer insert(T entity, String... indexNames);
Integer insertBatch(Collection<T> entityList)
Integer insertBatch(Collection<T> entityList, String... indexNames);
Integer deleteById(Serializable id);
Integer deleteById(Serializable id, String... indexNames);
Integer delete(LambdaEsQueryWrapper<T> wrapper);
Integer deleteBatchIds(Collection<? extends Serializable> idList);
Integer deleteBatchIds(Collection<? extends Serializable> idList, String... indexNames);
Integer updateById(T entity);
Integer updateById(T entity, String... indexNames);
Integer updateBatchByIds(Collection<T> entityList);
Integer updateBatchByIds(Collection<T> entityList, String... indexNames);
Integer update(T entity, LambdaEsUpdateWrapper<T> updateWrapper);
Long selectCount(LambdaEsQueryWrapper<T> wrapper);
Long selectCount(Wrapper<T> wrapper, boolean distinct);
T selectById(Serializable id);
T selectById(Serializable id, String... indexNames);
List<T> selectBatchIds(Collection<? extends Serializable> idList);
List<T> selectBatchIds(Collection<? extends Serializable> idList, String... indexNames);
T selectOne(LambdaEsQueryWrapper<T> wrapper);
List<T> selectList(LambdaEsQueryWrapper<T> wrapper);
四、参数文档
Easy-Es文档
springboot操作ES之ElasticSearch_EasyEs
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)