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


猜你喜欢
- 目录一、所使用的环境配置:二、项目简介三、知识点总结(代码和配置)SpringBoot:1.Mybatis-Plus配置文件,实现分页查询:
- 文章描述这个效果可能很多人都在抖音看到过,即把一个短视频,转成数字、字母等乱码组成的形式进行播放。开发环境.NET Framework版本:
- 1.雪崩效应 雪崩效应如上图所示,假设我们有3个微服务A,B,C,A调用B,B调用C,如果C挂掉了,由于B是同步调用,不断等待,导致资源耗尽
- 本文为大家分享了java微信公众号企业付款的开发代码,供大家参考,具体内容如下详情参照微信开发者文档 企业付款文档 java代码
- 本文实例讲述了C#使用WebService结合jQuery实现无刷新翻页的方法。分享给大家供大家参考。具体如下:1. 首先创建数据库、表Ar
- 本文实例讲述了asp.net实现遍历Request的信息操作。分享给大家供大家参考,具体如下:#需求:在服务端获取从客户端发送过来的所有数据
- 目录引言什么是Span关于String的一段性能提升测试代码最终性能对比写在最后引言C# 是一门现代化的编程语言,与Java十分的相似。熟练
- JVM内存组成结构JVM栈由堆、栈、本地方法栈、方法区等部分组成,结构图如下所示:1)堆所有通过new创建的对象的内存都在堆中分配,其大小可
- 1. 使用try-with-resources简化文件读取操作:修改前:FileInputStream fis = null;try { &
- 前言Intellij IDEA 2017.2.2版本针对Springboot设置了一些特性,本篇文章给大家简单介绍一下如何使用这些特性。Ru
- 【引用】迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。 它的主要特点是以起始点为中心
- 作为Android开发者,工作中少不了要反编译别人的apk,当然主要目的还是为了学习到更多,取彼之长,补己之短。今天就来总结一下Androi
- 前言最近想体验下最新版本的SpringBoot,逛了下官网,发现SpringBoot目前最新版本已经是2.6.4了,版本更新确实够快的。之前
- 前言做过java web开发的小伙伴大多数时候都需要链接数据库,这个时候就需要配置数据库引擎DriverClassName参数,这样我们的j
- 本文实例讲述了C#画笔使用复合数组绘制单个矩形的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syst
- 前文传送门:Netty分布式Future与Promise执行回调相关逻辑剖析概述FastThreadLocal我们在剖析堆外内存分配的时候简
- springboot:接收date类型的参数今天有个postmapping方法,地址都正确,就是死活进不去,真是奇怪了。终于从日志中得出些端
- 本文实例为大家分享了SSM实现学生管理系统的具体代码,供大家参考,具体内容如下概述基于Spring + Spring MVC 的学生管理系统
- 在逆向一个Android程序时,如果只是盲目的分析需要阅读N多代码才能找到程序的关键点或Hook点,本文将分享一下如何快速的找到APP程序的
- 很多时候你新建了Maven 或者SpringBoot 工程,激动的点了主启动类,你就发现了下面的错误这里说的是啥意思呢,你没有数据库相关的链