Get a count of how many documents are in each directory.
Applies to MarkLogic versions 7+
declare function local:map-uris($uris as xs:string*) { let $map := map:map() let $_ := for $uri in $uris let $toks := fn:tokenize($uri, "/") for $t at $i in fn:subsequence($toks, 1, fn:count($toks) - 1) let $key := fn:string-join($toks[1 to $i], "/") || "/" let $count := (map:get($map, $key), 0)[1] return map:put($map, $key, ($count + 1) ) return $map }; local:map-uris(cts:uris())
Required Indexes:
MarkLogic allows you to segment content by collections and by directories. If you’re using directories, it can be helpful to know how many documents are in directory. This information might be used just by you, as a content manager, or presented to your end users.
The local:map-uris
function is given a sequence of URIs and returns a map. The keys of the map are the directories, starting with the root (“/”). A URI of “/a/b/c/1.xml” will contribute one count to “/”, “/a/”, “/a/b/”, and “a/b/c/”. As such, the count for “/” will match the total number of URIs passed in, assuming that all URIs are in the root directory.
Rather than running on all URIs, you can pass a query to cts:uris, which will run unfiltered. This could be useful in building a multi-tier facet to give users information about available content.
If you run this on a large database, there’s a good chance that it will time out. In that case, you might want to make separate calls for each of your top-level directories using cts:uri-match().
By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.