1、环境准备
我本地使用的环境为:
虚拟机版本:Centos 7.3 两台 IP 分别为:192.168.56.12, 192.168.56.13
Elasticsearch版本:6.4.0 (已安装IK分词器)
虚拟机中JDK版本:12.0.1
宿主机系统:Windows 10
宿主机JDK版本:1.8
Idea版本: 2019.1.3
2、创建工程
1)、在Idea中创建一个Maven工程,并导入Spring Data Elasticsearch依赖。
Spring Data Elasticsearch依赖(pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.Aiden</groupId>
<artifactId>SpringData-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency> </dependencies> </project>
2)、创建配置文件
在maven工程的resource目录下创建Spring Data Elasticsearch的配置文件 ApplicationContext.xml,并引入Elasticsearch命名空间
ApplicationContext.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
"> </beans>
3)、创建实体Film
在maven工程中创建包com.Aiden.doamin,并在此包下创建实体类Film
package com.Aiden.domain; import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType; public class Film { private Long id;
private String title;
private String content;
private String date;
private String price;
private String director; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
} public String getDirector() {
return director;
} public void setDirector(String director) {
this.director = director;
} @Override
public String toString() {
return "Film{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", date='" + date + '\'' +
", price='" + price + '\'' +
", director='" + director + '\'' +
'}';
}
}
4)、创建Dao
在maven工程中创建com.Aiden.dao包,并在包中创建仓库 FilmRepository
package com.Aiden.dao; import com.Aiden.domain.Film;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository; @Repository
public interface FilmRepository extends ElasticsearchRepository<Film,Long> { }
5)、创建Service
在maven工程中创建com.Aiden.service包,并在包中创建 FilmService 接口,并在其中添加save方法
package com.Aiden.service; import com.Aiden.domain.Film; public interface FilmService { public void save(Film film); }
在刚创建的service包下创建Impl包(即com.Aiden.service.Impl包),并在此包中创建FilmServiceImpl类并实现FilmService接口,实现其方法
package com.Aiden.service.impl; import com.Aiden.dao.FilmRepository;
import com.Aiden.domain.Film;
import com.Aiden.service.FilmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class FilmServiceImpl implements FilmService { @Autowired
private FilmRepository filmRepository; public void save(Film film) {
filmRepository.save(film);
} }
6)、修改Spring Data Elasticsearch配置文件ApplicationContext.xml,完成项目配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
"> <!-- 扫描Dao包,自动创建实例 -->
<elasticsearch:repositories base-package="com.Aiden.dao"/> <!-- 扫描Service包,创建Service的实体 -->
<context:component-scan base-package="com.Aiden.service"/> <!-- 配置elasticSearch的连接 -->
<elasticsearch:transport-client id="client" cluster-nodes="192.168.56.12:9300,192.168.56.13:9300" cluster-name="my-application"/> <!-- ElasticSearch模版对象 -->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean> </beans>
7)、配置实体类
基于spring data elasticsearch注解配置索引、映射和实体的关系
package com.Aiden.domain; import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType; @Document(indexName = "film",type = "action")
public class Film {
@Id
private Long id;
@Field(index = true,analyzer = "ik_max_word",store = true,searchAnalyzer = "ik_max_word",type = FieldType.text)
private String title;
@Field(index = true,analyzer = "ik_max_word",store = true,searchAnalyzer = "ik_max_word",type = FieldType.text)
private String content;
@Field(index = true,analyzer = "ik_max_word",store = true,searchAnalyzer = "ik_max_word",type = FieldType.text)
private String date;
@Field(index = true,analyzer = "ik_max_word",store = true,searchAnalyzer = "ik_max_word",type = FieldType.text)
private String price;
@Field(index = true,analyzer = "ik_max_word",store = true,searchAnalyzer = "ik_max_word",type = FieldType.text)
private String director; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
} public String getDirector() {
return director;
} public void setDirector(String director) {
this.director = director;
} @Override
public String toString() {
return "Film{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", date='" + date + '\'' +
", price='" + price + '\'' +
", director='" + director + '\'' +
'}';
}
}
8)、创建测试类
在maven工程的test文件夹下创建测试类 SpringDataElasticSearchTest,并添加测试方法
package com.Aiden.Test; import com.Aiden.domain.Film;
import com.Aiden.service.FilmService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:ApplicationContext.xml")
public class SpringDataElasticSearchTest { @Autowired
private FilmService filmService; @Autowired
private ElasticsearchTemplate elasticsearchTemplate; @Test
public void createIndex() throws Exception {
elasticsearchTemplate.createIndex(Film.class);
elasticsearchTemplate.putMapping(Film.class);
} @Test
public void saveFilm1() {
Film film = new Film();
film.setTitle("蜘蛛侠:英雄远征 Spider-Man: Far from Home");
film.setContent("在复仇者联盟众英雄的努力下,于灭霸无限手套事件中化作为灰烬的人们,重新回到了人世间,曾经消失的蜘蛛侠 彼得帕克 也回归到了普通的生活之中,数月后,蜘蛛侠彼得帕克所在的学校举行了欧洲旅游,帕克也在其中, 在欧州威尼斯旅行时,一个巨大无比的水怪袭击了威尼斯,不敌敌人的蜘蛛侠幸得一位自称神秘客的男子搭救才击退敌人,之后 神盾局局长找上正在旅游的彼得帕克并要求其加入神盾局,并安排神秘客协助帕克,神秘客自称来自其他宇宙,并告知一群名为元素众的邪恶*已经入侵这个宇宙了,为了守护来之不易的和平,蜘蛛侠决定与神秘客联手,然而在神秘客那头罩之中,似乎隐藏着什么不为人知的真相……");
film.setDate("2019-06-28");
film.setDirector("乔·沃茨");
film.setPrice("78");
film.setId(1L);
filmService.save(film);
} }
9)、运行测试
执行SpringDataElasticSearchTest中的测试方法 createIndex 、saveFilm1,创建索引及加入文档。
测试方法执行完毕,在head插件中查看Elasticsearch集群中索引情况:
索引创建成功:
查看索引信息中的mapping信息,创建正常:
执行saveFilm1加入文档,文档加入成功:
至此Spring Data Elasticsearch工程创建成功!