Elasticsearch怎么配置?
本文章转自:乐字节
文章主要讲解:Elasticsearch怎么配置
准备工作
环境
- Elasticsearch:7.9.3
- Spring Boot:2.4.3
- JDK:11.0.7
- 前端:仿京东商城模板 + Vue,文中配有详细代码
- IDE:IntelliJ IDEA
Elasticsearch
安装流程这里不过多赘述,本文使用 Elasticsearch 集群环境,已安装好 IK 中文分词器。
下图为 elasticsearch-head 插件(浏览器可以直接安装该插件进行连接)显示的集群信息。
Spring Boot
创建项目
使用 Spring Initializr
初始化 Spring Boot 项目,添加 Spring Web
,Spring Data Elasticsearch
,MySQL
,MyBatis
,Lombok
。
配置文件
application.yml 配置 MySQL、Elasticsearch、MyBatis 相关信息。
spring:
# 数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/example?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
# elasticsearch
elasticsearch:
rest:
uris: 192.168.10.10:9200,192.168.10.11:9200,192.168.10.12:9200
mybatis:
configuration:
map-underscore-to-camel-case: true # 开启驼峰映射
启动类
启动类添加 Mapper 接口扫描。
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.mapper") // Mapper 接口扫描
@SpringBootApplication
public class SearchDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SearchDemoApplication.class, args);
}
}
前端
将我为大家准备好的前端资源文件添加至项目 resources
目录下的 static
目录中。
前端资源文件获取公众号:《乐字节》,发送999
在 list.html 中使用 CDN 添加 Vue 和 Axios 免去下载文件的过程。
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios@0.21.1/dist/axios.min.js"></script>
感谢大家的观看与支持,下期将为大家讲解Elasticsearch项目开发流程
阅读 696
22 声望
1 粉丝
0 条评论
得票最新