marklogic Working with fn:count, xdmp:estimate and cts:frequency Counting documents when fragments are defined


Example

The XML document to consider in this example -

<a>
    <b>test-value</b>
    <d>fragment-d</d>
    <c-root>
        <d>fragment-d</d>
        <e>fragment-e</e>
    </c-root>
</a>

A fragment root is declared on <c-root>

If this is the only document in the database, xdmp:estimate and fn:count are going to behave differently -

xdmp:estimate(cts:search(fn:doc(), cts:element-value-query(xs:QName("d"), "fragment-d")))

Result of the above query will be 2 (Number of fragments)

fn:count(cts:search(fn:doc(), cts:element-value-query(xs:QName("d"), "fragment-d")))

Result of the above query will be 1 (Number of documents)

In terms of performance xdmp:estimate is much better than fn:count as it takes the advantages of indexes while resolving the search results