Elasticsearch Max_Num_Segments in Elasticsearch

By Opster Team

Updated: Nov 2, 2023

| 2 min read


In Elasticsearch, managing the number of segments in an index is crucial for optimizing search and indexing performance. One of the key parameters that can be used to control the number of segments is max_num_segments. In this article, we will discuss the importance of max_num_segments, how to configure it, and best practices for using it to improve Elasticsearch performance. If you want to learn about Elasticsearch index lifecycle Management & policy, check out this guide.

Understanding Segments in Elasticsearch

Segments are the basic building blocks of an Elasticsearch index. When documents are indexed, they are stored in segments, which are immutable data structures. As more documents are indexed, new segments are created. Over time, segments can become fragmented, leading to suboptimal search performance.

To maintain optimal performance, Elasticsearch periodically merges smaller segments into larger ones through a process called segment merging. This process reduces the number of segments and helps maintain a healthy index structure. The max_num_segments parameter plays a crucial role in controlling this merging process.

Role of max_num_segments in Elasticsearch

The max_num_segments parameter is used during the force merge operation, which is an API call that triggers the merging of segments in an index. The force merge operation can be used to reduce the number of segments to a specified target, which is defined by the max_num_segments parameter. By reducing the number of segments, you can improve search performance and reduce the overhead associated with managing multiple segments.

Configuring max_num_segments

To configure max_num_segments, you can use the force merge API. The following example demonstrates how to use the force merge API to reduce the number of segments in an index called “my_index” to a target of 1:

POST /my_index/_forcemerge?max_num_segments=1

In this example, the force merge operation will attempt to merge the segments in “my_index” until there is only one segment left per shard. Note that the force merge operation can be resource-intensive and may take some time to complete, depending on the size of your index and the number of segments.

Best Practices for Using max_num_segments

1. Choose an appropriate value for max_num_segments: The optimal value for max_num_segments depends on your use case and the resources available in your Elasticsearch cluster. In general, a lower value will result in better search performance, but may increase indexing latency. A higher value will reduce indexing latency but may lead to suboptimal search performance. It is essential to strike a balance between search and indexing performance based on your specific requirements.

2. Monitor segment count and size: Regularly monitoring the number of segments and their sizes in your indices can help you identify when a force merge operation may be necessary. You can use the _cat/segments API to obtain information about the segments in your indices:

GET /_cat/segments?v

3. Schedule force merge operations during periods of low cluster activity: Since force merge operations can be resource-intensive, it is recommended to schedule them during periods of low cluster activity, such as during off-peak hours or maintenance windows.

4. Avoid force merging too frequently: Frequent force merge operations can lead to increased resource usage and may negatively impact cluster performance. It is essential to find a balance between maintaining a healthy index structure and minimizing the impact of force merge operations on cluster performance.

5. Use index lifecycle management (ILM) policies: Elasticsearch provides index lifecycle management (ILM) policies that can automate the process of managing segments and force merging. By configuring ILM policies, you can ensure that your indices are automatically optimized based on predefined criteria, such as segment count or index age.


In summary, the max_num_segments parameter is an essential tool for optimizing Elasticsearch performance by controlling the number of segments in an index. By understanding the role of max_num_segments, configuring it appropriately, and following best practices, you can improve search performance and maintain a healthy index structure in your Elasticsearch cluster.