Elasticsearch索引管理与查询优化
目录
索引基础概念
索引结构
核心概念
索引 (Index):
- 类似于关系数据库中的数据库
- 包含具有相似特征的文档集合
- 由一个或多个分片组成
分片 (Shard):
- 索引的水平分割单位
- 每个分片是一个独立的Lucene索引
- 支持主分片和副本分片
映射 (Mapping):
- 定义文档字段的数据类型和索引方式
- 类似于关系数据库中的表结构
- 支持动态和静态映射
索引命名规范
# 推荐的索引命名模式
<数据类型>-<环境>-<日期>
# 示例
logs-production-2024.01.15
metrics-staging-2024.01.15
events-development-2024.01.15
# 时间序列数据
apache-logs-2024.01.15
nginx-access-2024.01.15
app-errors-2024.01.15
索引模板管理
1. 索引模板基础
创建索引模板:
PUT _index_template/logs-template
{
"index_patterns": ["logs-*"],
"priority": 100,
"template": {
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1,
"index.refresh_interval": "30s",
"index.codec": "best_compression",
"index.mapping.total_fields.limit": 2000,
"index.max_result_window": 50000
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"level": {
"type": "keyword",
"ignore_above": 256
},
"message": {
"type": "text",
"analyzer": "standard",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 512
}
}
},
"host": {
"properties": {
"name": {
"type": "keyword"
},
"ip": {
"type": "ip"
}
}
},
"tags": {
"type": "keyword"
},
"fields": {
"type": "object",
"dynamic": true
}
}
},
"aliases": {
"logs-current": {
},
"logs-all": {
}
}
},
"composed_of": ["logs-mappings", "logs-settings"],
"version": 1,
"_meta": {
"description": "Template for application logs",
"created_by": "elk-admin",
"created_at": "2024-01-15"
}
}
2. 组件模板
映射组件模板:
PUT _component_template/logs-mappings
{
"template"