摘要
本文的主要内容是介绍ElasticSearch
和Kibana
的安装及使用Spring Boot
集成ElasticSearch
。
本文使用的版本如下:
ElasticSearch
7.6.2Kibana
7.6.2Spring Boot
2.2.2.RELEASE
安装ElasticSearch
在官网下载压缩包,下载地址为:https://www.elastic.co/cn/downloads/elasticsearch
,当前的最新版本为7.6.2
。
解压缩后的目录如下:
进入bin
目录执行./elasticsearch -d
即可在后台启动es服务,启动成功访问http://localhost:9200
得到如下结果代表启动成功:
在Linux环境下不能使用Root用户进行启动,否则会报错。
安装ik分词器
ES自带的分词器对中文不太友好,我们需要安装一下ik分词插件。该插件的下载地址为:https://github.com/medcl/elasticsearch-analysis-ik/releases
,下载同自己的ES服务相对应的版本,在plugins
目录下新建ik
目录并将下载的压缩包解压到该目录,之后重启ES服务即可,目录结构如下:
之后访问http://localhost:9200/_cat/plugins
可以查看到ik的信息代表安装成功,返回结果如下:
Spring Boot操作es
在上面的内容中,我们已经安装好了es服务,接下来我们介绍下如何使用spring-boot-starter-data-elasticsearch
操作es,项目github地址为:https://github.com/xiehuaa/spring-boot-es
创建数据库,并导入项目中source/pe_university.sql
文件中的sql数据,将applicatiion.yml
文件中的数据库配置修改为你自己的数据库路径。
详细的代码可以下载上面给出的github地址中进行查看。
创建Spring Boot
项目并引入es的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
在application.yml
文件中加入ES的配置,如下:
spring:
elasticsearch:
rest:
uris: http://localhost:9200
创建文档对象如下:
import lombok.Getter;
import lombok.Setter;
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;
import java.io.Serializable;
import java.util.Date;
@Setter
@Getter
@Document(indexName = "university", type =