Elasticsearch (more exactly, Lucene) has something called a merge policy, which takes several factors into account when merging, like the number of segments, their size, the percentage of deleted documents per segment until it decides a merge needs to happen automatically in the background.
if you want to know more about this, you should check out the tiered merge policy.
Hope this helps!
--Alex