Elasticsearch 查询与索引优化(倒排索引、分片、副本与查询DSL)概述合理的分片、副本与映射设计可显著提升查询与写入性能。结合查询 DSL 的优化与缓存策略,保障稳定性与成本。关键实践与参数分片规划:根据数据量与节点数设置 `number_of_shards`;推荐每分片 20–50GB。副本数:`number_of_replicas`≥1 保证高可用;读多场景提高副本以提升吞吐。映射与分析器:字段类型与分词器(standard/ik);`keyword` 字段用于精确匹配与聚合。刷新与合并:`refresh_interval`(如 5s)平衡实时与写入成本;段合并影响查询性能。配置示例{ "settings": { "number_of_shards": 4, "number_of_replicas": 1, "refresh_interval": "5s" }, "mappings": { "properties": { "title": { "type": "text", "analyzer": "standard" }, "category": { "type": "keyword" }, "created_at": { "type": "date" } } } } 验证方法`cat/shards` 与 `cat/segments` 观察分片与段状态;`_stats` 查看查询与缓存命中。压测查询 DSL(term/match/bool/filter)对比延迟与吞吐;聚合使用 `keyword` 字段。调整 `refresh_interval` 与合并策略后对比写入与查询的权衡。注意事项避免过多小分片与热点分片;使用路由键均衡写入。字段映射固定后谨慎变更;版本演进需重建索引与重放数据。集群与索引生命周期管理(ILM)降低长期维护成本。

发表评论 取消回复