Ambuj Jauhari
Ambuj Jauhari

Reputation: 1299

How to list all the URI of all documents in a directory in marklogic

Hi how can i retrieve the URI of all the documents in directory. I create the below xquery to achieve that but that doesn't helps.

for $i in xdmp:directory("/Test/performance/results/","infinity")
let $k := document-uri(fn:doc($i))
return <li>{$k}</li>

Upvotes: 4

Views: 3463

Answers (3)

mblakele
mblakele

Reputation: 7842

For efficiency you should use the URI lexicon.

cts:uris((), (), cts:directory-query("/Test/performance/results/","infinity"))

See https://docs.marklogic.com/cts:uris for documentation.

Upvotes: 12

DALDEI
DALDEI

Reputation: 3732

The posters XQuery is incorrect because $i is already a document not a URI so running fn:doc($i) is not going to work.

Adam's solution will work (to infinate depth) but can be very slow as it requires fetching every document. On large databases thats very slow.

If you enable the URI Lexicon then you can do much better. Here is the source for the xmlsh marklogic "ls" command ... embedded in it is fairly simple XQuery.

https://github.com/DALDEI/xmlsh/blob/master/extensions/marklogic/src/org/xmlsh/marklogic/ls.xsh

The relevent code:

xquery version "1.0-ml";
declare variable $root external;
for $p in fn:distinct-values( 
    for $d in cts:uris($root,"document", 
          if( $root eq "" ) then () else cts:directory-query($root,"infinity"))
    let $p := substring-after( $d , $root )
    where $d ne $root 
    return 
       if( contains($p,"/") ) then fn:concat($root , substring-before( $p , "/" ) , "/" )
    else 
       concat($root ,$p) 
    )
where $p eq "" or $p ne $root
return $p 

This lists only the direct children of a directory using the uri lexicon

This program is a bit more complex - it tries to use the uri lexicon but if it fails reverts to the xdmp:directory ... it lists either direct or recursive depending on the -r flag

https://github.com/DALDEI/xmlsh/blob/master/extensions/marklogic/src/org/xmlsh/marklogic/list.xsh

Upvotes: 0

adamretter
adamretter

Reputation: 3517

How about:

xdmp:directory("/Test/performance/results/","infinity") !
    <li>{fn:document-uri(.)}</li>

Upvotes: 1

Related Questions