前言
本章学习SpringBoot整合SpringData ElasticSearch的相关知识
方法
1.概念
之前我们通过spring+spring-data-elasticsearch基本了解了如何操作es,接下来将进一步的深入!
2.环境搭建
1)创建相应工程
我们本次创建的是springboot工程!版本:2.2.0.RELEASE!!
2)修改pom文件
<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 http://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.2.0.RELEASE</version>
</parent>
<groupId>cn.edu.ccut</groupId>
<artifactId>springboot-elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- 设定Java的版本 -->
<java.version>1.8</java.version>
<!-- 解决pom.xml首行报错 -->
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
</properties>
<dependencies>
<!-- 配置springBoot的web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 配置springBoot的elasticsearch启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- 配置devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 配置springBoot的test启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cn.edu.ccut.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
3)配置application.properties
#ElasticSearch
spring.elasticsearch.rest.uris=http://localhost:9200
spring.data.elasticsearch.repositories.enabled=true
3.整合演示
1)编写实体类Person
package cn.edu.ccut.bo;
import java.io.Serializable;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
@Document(indexName="jwang01",type="_doc")
public class Person implements Serializable{
@Field(name="id")
private String id;
@Field(name="name")
private String name;
@Field(name="age")
private Integer age;
@Field(name="email")
private String email;
@Field(name="hobby")
private String hobby;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String id, String name, Integer age, String email, String hobby) {
super();
this.id = id;
this.name = name;
this.age = age;
this.email = email;
this.hobby = hobby;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", email=" + email + ", hobby=" + hobby + "]";
}
}
2)编写整体测试代码
package cn.edu.ccut;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=App.class)
public class ElasticSearchTest {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void testOK(){
System.out.println("this is OK!");
}
}
运行后如果没有异常,则证明我们环境配置成功。
注意:本次仅仅列举出下面示例的方法,请大家自行验证,本人已验证成功!
3)添加文档方法
@Test
public void testCreateDoc() throws Exception{
Person person = new Person("1005","陈立志",23,"2345631234@qq.com","跳舞、踢足球、下围棋");
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(person.getId().toString())
.withObject(person)
.build();
String documentId = elasticsearchRestTemplate.index(indexQuery);
System.out.println(documentId);
}
4)根据id查询文档
@Test
public void testDeleteDoc() throws Exception{
String documentId = elasticsearchRestTemplate.delete(Person.class, "1005");
System.out.println(documentId);
}
5)根据id删除文档
@Test
public void testDeleteDoc() throws Exception{
String documentId = elasticsearchRestTemplate.delete(Person.class, "1005");
System.out.println(documentId);
}
6)根据id更新文档
@Test
public void testUpdateDoc() throws Exception{
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("name", "张三");
UpdateRequest updateRequest = new UpdateRequest("jwang01", "_doc", "1001").doc(jsonMap);
UpdateQuery query = new UpdateQueryBuilder()
.withClass(Person.class)
.withId("1001")
.withUpdateRequest(updateRequest)
.build();
elasticsearchRestTemplate.update(query);
}
7)查询指定index的所有数据
@Test
public void testSearchDoc02() throws Exception{
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAllQuery())
.build();
List<Person> result = elasticsearchRestTemplate.queryForList(searchQuery, Person.class);
for(Person person : result){
System.out.println(person);
}
}
8)查询爱好中包含篮球的数据
@Test
public void testSearchDoc03() throws Exception{
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("hobby", "篮球"))
.build();
List<Person> result = elasticsearchRestTemplate.queryForList(searchQuery, Person.class);
for(Person person : result){
System.out.println(person);
}
}
我们可以发现,整合了springboot之后,ES的配置会更加简洁,仅仅需要两行!