ElasticSearch进阶(三)SpringBoot整合SpringData ElasticSearch

前言

      本章学习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的配置会更加简洁,仅仅需要两行!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值