一、安装Elasticsearch
1、Windows安装
Windows安装比较简单,ES官网Download Elasticsearch | Elastic下载压缩包,解压出来, bin 目录下有个elasticsearch.bat,双击,就运行起来了。
然后在浏览器输入localhost:9200验证,成功会返回下面的图片。
二、开始写代码
我的springboot版本是2.7.5,ES是7.17.3
项目结构如下:
1、引入依赖
xml
<!-- spring data es -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api -->
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
2、配置文件
两种方式
(1)yaml配置文件
yaml
server:
port: 8081
spring:
elasticsearch:
# elasticsearch地址
uris: localhost:9200
connection-timeout: 30000
socket-timeout: 50000
socket-keep-alive: false
(2)api 方式
会覆盖掉yml
java
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
/**
* @author
*/
@SpringBootConfiguration
public class ElasticSearchConfig {
@Bean
public ElasticsearchClient elasticsearchClient(){
RestClient client = RestClient.builder(new HttpHost("localhost", 9200))
.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(30000)
.setSocketTimeout(50000))
.build();
ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
return new ElasticsearchClient(transport);
}
}
3、新建 User 实体类
id这个字段一定要有,作为主键索引,这个@Document里面的indexName就相当于mysql里面的表名,在elasticsearch里面叫索引。
java
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "user")
public class User {
@Id
private String id;
private String name;
private String sex;
private Integer age;
}
4、新建 UserRepository
java
import com.example.demo.document.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @author
*/
public interface UserRepository extends ElasticsearchRepository<User, String> {
}
5、新建 Controller
java
import com.example.demo.document.User;
import com.example.demo.repository.UserRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository){
this.userRepository = userRepository;
}
/**
* 添加
*/
@RequestMapping("/insert")
public String insert() {
User user = new User();
user.setId("1");
user.setName("徐一杰");
user.setSex("男");
user.setAge(22);
userRepository.save(user);
return "success";
}
/**
* 删除
*/
@RequestMapping("/delete")
public String delete() {
User user = userRepository.findById("1").get();
userRepository.delete(user);
return "success";
}
/**
* 局部更新
*/
@RequestMapping("/update")
public String update() {
User user = userRepository.findById("1").get();
user.setName("泡泡");
userRepository.save(user);
return "success";
}
/**
* 查询
*/
@RequestMapping("/get")
public User get() {
User user = userRepository.findById("1").get();
System.out.println(user);
return user;
}
@RequestMapping("/getAll")
public Page<User> getAll() {
Pageable pageable = PageRequest.of(1,20);
Page<User> user = userRepository.findAll(pageable);
System.out.println(user);
return user;
}
}
6、测试
(1) 启动项目
可以看到,我们的user索引自动添加到elasticsearch里面了。
(2) 查询索引
我们用postman请求 http://localhost:8081/user/getAll,可以看到,返回了 user 的信息。