压缩索引的本质:在索引只读等三个条件的前提下,减少索引的主分片数。

    1. # 设置待压缩的索引,分片设置为5个。
    2. PUT kibana_sample_data_logs_ext
    3. {
    4. "settings": {
    5. "number_of_shards":5
    6. }
    7. }
    8. # 准备索引数据
    9. POST _reindex
    10. {
    11. "source":{
    12. "index":"kibana_sample_data_logs"
    13. },
    14. "dest":{
    15. "index":"kibana_sample_data_logs_ext"
    16. }
    17. }
    18. # shrink 压缩之前的三个必要条件
    19. PUT kibana_sample_data_logs_ext/_settings
    20. {
    21. "settings": {
    22. "index.number_of_replicas": 0,
    23. "index.routing.allocation.require._name": "node-024",
    24. "index.blocks.write": true
    25. }
    26. }
    27. # 实施压缩
    28. POST kibana_sample_data_logs_ext/_shrink/kibana_sample_data_logs_shrink
    29. {
    30. "settings": {
    31. "index.number_of_replicas": 0,
    32. "index.number_of_shards": 1,
    33. "index.codec": "best_compression"
    34. },
    35. "aliases": {
    36. "kibana_sample_data_logs_alias": {}
    37. }
    38. }

    image.png
    强调一下三个压缩前的条件,缺一不可:

    • “index.number_of_replicas”: 0 副本设置为 0
    • “index.routing.allocation.require._name”: “node-024” 分片数据要求都集中到一个独立的节点
    • “index.blocks.write”: true 索引数据只读