SpringBoot整合Elasticsearch以及基础分词索引查询环境的搭建

236 阅读2分钟

image.png

先选自己需要的es版本,我的2.7.x是

Elasticsearch 7.17.3 | Elastic

 下载上面链接的安装包
 解压到任意目录
 启动es /bin/elasticsearch.bat
 查看安装结果,在网页输入localhost:9200,出现下图即为成功

安装node

安装head插件的前提需要安装node和grunt,所以需要把该两项配置好。

Node.js — Download Node.js®

安装过程结束后,在cmd窗口查看是否安装成功,使用命令:node -v,出现版本,则说明安装成功。

安装grunt

设置为淘宝镜像源 npm config set registry registry.npmmirror.com/

在node安装路径下,使用命令安装:npm install -g grunt-cli 安装grunt。 安装结束后,使用命令grunt
-version查看是否安装成功,出现如下截图,说明安装成功。

安装es-head插件 方便查看ES中的索引及数据

Releases · mobz/elasticsearch-head

解压elasticsearch-head-master
在该目录下进入cmd命令,执行npm install

执行完成后运行启动命令       grunt server

还需要解决跨域问题

由于前后端分离开发,所以会存在跨域问题,需要在服务端做CORS的配置。
修改第一步安装的Elasticsearch目录下的/config/elasticsearch.yml,添加如下设置

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-credentials: true
http.cors.allow-headers: Content-Type,Accept,Authorization,x-requested-with
#http.cors.allow-headers: "*"

然后重启es 浏览器输入127.0.0.1:9100

安装kibana

kibana官方7.12.0下载地址
kibana中文社区下载地址

解压
修改 kibana-7.12.0-windows-x86_64/config/kibana.yml 32行
改为elasticsearch.hosts: [“http://127.0.0.1:9200”]

!!!保存之后,运行bin/kibana.bat

浏览器中访问kibana

如果想使用ip访问kibana,需要修改 kibana-7.12.0-windows-x86_64/config/kibana.yml 7行
改为 server.host: “0.0.0.0”
如果想使用kibana汉化 需要修改 kibana-7.12.0-windows-x86_64/config/kibana.yml 最后一行
i18n.locale: “zh-CN”

首页: http://127.0.0.1:5601/ 开发工具: http://127.0.0.1:5601/app/dev_tools#/console

安装ik分词器

  • 需要对应es版本,我的下载地址 release.infinilabs.com/analysis-ik…
  • 把下载的ik分词器压缩包中的内容全部解压至Elasticsearch的安装目录/plugins/ik内。

重启es和kibana即可

测试 GET _analyze { "analyzer": "ik_max_word", "text": "中华人民共和国国歌" }

至此完成es+ik+kibana+es-head环境搭建配置

下一步:springboot整合es

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.17.26</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

application.yml

spring:
  elasticsearch:
    uris: 127.0.0.1:9200
    connection-timeout: 1s
    socket-timeout: 30s