在 Elasticsearch 中,“优化索引结构”通常可以从映射(Mapping)设计、分片与副本策略、索引生命周期管理、查询与写入优化等多个层面来进行。下面按实战角度系统说明。
dynamic"dynamic": "strict" // 或 "false"
text + keyword 双字段浪费存储text 与 keywordtextkeywordkeyword"username": { "type": "keyword" }
"index": false"doc_values": false"store": false"big_field": {
"type": "text",
"index": false
}
copy_to 减少字段数"first_name": { "type": "text", "copy_to": "full_name" },
"last_name": { "type": "text", "copy_to": "full_name" }
date,不要存字符串"created_at": { "type": "date" }
_split"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
适合日志、监控、事件类数据
PUT _ilm/policy/logs-policy
{
"policy": {
"phases": {
"hot": { "actions": { "rollover": { "max_size": "30gb" } } },
"delete": { "min_age": "30d", "actions": { "delete": {} } }
}
}
}
好处:
"refresh_interval": "30s"
"number_of_replicas": 0
写入完成后再恢复
wildcard / 前导通配符"name": "*abc" // ❌
filter 代替 query"filter": [
{ "term": { "status": "active" } }
]
"_source": ["id", "name"]
keywordcomposite 聚合分页PUT orders
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "30s"
},
"mappings": {
"properties": {
"order_id": { "type": "keyword" },
"user_id": { "type": "keyword" },
"amount": { "type": "scaled_float", "scaling_factor": 100 },
"status": { "type": "keyword" },
"created_at": { "type": "date" },
"remark": { "type": "text", "index": false }
}
}
}
❌ 一个索引存所有数据
❌ 分片越多越快
❌ 所有字段都 text
❌ 不做 ILM
❌ 实时 refresh
如果你有具体业务场景(如日志、电商、搜索、时序数据),我可以给你一套针对性的索引结构设计方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。