title: How does Elasticsearch 7 track_total_hits improve query speed?
body: <p>I recently upgraded from Elasticsearch 6 to 7 and stumbled across the 10000 hits limit.</p>
  <p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#track-total-hits-10000-default" rel="noreferrer">Changelog</a>, <a href="https://www.elastic.co/guide/en/elasticsearch/reference/master/search-your-data.html#track-total-hits" rel="noreferrer">Documentation</a>, and I also found a single <a href="https://ebaytech.berlin/stop-tracking-total-hits-2f63bb5fb6f5" rel="noreferrer">blog post from a company</a> that tried this new feature and measured their performance gains.</p>
  <p>But I'm still not sure how and why this feature works. Or does it only improve performance under special circumstances?</p>
  <p>Especially when sorting is involved, I can't get my head around it. Because (at least in my world) when sorting a collection you have to visit every document, and that's exactly what they are trying to avoid according to the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/master/search-your-data.html#track-total-hits" rel="noreferrer">Documentation</a>: <em>&quot;Generally the total hit count can’t be computed accurately without visiting all matches, which is costly for queries that match lots of documents.&quot;</em></p>
  <p>Hopefully someone can explain how things work under the hood and which important point I am missing.</p>