Elasticsearch 索引与查询优化实战索引与映射示例PUT /articles
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"title": { "type": "text" },
"tags": { "type": "keyword" },
"created_at": { "type": "date" }
}
}
}
查询与聚合示例POST /articles/_search
{
"query": {
"bool": {
"must": [ { "match": { "title": "性能优化" } } ],
"filter": [ { "terms": { "tags": ["前端","优化"] } } ]
}
},
"aggs": { "by_tag": { "terms": { "field": "tags" } } },
"size": 10
}
优化要点为精确匹配字段使用 `keyword` 类型控制 `size` 与返回字段,避免不必要的负载使用聚合进行统计,避免客户端进行二次汇总维护与监控通过 `GET /_cluster/health` 观察集群状态与分片数量对高频更新索引定期执行 `FORCE MERGE` 与刷新策略评估总结合理的映射与查询结构可显著提升检索性能与可维护性,结合聚合实现实时统计与分析。

发表评论 取消回复