ElasticSearch进阶(二)SpringData ElasticSearch整合

前言

     本章讲解Spring+Spring Data ElasticSearch的整合操作

方法

1.概念

我们知道,Spring Data 是持久层通用解决方案,支持关系型数据库 Oracle、MySQL、非关系型数据库NoSQL、Map-Reduce 框架、云基础数据服务 、搜索服务。Spring Data 包含多个子项目,其中就有我们要学习的Spring Data ElasticSearch

2.环境搭建

1)创建相应工程

我们本次创建的是spring的普通工程!

ElasticSearch进阶(二)SpringData ElasticSearch整合_第1张图片

2)修改pom文件


	4.0.0
	cn.edu.ccut
	spring-springdata-jpa
	0.0.1-SNAPSHOT
	war

	
		
		1.8
		
		5.1.9.RELEASE
	

	
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
		
		  org.springframework.data
		  spring-data-elasticsearch
		  3.2.0.RELEASE
		
		
		
			org.springframework
			spring-test
			${spring.version}
			test
		
		
		
			junit
			junit
			4.12
			test
		
		
		
	

3)配置applicationContext.xml




	

	

	
		
	

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.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class PersonTest {

	@Autowired
	private ElasticsearchRestTemplate elasticsearchRestTemplate;

	@Test
	public void testOK(){
		System.out.println("this is good");
	}
	
}

运行后如果没有异常,则证明我们环境配置成功。

注意:本次仅仅列举出下面示例的方法,请大家自行验证,本人已验证成功!

3)添加文档方法

@Test
public void testCreateDoc() throws Exception{
	Person person = new Person("1005","陈立志",23,"[email protected]","跳舞、踢足球、下围棋");
	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 testSearchDoc01() {
	Person person = elasticsearchRestTemplate.queryForObject(GetQuery.getById("1001"), Person.class);
	System.out.println(person);
}

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 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 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 result = elasticsearchRestTemplate.queryForList(searchQuery, Person.class);
	for(Person person : result){
		System.out.println(person);
	}
}

我们可以发现,整合了之后运用ElasticsearchTemplate,操作ES会变的更加方便!

你可能感兴趣的:(ElasticSearch)