Elasticsearch Mastering Elasticsearch Force Merge Operations

By Opster Team

Updated: Oct 31, 2023

| 3 min read

Quick Links

Overview

Elasticsearch’s force merge operation is a crucial tool for optimizing index performance and managing storage space. It is a process that reduces the number of segments in each shard, thereby improving search speed and freeing up disk space. However, it’s not a one-size-fits-all solution and should be used judiciously to avoid potential pitfalls. 

Understanding Force Merge

In Elasticsearch, an index is composed of one or more shards, each of which is a self-contained Lucene index. These shards are further divided into segments, which are the basic units of indexing and searching. Over time, as documents are added, updated, or deleted, the number of segments increases, leading to slower search performance and increased disk usage. During normal operations, Lucene will regularly merge small segments into larger ones asynchronously in the background. It is also possible to force a merge operation manually.

The force merge operation consolidates smaller segments into fewer, larger segments. This can significantly improve search performance and reduce disk usage. However, it’s a resource-intensive operation and can temporarily degrade cluster performance, so it should be used sparingly and during off-peak hours, if possible.

When to Use Force Merge

Force merge is most beneficial in the following scenarios:

1. Read-Only Indices: Once an index is no longer receiving updates, force merging can improve search performance and reduce disk usage.

2. Time-Based Indices: For indices that are based on a time series (e.g., log or event data), force merging older indices can be beneficial.

3. Disk Space Management: If disk space is a concern, force merging can help by reducing the number of segments and thereby freeing up disk space.

It is worth noting that since Elasticsearch 8.11, the new data streams lifecycle feature is available which takes care of merging the long tail of recently created small segments as soon as the write index gets rolled over. This feature is only available for data streams backing indexes and for any other index types, the regular merging process takes place.

How to Perform a Force Merge

To perform a force merge, you can use the `_forcemerge` API. Here’s a basic example:

POST /my_index/_forcemerge

This will start a force merge operation on the `my_index` index. By default, Elasticsearch will reduce the number of segments to segments of 5GB. You can also specify a target number of segments using the `max_num_segments` parameter:

POST /my_index/_forcemerge?max_num_segments=5

This will reduce the number of segments to five, if your index is smaller than 25GB, otherwise the number of segments might be slightly over 5.

Monitoring Force Merge Operations

You can monitor the progress of a force merge operation using the `_cat/segments` API as well as the `_cat/tasks` API:

GET /_cat/segments/my_index?v

GET _cat/tasks?v&detailed&actions=*merge*

The first command will return a list of segments for the `my_index` index, including their size and the number of documents they contain. The second command will show the force merge tasks that are currently running and it can be helpful when monitoring.

Best Practices for Force Merge Operations

Here are some best practices to follow when using force merge:

1. Avoid Force Merging Active Indices: Force merging an index that is actively receiving updates can lead to a large number of small segments, negating the benefits of the force merge.

2. Limit Concurrent Force Merges: Force merges are resource-intensive operations. Running multiple force merges concurrently can degrade cluster performance.

3. Use the `only_expunge_deletes` Option for Active Indices: If you need to force merge an active index, consider using the `only_expunge_deletes` option. This will only merge segments with a high percentage of deleted documents, reducing the impact on performance. Using this option also helps reduce the disk usage as deleted documents will be removed.

POST /my_index/_forcemerge?only_expunge_deletes=true

Conclusion

In conclusion, Elasticsearch’s force merge operation is a powerful tool for optimizing index performance and managing storage space. However, it should be used judiciously to avoid potential pitfalls. If you want to learn about the Elasticsearch heavy merges issue and how to fix it, check out this guide.

By understanding when and how to use force merge, you can ensure that your Elasticsearch cluster remains performant and efficient. 

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?