Hi Rachel, Just to give you an idea how you could replace a search with a lookup:
xquery version "1.0-ml"; import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy"; for $x in cts:search(xdmp:document-properties(), dls:documents-query() ) let $y := cts:element-values(xs:QName("dls:version-id"), (), ("properties"), cts:and-query(( cts:element-value-query( xs:QName("dls:latest"),"true", (),0), cts:element-value-query( xs:QName("dls:document-uri"), base-uri($x), (),0) )) ) return <content>{$x/*:properties/*[not(namespace-uri() = 'http://marklogic.com/xdmp/dls')]}<version>{$y}</version></content> Cheers, Geert > -----Oorspronkelijk bericht----- > Van: Geert J. [mailto:geert.jos...@dayon.nl] > Verzonden: vrijdag 7 februari 2014 0:59 > Aan: MarkLogic Developer Discussion > Onderwerp: RE: [MarkLogic Dev General] Efficient way to get the version > number of latest dls managed document > > Hi Rachel, > > Just to be sure, I compared the dls library between ML 6 and ML 7, but > apart from some small changes related to external users (LDAP related > things, new in ML 7), the library was pretty much unchanged. > > I ran a small test case, to see more clearly what happens. So, in short, > there are always two copies having latest 'true', the main doc, and the > latest version doc. And only the latter has indeed the version-id. So, > yes, you will need to pull properties from two fragments together to get > the result you are looking for. > > There isn't a join like you might be used to in SQL, but it is still very > well possible to pull things together. You will just need to do multiple > searches, or if you are only looking for values, do multiple lexicon look > ups. The more blunt approach would be to do something like: > > for $x in cts:search(xdmp:document-properties(), > dls:documents-query() > ) > let $y := cts:search(xdmp:document-properties(), > cts:and-query(( > cts:element-value-query( > xs:QName("dls:latest"),"true", > (),0), > cts:element-value-query( > xs:QName("dls:document-uri"), > base-uri($x), > (),0) > )) > ) > return > <content>{$x, $y}</content> > > I guess it essentially resembles your approach. I can imagine such > approach could get slow as the database grows. > > But as said, you could replace the inner cts:search by lexicon lookups if > you are only interested in the version-id, which is indexed afaik. And if > your other properties are atomic, you might be able to compose those using > lexicon lookups as well, by adding range indexes on them. That would avoid > disk io all together... > > Kind regards, > Geert > > > -----Oorspronkelijk bericht----- > > Van: general-boun...@developer.marklogic.com [mailto:general- > > boun...@developer.marklogic.com] Namens Rachel Wilson > > Verzonden: donderdag 6 februari 2014 14:01 > > Aan: MarkLogic Developer Discussion > > Onderwerp: Re: [MarkLogic Dev General] Efficient way to get the version > > number of latest dls managed document > > > > Thanks for the reply Geert :) > > > > We're using ML6. > > > > The version-id property does exist but only on the "version" versions of > > documents (for want of a better way to put it). The "master" document > > contains a property that we're interested in and our ultimate query > result > > needs to include the property on that master document as well as the > > latest version number. > > > > <result> > > > > <uri>/project/lappingpublish/content/243e2f5f-f2c9-4f9d-89bb- > > 491163cd0e00</ > > uri> (:this one is what we're calling the "master" document:) > > <dls:version xmlns:dls="http://marklogic.com/xdmp/dls"> > > <dls:latest>true</dls:latest> > > </dls:version> > > <prop:userDefinedProperty>interestingValue<prop:userDefinedProperty> > > </result> > > <result> > > > > <uri>/project/lappingpublish/content/243e2f5f-f2c9-4f9d-89bb- > > 491163cd0e00_2 > > 43e2f5f-f2c9-4f9d-89bb-491163cd0e00_versions/1-243e2f5f-f2c9-4f9d-89bb- > > 4911 > > 63cd0e00</uri> > > <dls:version xmlns:dls="http://marklogic.com/xdmp/dls"> > > <dls:version-id>1</dls:version-id> > > <dls:latest>false</dls:latest> > > </dls:version> > > </result> > > ... > > <result> > > > > <uri>/project/lappingpublish/content/243e2f5f-f2c9-4f9d-89bb- > > 491163cd0e00_2 > > 43e2f5f-f2c9-4f9d-89bb-491163cd0e00_versions/4-243e2f5f-f2c9-4f9d-89bb- > > 4911 > > 63cd0e00</uri> > > <dls:version xmlns:dls="http://marklogic.com/xdmp/dls"> > > <dls:version-id>4</dls:version-id> > > <dls:latest>true</dls:latest> > > </dls:version> > > </result> > > > > > > > > So we have a function at the moment that looks like below, but it's > > getting slow for large projects because we need to run something similar > > to you els-latest-version function and map lookup hundreds of thousands > of > > times. I guess I was hoping there was an optimised dls function similar > > to dls:documents-query() that I'd missed somewhere. > > > > Eg. > > > > declare function local:get-file-ids($projectId) { > > let $baseUri := concat("/project/",$projectId,"/content/") > > let $fileIdLookup := map:map() > > > > (: ML doesnt do joins between docs and so in order to lookup the > fileid > > property from master doc later we'll need a map. :) > > let $x := for $docProp in > cts:search(xdmp:directory-properties($baseUri), > > > > cts:properties-query(cts:element-value-query(xs:QName("prop:fileId"), > "*", > > "wildcarded")), > > "unfiltered") > > return map:put($fileIdLookup, base-uri($docProp), > > $docProp/prop:properties/prop:fileId/text()) > > > > (: find latest undeleted document including version number, can't use > > dls:documents-query() because version id only lives on versioned doc :) > > let $results := cts:search(xdmp:directory-properties($baseUri, > > "infinity"), > > cts:and-query( > > > > (cts:properties-query(cts:element-value-query(xs:QName("dls:latest"), > > "true")), > > > > cts:properties-query(cts:element-value-query(xs:QName("dls:version-id"), > > "*", "wildcarded")), > > > > cts:not-query(cts:properties-query(cts:element-value- > > query(xs:QName("prop:d > > eletedBy"), "*", "wildcarded"))) > > )), "unfiltered") > > > > (: now join it all together :) > > for $docProp in $results > > let $mainDoc := > $docProp/prop:properties/dls:version/dls:document-uri > > return <content> > > <id>{local:get-content-id-from-resultset($mainDoc/string())}</id> > > <fileId>{map:get($fileIdLookup, $mainDoc)}</fileId> > > > > > <version>{$docProp/prop:properties/dls:version/dls:version-id/string()}</v > e > > rsion> > > </content> > > }; > > > > > > > > > > > > > > -----Original Message----- > > From: "Geert J." <geert.jos...@dayon.nl> > > Reply-To: MarkLogic Developer Discussion > > <general@developer.marklogic.com> > > Date: Thursday, 6 February 2014 07:41 > > To: MarkLogic Developer Discussion <general@developer.marklogic.com> > > Subject: Re: [MarkLogic Dev General] Efficient way to get the version > > number of latest dls managed document > > > > Hi Rachel, > > > > Apologies for the late reply. Looking at the dls library code suggests > > that the version-id property should normally be present: > > > > declare private function dls-latest-version( > > $uri as xs:string > > ) as xs:unsignedInt > > { > > if (fn:exists(xdmp:directory-properties(version-directory($uri)))) > > then > > let $latest := > > cts:search( > > xdmp:document-properties(), > > cts:and-query(( > > cts:element-value-query( > > xs:QName("dls:latest"),"true", > > (),0), > > cts:element-value-query( > > xs:QName("dls:document-uri"),$uri, > > ("exact"),0))), > > ("score-simple"),0) > > return > > if ($latest) > > then $latest/prop:properties/dls:version/dls:version-id > > else 0 > > else 0 > > }; > > > > What version of MarkLogic are you running? > > > > Kind regards, > > Geert > > > > > -----Oorspronkelijk bericht----- > > > Van: general-boun...@developer.marklogic.com [mailto:general- > > > boun...@developer.marklogic.com] Namens Rachel Wilson > > > Verzonden: vrijdag 24 januari 2014 16:14 > > > Aan: MarkLogic Developer Discussion > > > Onderwerp: Re: [MarkLogic Dev General] Efficient way to get the > version > > > number of latest dls managed document > > > > > > I was just wondering if anyone had any pointers in relation to this > > > question. It's been a busy week on the list and it's only a small > > > question I realise, but just in case it slipped through the net... > > > > > > -----Original Message----- > > > From: Rachel Wilson <rachel.wil...@bbc.co.uk> > > > Date: Wednesday, 22 January 2014 15:04 > > > To: MarkLogic Developer Discussion <general@developer.marklogic.com> > > > Subject: Efficient way to get the version number of latest dls managed > > > document > > > > > > What is the most efficient way of getting back the version number of > the > > > latest dls managed document, in a cts:search or similar, given that it > > is > > > not possible to join two documents? > > > > > > It would be easy if the current document actually showed the > > > <dls:version-id>2</dls:version-id> element (just like the previous > > > versions), but it doesn't. It only shows <dls:latest>true</dls:latest> > > > > > > E.g. Dls properties of latest revision (what we tend to call the > > "master" > > > document) at /project/myproject/content/a > > > > > > <http://bgb01xuev1003.national.core.bbc.co.uk/EnterpriseVault/ViewMessag > > > e.a > > > > > > sp?VaultId=18D759FC5FAC19A4B9ED689AE23969ADD1110000BGB01XUEV100 > > > 1&SavesetId= > > > > > > 201312147013497~201309131531480000~Z~607DFE45035DC771F5921AC26A > > > EFFFB1>: > > > <dls:version xmlns:dls="http://marklogic.com/xdmp/dls"> > > > <dls:latest>true</dls:latest> > > > <dls:created>2013-09-13T15:13:57.225869+01:00</dls:created> > > > <dls:author>10428144080895388018</dls:author> > > > <dls:annotation/> > > > <dls:deleted>false</dls:deleted> > > > </dls:version> > > > > > > > > > Dls properties of a previous version at > > > /project/myproject/content/a_a_versions/1-a > > > > > > <http://bgb01xuev1003.national.core.bbc.co.uk/EnterpriseVault/ViewMessag > > > e.a > > > > > > sp?VaultId=18D759FC5FAC19A4B9ED689AE23969ADD1110000BGB01XUEV100 > > > 1&SavesetId= > > > > > > 201312147013497~201309131531480000~Z~607DFE45035DC771F5921AC26A > > > EFFFB1> > > > <dls:version xmlns:dls="http://marklogic.com/xdmp/dls"> > > > <dls:version-id>1</dls:version-id> > > > > > > <dls:document-uri>/project/testapi-editing- > > > activity/content/a</dls:document > > > -uri> > > > <dls:latest>false</dls:latest> > > > <dls:created>2013-09-13T15:13:55.905791+01:00</dls:created> > > > <dls:replaced>2013-09-13T15:13:56.787683+01:00</dls:replaced> > > > <dls:author>10428144080895388018</dls:author> > > > <dls:annotation/> > > > <dls:deleted>false</dls:deleted> > > > </dls:version> > > > > > > > > > Specifically, I'd like to write a query to retrieve some user defined > > > property data from the properties of the "master" document as well as > > the > > > version number of the latest version. > > > > > > This won't work because the properties for the document returned by > > > dls:documents-query() doesn't have the version number. > > > --- > > > for $x in cts:search(xdmp:document-properties(), > > > cts:and-query(( > > > dls:documents-query(), > > > )) > > > ) > > > > > > return > > > <content> > > > > > > > > > <fileId>{$x:/prop:properties/prop:myuserdefinedproperty/string()}</fileId> > > > > > > > > > <version>{$x/prop:properties/dls:version/dls:version-id/string()}</version > > > > > > </content> > > > --- > > > > > > And this won't work because I also need the property in the "master" > dls > > > document that doesn't exist on the dls versions properties > > > --- > > > for $x in cts:search(xdmp:document-properties(), > > > cts:and-query(( > > > > > cts:element-value-query(xs:QName("dls:latest"), > > > "true"), > > > > > > cts:element-value-query(xs:QName("dls:version-id"), "*", > "wildcarded"), > > > )) > > > ) > > > > > > return > > > <content> > > > > > > > > > <fileId>{$x:/prop:properties/prop:myuserdefinedproperty/string()}</fileId> > > > > > > > > > <version>{$x/prop:properties/dls:version/dls:version-id/string()}</version > > > > > > </content> > > > --- > > > > > > I suspect that some might suggest denormalising the user defined > > property > > > (would they?) but for the moment I'm stuck with the data structure as > it > > > is. > > > > > > Many thanks, > > > Rachel > > > > > > > > > > > > > > > ----------------------------- > > > http://www.bbc.co.uk > > > This e-mail (and any attachments) is confidential and > > > may contain personal views which are not the views of the BBC unless > > > specifically stated. > > > If you have received it in > > > error, please delete it from your system. > > > Do not use, copy or disclose the > > > information in any way nor act in reliance on it and notify the sender > > > immediately. > > > Please note that the BBC monitors e-mails > > > sent or received. > > > Further communication will signify your consent to > > > this. > > > ----------------------------- > > > _______________________________________________ > > > General mailing list > > > General@developer.marklogic.com > > > http://developer.marklogic.com/mailman/listinfo/general > > _______________________________________________ > > General mailing list > > General@developer.marklogic.com > > http://developer.marklogic.com/mailman/listinfo/general > > > > > > > > ----------------------------- > > http://www.bbc.co.uk > > This e-mail (and any attachments) is confidential and > > may contain personal views which are not the views of the BBC unless > > specifically stated. > > If you have received it in > > error, please delete it from your system. > > Do not use, copy or disclose the > > information in any way nor act in reliance on it and notify the sender > > immediately. > > Please note that the BBC monitors e-mails > > sent or received. > > Further communication will signify your consent to > > this. > > ----------------------------- > > _______________________________________________ > > General mailing list > > General@developer.marklogic.com > > http://developer.marklogic.com/mailman/listinfo/general _______________________________________________ General mailing list General@developer.marklogic.com http://developer.marklogic.com/mailman/listinfo/general