说到 elasticsearch 优化,在官方文档上有专门的一个章节介绍: How to
- General recommendations
- Recipes
- Tune for indexing speed
- Tune for search speed
- Tune for disk usage
- Fix common cluster issues
- Size your shards
- Use Elasticsearch for time series data
这其中的大纲有很好的指导意义,按图索骥即可。此外,还有大大小小的各类优化散落文档各章节,例如常见的缓存:Shard Request Cache,又如隐晦的类型:Numeric field types!
ES 的官方文档写得不错,特别是 tips!知其然,知其所以然,才能有的放矢!
在这儿罗列三个性价比很高且操作简单的优化技巧 ( 读优化,排名不分先后 ):
- 类型转换:numeric -> keyword,其中的原理参看:压榨 ES,从 numeric 到 keyword,秒变闪电侠,优化效果如下:
- 合并分片:8 shard -> 1 shard,这乍一看貌似有悖直觉,但事实上,在数据量较少的情况下,分片结果的聚合反倒是瓶颈,优化效果如下:
- 水平扩容:9 replica -> 12 replica,no need to say any more,优化效果如下:
此外,优化技巧还有周期性 merge,这个适合在集中频繁变更的场景 ( 低峰期 merge ),而其中的优化原理是 LSM Tree 中 update = delete + insert,而其中的 delete 也仅仅是打个 tag ( LSM Tree 非常适合 Write Optimization )!
alias 是一个最佳实践,特别适合 reindex 场景,同时特别地,建议不要关闭 source,否则无法 reindex!
附录 -- Reindex 步骤
-
停写 ( 非必要,但注意 reindex 过程中数据不会同步到新索引 )
-
设置 mapping
PUT /goods-deno-v1
{
"settings": {
"number_of_shards": 12
},
"mappings": {
"_routing": {
"required": true
},
"properties": {
"brandId": {
"type": "keyword", "index_options": "docs"
}
// ······
}
}
}
3. Reindex
POST _reindex
{
"source": {
"index": "goods-deno-pattern"
},
"dest": {
"index": "goods-deno-v1"
}
}
4. 待同步完成后切换 alias
POST _aliases
{
"actions": [
{
"remove": {
"index": "goods-deno",
"alias": "goods-deno-pattern"
}
},
{
"add": {
"index": "goods-deno-v1",
"alias": "goods-deno-pattern"
}
}
]
}
5. 处理增量数据 ( 例如倘若在 binlog 链路上的画简单回拨 timestamp 即可,一个更优雅的则是双写 )