Find the URIs of binary documents.
xquery version "1.0-ml"; declare namespace qry = "https://marklogic.com/cts/query"; let $binary-term := xdmp:plan(/binary())//qry:term-query/qry:key/text() return cts:uris((), (), cts:term-query($binary-term))
This recipe returns a sequence of URIs for all the binary documents in the target database.
The implementation relies on how the
/binary() XPath is interpreted. xdmp:plan() tells us how MarkLogic sees a query. Part of the result is the final plan:
<qry:final-plan xmlns:qry="https://marklogic.com/cts/query"> <qry:and-query> <qry:term-query weight="0"> <qry:key>7908746777995149422</qry:key> <qry:annotation>document-format(binary)</qry:annotation> </qry:term-query> </qry:and-query> </qry:final-plan>
Notice the term-query part — in addition to storing a document, MarkLogic stores metadata about a document, and that metadata is queryable, too. Sometimes the trick is just figuring out how to specify that query. In this case, we use information from
xdmp:plan to get the job done.
You might ask, “Why not just use XPath, such as
/binary()?” This would also work, but it works by retrieving the binaries themselves. You could take it a step further with
/binary() ! fn:base-uri(.) to get just the URIs (which is what the recipe provides), but again, this requires loading up the actual documents and doing something with them. The beauty of the recipe is that it works on indexes.
There’s one sneaky bit with this recipe:
cts:term-query isn’t a published function. That means you should be careful where you use it, but for this recipe, it gets the job done.