Elasticsearch 7.17.9的部署与配置

系统环境

  • 操作系统: BCLinux 7.9
  • 内核版本: 5.10.0-200.0.0.161.264.bclinux.21.10U5.x86_64
  • Elasticsearch版本: 7.17.9

一、准备工作

1.1 系统用户与目录配置
1
2
3
4
5
6
7
8
9
10
11
# 创建ES用户组和用户
groupadd es
useradd -g es es

# 创建ES数据目录
mkdir -p /opt/es
mkdir -p /opt/es/data

# 设置目录权限
chown es:es /opt/es -R
chmod 755 /opt/es
1.2 防火墙配置
1
2
3
4
5
6
7
# 临时关闭防火墙(测试环境)
sudo systemctl stop firewalld

# 生产环境建议配置防火墙规则
# firewall-cmd --zone=public --add-port=9200/tcp --permanent
# firewall-cmd --zone=public --add-port=9300/tcp --permanent
# firewall-cmd --reload

二、Elasticsearch配置

2.1 配置文件修改

切换到es用户进行配置:

1
2
su - es
vi /opt/es/elasticsearch-7.17.9/config/elasticsearch.yml

配置文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 集群配置
cluster.name: my-application
node.name: node-1

# 路径配置
path.data: /opt/es/data
path.logs: /opt/es/data

# 内存锁定(生产环境建议开启,测试环境可注释)
# bootstrap.memory_lock: true

# 网络配置
network.host: 0.0.0.0
http.port: 9200
transport.port: 9300

# 单节点模式(测试环境)
discovery.type: single-node

# 安全配置(测试环境可关闭)
xpack.security.http.ssl.enabled: false
xpack.security.enabled: false

# 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"

# JVM堆内存配置(根据实际情况调整)
# -Xms2g -Xmx2g

2.2 启动Elasticsearch

1
2
3
4
5
# 后台启动
/opt/es/elasticsearch-7.17.9/bin/elasticsearch -d

# 验证服务状态
curl http://localhost:9200/

三、索引与映射管理

3.1 端口说明

  • 9200端口: HTTP API通信端口,用于外部调用
  • 9300端口: TCP协议端口,用于集群内部通信

3.2 基础索引操作

创建索引

1
PUT http://192.168.10.36:9200/index1

响应示例:

json

1
2
3
4
5
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "index1"
}

创建映射(字段定义)

1
PUT http://192.168.10.36:9200/index1/_mapping

请求体:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"properties": {
"gender": {
"type": "keyword",
"index": true
},
"phone": {
"type": "keyword",
"index": true,
"null_value": "NULL"
}
}
}

插入文档

1
PUT http://192.168.10.36:9200/index1/_doc/1

请求体:

1
2
3
4
{
"gender": "男性",
"phone": "19487422"
}

3.3 项目索引示例

创建用于存储项目信息的严格模式索引:

1
PUT http://192.168.10.36:9200/project_items

请求体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"mappings": {
"dynamic": "strict",
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"createtime": {
"type": "integer"
},
"updatetime": {
"type": "integer"
}
}
}
}

四、Elasticsearch核心概念对比

Elasticsearch 关系型数据库
Index(索引) Database(数据库)
Document(文档) Row(行)
Field(字段) Column(列)
Mapping(映射) Schema(表结构)
Type(类型) Table(表)

五、API操作参考

5.1 索引操作

1
2
3
4
PUT   /<index>            # 创建索引
GET /<index> # 查看索引信息
DELETE /<index> # 删除索引
GET /_cat/indices?v # 查看所有索引(类似SHOW DATABASES)

5.2 文档操作

1
2
3
4
5
POST    /<index>/_doc           # 插入文档(自动生成ID)
POST /<index>/_doc/<id> # 插入/替换文档(指定ID)
GET /<index>/_doc/<id> # 查看文档
DELETE /<index>/_doc/<id> # 删除文档
POST /<index>/_update/<id> # 更新文档

5.3 条件删除

1
POST /<index>/_delete_by_query

请求体:

1
2
3
4
5
6
7
{
"query": {
"match": {
"columna": 1
}
}
}

5.4 查询操作

1
GET /<index>/_search

查询示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"query": {
"match_all": {}, // 查询所有文档
"match": { // 分词匹配查询
"name": "value1"
},
"multi_match": { // 多字段匹配
"query": "搜索词",
"fields": ["title", "content"]
},
"term": { // 精确匹配(类似LIKE)
"status": "active"
}
}
}

六、后续施工计划

6.1 Python定时同步脚本

开发Python脚本实现MySQL与Elasticsearch的定时数据同步

6.2 中文分词插件部署

安装IK Analyzer插件以支持中文分词查询:

1
2
3
# 下载对应版本的IK分词器
# 将插件放入plugins目录
# 重启Elasticsearch服务

6.3 FRP端口映射

配置FRP实现内网穿透,支持外部访问:

  • 配置frpc.ini客户端文件
  • 设置端口映射规则
  • 启动FRP服务

七、故障排查