程序小牛

来了就随便看看,虽然没有什么好看的~~

elasticsearch学习与实用

学习springboot整合ElasticSearch 7.X版本并通过小demo实现基本的增删改查

  1. 引入依赖
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
  1. 修改配置文件
1
2
3
4
spring:
application:
rest:
uris: http://xx.XX.XX.XX:9200/
  1. 新增一些实体类、to类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//实体类
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_user")
public class TUser implements Serializable {

private static final long serialVersionUID = 1L;

@TableId(value = "id", type = IdType.AUTO)
private Integer id;

private LocalDate birthday;

private String gender;

private String username;

private String password;

private String remark;

private String station;

private String telephone;

}

//es实体类
@Data
@Document(indexName = "user_info")
public class UserInfoTO {
@Id
private Integer id;

private LocalDate birthday;

private String gender;

private String username;

private String password;

private String remark;

private String station;

private String telephone;
}
  1. 实用es新增用户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 1.创建一个 elasticsearch 持久层接口,类似MP,内置了 es 增删改查方法
@Repository
public interface ITUserEsSevice extends ElasticsearchRepository<UserInfoTO,Integer> {
}

// 2.定义swagger接口
@ApiOperation(value = "新增用户")
@PostMapping("/saveUser")
public void saveUser(@RequestBody TUser tUser){
itUserService.saveUser(tUser);
}

// 3.实现新增接口
@Autowired
ITUserEsSevice itUserEsSevice;

@Override
public void saveUser(TUser tUser) {
UserInfoTO userInfoTO = new UserInfoTO();
BeanUtils.copyProperties(tUser,userInfoTO);
//保存到Es上
// itUserEsSevice.save(userInfoTO);
itUserEsSevice.delete(userInfoTO);
}

参考一:Linux环境下安装Elasticsearch

参考二:Linux安装Kibana详细教程

参考三:springboot整合ElasticSearch实战

参考四:Spring Data Elasticsearch 实体类注解说明 此博主包含 Elasticsearch 其它系列博文