ElasticSearch教程FG007-ElasticSearch索引创建与Mapping设计
Part01-基础概念与理论知识
1.1 ElasticSearch索引概念
ElasticSearch索引是文档的集合,类似于关系型数据库中的数据库。每个索引都有自己的映射(Mapping)和设置(Settings),定义了文档的结构和索引的行为。更多视频教程www.fgedu.net.cn
索引具有以下特点:
- 每个索引都有唯一的名称
- 索引可以包含多个文档
- 索引支持分片和副本,提高性能和可用性
- 索引可以通过API进行创建、修改和删除
1.2 ElasticSearch Mapping概念
Mapping是定义文档结构的过程,包括字段名、数据类型、分词器等设置。Mapping类似于关系型数据库中的表结构定义。学习交流加群风哥微信: itpux-com
Mapping的主要作用:
- 定义字段的数据类型
- 指定字段的索引方式
- 配置字段的分词器
- 控制字段的存储行为
1.3 字段数据类型
ElasticSearch支持多种数据类型,包括:
- 核心数据类型:text, keyword, date, long, integer, double, boolean, ip
- 复合数据类型:object, nested
- 特殊数据类型:geo_point, geo_shape, completion
风哥提示:选择合适的数据类型对索引性能和查询效率至关重要
Part02-生产环境规划与建议
2.1 索引命名规范
生产环境中,索引命名应遵循以下规范:
- 使用小写字母
- 使用连字符(-)分隔单词
- 包含业务标识和时间信息(如:fgedu-logs-2024.01)
- 避免使用特殊字符
2.2 Mapping设计原则
Mapping设计应遵循以下原则:
- 按需定义字段,避免冗余
- 选择合适的数据类型
- 合理设置字段的索引属性
- 为text字段选择合适的分词器
- 考虑字段的聚合和排序需求
2.3 生产环境索引规划
生产环境索引规划应考虑以下因素:
- 数据量大小
- 查询模式
- 更新频率
- 存储需求
- 性能要求
学习交流加群风哥QQ113257174
Part03-生产环境项目实施方案
3.1 索引创建实战
创建索引的基本命令:
curl -X PUT “http://192.168.1.10:9200/fgedu-products” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products”
}
3.2 Mapping配置实战
创建索引时指定Mapping:
curl -X PUT “http://192.168.1.10:9200/fgedu-products” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2
},
“mappings”: {
“properties”: {
“id”: {
“type”: “keyword”
},
“name”: {
“type”: “text”,
“analyzer”: “ik_max_word”
},
“price”: {
“type”: “double”
},
“category”: {
“type”: “keyword”
},
“created_at”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products”
}
3.3 索引模板应用
创建索引模板:
curl -X PUT “http://192.168.1.10:9200/_index_template/fgedu-logs-template” -H “Content-Type: application/json” -d ‘{
“index_patterns”: [“fgedu-logs-*”],
“template”: {
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 1
},
“mappings”: {
“properties”: {
“timestamp”: {
“type”: “date”
},
“level”: {
“type”: “keyword”
},
“message”: {
“type”: “text”
},
“service”: {
“type”: “keyword”
}
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
更多学习教程公众号风哥教程itpux_com
Part04-生产案例与实战讲解
4.1 电商商品索引设计
电商商品索引的Mapping设计:
curl -X PUT “http://192.168.1.10:9200/fgedu-products” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2,
“analysis”: {
“analyzer”: {
“ik_smart”: {
“type”: “custom”,
“tokenizer”: “ik_smart”
}
}
}
},
“mappings”: {
“properties”: {
“product_id”: {
“type”: “keyword”
},
“product_name”: {
“type”: “text”,
“analyzer”: “ik_smart”,
“fields”: {
“keyword”: {
“type”: “keyword”
}
}
},
“description”: {
“type”: “text”,
“analyzer”: “ik_smart”
},
“price”: {
“type”: “double”
},
“stock”: {
“type”: “integer”
},
“category”: {
“type”: “keyword”
},
“brand”: {
“type”: “keyword”
},
“sales”: {
“type”: “integer”
},
“created_at”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products”
}
4.2 日志索引设计
日志索引的Mapping设计:
curl -X PUT “http://192.168.1.10:9200/fgedu-logs-2024.01” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 1
},
“mappings”: {
“properties”: {
“timestamp”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss.SSS”
},
“level”: {
“type”: “keyword”
},
“logger”: {
“type”: “keyword”
},
“message”: {
“type”: “text”
},
“thread”: {
“type”: “keyword”
},
“stack_trace”: {
“type”: “text”
},
“service_name”: {
“type”: “keyword”
},
“host”: {
“type”: “keyword”
},
“environment”: {
“type”: “keyword”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-logs-2024.01”
}
4.3 Mapping优化实战
优化Mapping以提高性能:
curl -X GET “http://192.168.1.10:9200/fgedu-products/_mapping”
# 执行
# 输出日志
{
“fgedu-products”: {
“mappings”: {
“properties”: {
“product_id”: {
“type”: “keyword”
},
“product_name”: {
“type”: “text”,
“analyzer”: “ik_smart”,
“fields”: {
“keyword”: {
“type”: “keyword”
}
}
},
“description”: {
“type”: “text”,
“analyzer”: “ik_smart”
},
“price”: {
“type”: “double”
},
“stock”: {
“type”: “integer”
},
“category”: {
“type”: “keyword”
},
“brand”: {
“type”: “keyword”
},
“sales”: {
“type”: “integer”
},
“created_at”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss”
}
}
}
}
}
风哥提示:对于不需要搜索的字段,可以设置index: false以节省存储空间和提高性能
Part05-风哥经验总结与分享
5.1 索引设计最佳实践
- 根据数据量和查询模式选择合适的分片数
- 合理设置副本数以提高可用性
- 使用索引模板统一管理相似索引的配置
- 定期清理过期索引以节省存储空间
- 使用别名管理索引,实现无缝切换
5.2 Mapping常见问题
- 数据类型选择不当导致查询性能下降
- 过多的字段导致Mapping膨胀
- text字段未指定合适的分词器
- 动态Mapping导致字段类型不一致
- 嵌套字段过深影响查询性能
5.3 生产环境调优建议
- 使用静态Mapping而非动态Mapping
- 为频繁查询的字段创建合适的索引
- 合理使用字段别名简化查询
- 定期检查和优化Mapping
- 使用索引生命周期管理自动处理索引
from ElasticSearch视频:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
