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工程创建成功!

ElasticSearch(十一):Spring Data ElasticSearch 的使用(一)的更多相关文章

  1. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  2. Elasticsearch基本用法(2)--Spring Data Elasticsearch

    Spring Data Elasticsearch是Spring Data项目下的一个子模块. 查看 Spring Data的官网:http://projects.spring.io/spring-d ...

  3. Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...

  4. spring data elasticsearch 使用

    很久之前就安装了elasticsearch,一直没用java用过,最近看了一下spring data系列的elasticsearch,这里写一篇心得. 如果尚未安装elasticsearch,可以 参 ...

  5. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  6. elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词

    elasticsearch 自定义分词器 安装拼音分词器.ik分词器 拼音分词器: https://github.com/medcl/elasticsearch-analysis-pinyin/rel ...

  7. How to provide highlighting with Spring data elasticsearch

    How to provide highlighting with Spring data elasticsearch @Test public void shouldReturnHighlighted ...

  8. springboot集成spring data ElasticSearch

    ES支持SpringBoot使用类似于Spring Data Jpa的方式查询,使得查询更加方便. 1.依赖引入 compile “org.springframework.boot:spring-bo ...

  9. 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分

    Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...

随机推荐

  1. 【Git】PHP项目自动化部署的注意事项

    直接开始 1 服务器克隆项目 git clone git@*****.com/project.git 2 更改所有者 chown www:www /www/wwwroot/project/* -R 3 ...

  2. c# Mono.Cecil IL方式 读MethodBody

    using Kufen.Common.Definitions; using Mono.Cecil; using System; using System.Collections.Generic; us ...

  3. but only one is allowed(重复处理跨域请求)

    情景:vue的项目中在本地调试项目,在前端的跨域配置没有问题的情况下,出现这样的报错. 解决方案,参考: https://www.cnblogs.com/zsg88/articles/11576324 ...

  4. 做JAVA的需要了解的框架

    spring netty Elasticsearch Eureka Hystrix 接口的依赖性管理 Zuul Config Bus ActiveMQ redis zookper quartz had ...

  5. IDEA中类文件显示J,IDEA Unable to import maven project: See logs for details

    今天用了下lemon清理了下垃圾后,IDEA打开项目类文件图标由C变为J,在IDEA右侧的Maven Project中点击刷新提示IDEA Unable to import maven project ...

  6. ifcopenshell在VS2015下的编译

    源起 今天使用 IfcOpenShell的IfcConvert ,因为是开源的所以就想自己编译下,编译过程中遇到不少问题,因此记录下来 什么是IfcOpenShell? IfcOpenShell是一个 ...

  7. 在使用Hanlp配置自定义词典时遇到的问题

    要使用hanlp加载自定义词典可以通过修改配置文件hanlp.properties来实现.要注意的点是: 1. root根路径的配置: hanlp.properties中配置如下: #本配置文件中的路 ...

  8. php 函数篇

    1.array_values($data); 注:将关联数组转化为索引数组 <?php $a=array("Name"=>"Bill"," ...

  9. SQL Server日志处理及安全访问

    1.点SQL SERVER错误日志,右键,配置,限定错误日志的数目,比如6个 限制日志增长数量 2.然后运行命令: EXEC sp_cycle_errorlog ; 这个命令的作用是将当前日志归档,然 ...

  10. linux下启动tomcat时卡在Deploying web application directory

    找到jdk1.x.x_xx/jre/lib/security/Java.security文件,在文件中找到securerandom.source这个设置项,将其改为: securerandom.sou ...