SpringBoot整合ElasticSearch实践
作者:田守枝 发布时间:2023-02-05 06:02:08
本节我们基于一个发表文章的案例来说明SpringBoot如何elasticsearch集成。elasticsearch本身可以是一个独立的服务,也可以嵌入我们的web应用中,在本案例中,我们讲解如何将elasticsearch嵌入我们的应用中。
案例背景:每个文章(Article)都要属于一个教程(Tutorial),而且每个文章都要有一个作者(Author)。
一、实体设计:
Tutorial.java
public class Tutorial implements Serializable{
private Long id;
private String name;//教程名称
//setters and getters
//toString
}
Author.java
public class Author implements Serializable{
/**
* 作者id
*/
private Long id;
/**
* 作者姓名
*/
private String name;
/**
* 作者简介
*/
private String remark;
//setters and getters
//toString
}
Article.java
public class Article implements Serializable{
private Long id;
/**标题*/
private String title;
/**摘要*/
private String abstracts;
/**内容*/
private String content;
/**发表时间*/
private Date postTime;
/**点击率*/
private Long clickCount;
/**作者*/
private Author author;
/**所属教程*/
private Tutorial tutorial;
//setters and getters
//toString
}
二、整合SpringBoot与ElasticSearch
1、引入相应的依赖
pom.xml
<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.3.0.RELEASE </version>
</parent>
<dependencies>
<!-- 添加 web 应用的依赖 -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<!-- 添加 spring-data-elasticsearch的依赖 -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-data-elasticsearch </artifactId>
</dependency>
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-test </artifactId>
</dependency>
</dependencies>
2、修改配置文件
application.yml
spring:
data:
elasticsearch:
cluster-name: #默认为elasticsearch
cluster-nodes: #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
properties:
path:
logs: ./elasticsearch/log #elasticsearch日志存储目录
data: ./elasticsearch/data #elasticsearch数据存储目录
这些配置的属性,最终会设置到ElasticsearchProperties这个实体中。
3、为实体添加ElascticSearch的注解
Spring-data-elasticSearch提供了一些注解来帮助我们快速针对实体建立索引。
因为我们希望Article作为我们文章的搜索入口,所以我们在Article类上添加@Document注解。
@Document(indexName="projectname",type="article",indexStoreType="fs",shards=5,replicas=1,refreshInterval="-1")
public class Article implements Serializable{
....
}
默认情况下,添加@Document注解会对实体中的所有属性建立索引,由于本教程是讲解如何整合,并不是专门讲解ElasticSearch,故对于其他注解不再讲解。
4、建立搜索类
我们只要编写一个接口ArticleSearchRepository,来继承Spring-data-elasticSearch提供的ElasticsearchRepository即可。
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import com.tianshouzhi.springbootstudy.domain.Article;
//泛型的参数分别是实体类型和主键类型
public interface ArticleSearchRepository extends ElasticsearchRepository<Article, Long>{
}
5、单元测试
5.1、创建索引
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ElasticSearchTest {
@Autowired
private ArticleSearchRepository articleSearchRepository;
@Test
public void testSaveArticleIndex(){
Author author=new Author();
author.setId(1L);
author.setName("tianshouzhi");
author.setRemark("java developer");
Tutorial tutorial=new Tutorial();
tutorial.setId(1L);
tutorial.setName("elastic search");
Article article =new Article();
article.setId(1L);
article.setTitle("springboot integreate elasticsearch");
article.setAbstracts("springboot integreate elasticsearch is very easy");
article.setTutorial(tutorial);
article.setAuthor(author);
article.setContent("elasticsearch based on lucene,"
+ "spring-data-elastichsearch based on elaticsearch"
+ ",this tutorial tell you how to integrete springboot with spring-data-elasticsearch");
article.setPostTime(new Date());
article.setClickCount(1L);
articleSearchRepository.save(article);
}
}
运行单元测试,项目根目录下出现:
说明我们索引已经创建成功。
5.2测试搜索:
@Test
public void testSearch(){
String queryString="springboot";//搜索关键字
QueryStringQueryBuilder builder=new QueryStringQueryBuilder(queryString);
Iterable<Article> searchResult = articleSearchRepository.search(builder);
Iterator<Article> iterator = searchResult.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行单元测试,控制台输出
Article [id=1, title=springboot integreate elasticsearch, abstracts=springboot integreate elasticsearch is very easy, content=elasticsearch based on lucene,spring-data-elastichsearch based on elaticsearch,this tutorial tell you how to integrete springboot with spring-data-elasticsearch, postTime=Sun Feb 21 16:01:37 CST 2016, clickCount=1, author=Author [id=1, name=tianshouzhi, remark=java developer], tutorial=Tutorial [id=1, name=elastic search]]
说明搜索成功。读者可以尝试其他的搜索关键字进行搜索。
说明:以上方式是SpringBoot与ElasticSearch进行本地整合,即将ElasticSearch内嵌在应用,如果我们搭建了ElasticSearch集群,只需要将配置改为如下配置即可:
spring:
data:
elasticsearch:
cluster-nodes:115.28.65.149:9300 #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
来源:http://www.tianshouzhi.com/api/tutorials/springboot/101
猜你喜欢
- 一、添加数据1、在主表中添加从表数据在景点的住宿集合(Lodgings)中增加一个度假区(Resort)var dest = (from d
- 在实际的项目开发中会有很多的对象,如何高效、方便地管理对象,成为影响程序性能与可维护性的重要环节。Java 提供了集合框架来解决此类问题,线
- package com.yao;import java.util.concurrent.ExecutorService;import jav
- 策略模式的应用场景策略模式是否要使用,取决于业务场景是否符合,有没有必要。是否符合如果业务是处于不同的场景时,采取不同的处理方式的话,就满足
- 合并有序数组的实现java版本:实例代码public class Merge {//合并有序数组 public static v
- 环境:Spring5.3.12.RELEASE。Spring 3引入了一个core.onvert包,提供一个通用类型转换系统。系统定义了一个
- 使用SwingWorker线程模式谨慎地使用并发机制对Swing开发人员来说非常重要。一个好的Swing程序使用并发机制来创建不会失去响应的
- Java把内存分成两种,一种叫做栈内存,一种叫做堆内存在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配。
- 1、实现原理不同过滤器和 * 底层实现方式大不相同,过滤器 是基于函数回调的, * 则是基于Java的反射机制( * )实现的。1、拦
- 前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图
- 前言为什么用动静态库我们在实际开发中,经常要使用别人已经实现好的功能,这是为了开发效率和鲁棒性(健壮性);因为那些功能都是顶尖的工程师已经写
- 二进制数据一般输入的格式是0x45, 0x3a, 0xc3, 这种数据格式看起来是16进制的字符串,但是实际上在存储的时候每个都对应一个字节
- 1、Spring的事务管理主要包括3个接口TransactionDefinition:封装事务的隔离级别,超时时间,是否为只读事务和事务的传
- 最近在配置OpenCV的时候,由于使用的是VS2019,结果找不到Microsoft.Cpp.X64.user这个文件。导致每次新建项目都得
- 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便、快捷。但是当时并没有认真的去学习下,毕竟感觉
- HashMap的keySet()方法比较简单,作用是获取HashMap中的key的集合。虽然这个方法十分简单,似乎没有什么可供分析的,但真正
- 之前文章中我们讲到,java中实现同步的方式是使用synchronized block。在java 5中,Locks被引入了,来提供更加灵活
- 引言:在使用循环的时候,循环里面带有break、continue、return的时候经常弄混,今天特意整理了下,以待后用... f
- 一、项目背景1、介绍:最近在springboot项目中需要做一个阿里云OSS图片上传功能点,将OSS图片上传代码提取到公共工具类中,为了方便
- 使用代码特别注意 :拼接条件时,所使用到的条件strID,strBir必须是独立的var predicate = PredicateBuil