title: How to get total number of documents in Marklogic database?
body: <p>I have around 20 lacs documents in Marklogic Database. I want the total number of documents in my search application for pagination. For getting the totals I am using</p>
  
  <pre><code>xdmp:estimate(cts:search(doc(), $query))
  </code></pre>
  
  <p>where $query is the combination of various queries combined in <code>cts:and-query</code>.  But I am not getting the correct total. When $query is blank it shows much higher count than the total number of documents in the database. When I use </p>
  
  <pre><code>xdmp:estimate(doc())
  </code></pre>
  
  <p>it shows me the correct total but it would be static total which would not change according to the query. I want the total according to the results returned in response to a particular query that's why I passed $query as an argument but it is not showing correct total. <code>fn:count()</code> shows correct total but when the number of documents are around 20 lacs then <code>fn:count()</code> does not work because <code>fn:count()</code> is slower than <code>xdmp:estimate()</code>.  </p>
  
  <p>Please help me to get the correct total number of documents returned in response to the search term entered by the user.</p>