一、创建一个 SpringBoot 项目
1. 项目名称 spring-boot-es-example-01
2. 使用 maven
管理依赖包,在项目下创建 pom.xml
文件, 添加Elasticsearch
相关的依赖包和项目启动包。
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.example</groupId>
<name>spring-boot-es-example-01</name>
<artifactId>spring-boot-starter-es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Elasticsearch Project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--Spring Boot 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--Spring Boot Web Resful接口 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Boot Test 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Spring Boot Data Elasticsearch 数据库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--Lombokx -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<mainClass>com.springboot.es.example.SpringBootElasticsearchApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. 在项目下src/main/java
, 创建包目录结构 com.springboot.es.example
4. 在包目录下,创建domain
文件夹,创建实体类 User.java
java
package com.springboot.es.example.domain;
import lombok.Data;
import org.elasticsearch.index.VersionType;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* @author Calvin
* @date 2021/7/5
* @since v1.0.0
*/
@Data
@Document(indexName = "calvin", versionType = VersionType.INTERNAL)
public class User {
/** ID */
@Id
private String id;
/** 姓名 */
private String name;
/** 年龄 */
private Integer age;
/** 性别 */
private Integer sex;
}
5. 在包目录下,创建repository
文件夹,创建接口类 UserRepository.java
用于操作Elasticseearch
数据库。
java
package com.springboot.es.example.repository;
import com.springboot.es.example.domain.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @author Calvin
* @date 2021/7/8
* @since v1.0.0
*/
public interface UserRepository extends ElasticsearchRepository<User, String> {
/**
* 用户-根据名字,获取用户信息
*
* @param name 的名字
* @return {@link User}
*/
User findByName(String name);
}
6. 在包目录下,创建controller
文件夹,实现Restful
风格接口。
java
package com.springboot.es.example.controller;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.springboot.es.example.domain.User;
import com.springboot.es.example.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
* @author Calvin
* @date 2021/7/8
* @since v1.0.0
*/
@RestController
@RequestMapping("api/v1/user")
public class UserController {
@Autowired
private UserRepository userRepository;
@PostMapping(value = "add")
public Object add(@RequestBody @Validated User user) {
User save = userRepository.save(user);
return save;
}
@GetMapping(value = "get")
public Object getByName(@RequestParam(value = "name")String name) {
User user = userRepository.findByName(name);
return user;
}
}
7. 创建项目启动类 SpringBootElasticsearchApplication.java
, 需要配置注解扫包 @EnableElasticsearchRepositories
ES 才生效。
java
package com.springboot.es.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
/**
* @author Calvin
* @date 2021/7/5
* @since v1.0.0
*/
@EnableElasticsearchRepositories(basePackages = "com.springboot.es.example.repository")
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SpringBootElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootElasticsearchApplication.class, args);
}
}
8. 在项目下src/main/resources
, 创建配置文件 application.properties
, 添加ES相应访问配置。
properties
server.port= 8888
logging.level.com.springboot.es.example= debug
spring.data.elasticsearch.repositories.enabled=true
# 连接ES客户端IP地址
spring.data.elasticsearch.client.reactive.endpoints=127.0.0.1:9200
# 连接ES超时时间,毫秒为单位,默认为10秒
spring.data.elasticsearch.client.reactive.connection-timeout= 60
# 读取和写入的超时,毫秒为单位,默认为5秒
spring.data.elasticsearch.client.reactive.socket-timeout=10
# 是否启用ssl
spring.data.elasticsearch.client.reactive.use-ssl=false
# 调用ES的Restful接口IP地址
spring.elasticsearch.rest.uris= 127.0.0.1:9200
# ES 访问用户名
spring.data.elasticsearch.client.reactive.username=
# ES 访问密码
spring.data.elasticsearch.client.reactive.password=
二、演示结果
1. 新增数据到ES
2. 从ES获取数据
3.通过 Kibana
查看数据。
- 在左侧菜单栏中选中
Management
,点击索引模式
- 点击
创建索引模式
- 将列表下没有索引值,输入到
文本框中
,这里输入calvin
索引。