As far as the order-by automatically casting to xs:date and using a range index, in general, that will happen if there is a schema referenced in the documents declaring that element is of type xs:date. That is likely the "magic" that Mike is seeing.
-Danny From: [email protected] [mailto:[email protected]] On Behalf Of Geert Josten Sent: Thursday, July 12, 2012 9:02 AM To: MarkLogic Developer Discussion Subject: Re: [MarkLogic Dev General] sorting by date I'd be curious about the metrics. The index is not used here to retrieve fragments, but for sorting.. Grtz Van: [email protected]<mailto:[email protected]> [mailto:[email protected]<mailto:[email protected]>] Namens Ryan Dew Verzonden: donderdag 12 juli 2012 16:46 Aan: MarkLogic Developer Discussion Onderwerp: Re: [MarkLogic Dev General] sorting by date That actually makes sense to to me since the empty values wouldn't be in the index. if you need it so it includes empty and shows them first you might want to try separating the queries, so one query returning the documents without sortdate and a second query returning the items ascending like: ( for $doc in cts:search(fn:collection(), cts:and-query(( cts:directory-query ("/product/"), cts:not-query(cts:element-query(fn:QName("","sortdate"),cts:and-query(()))) )) ) return ... , for $doc in xdmp:directory ("/product/")[.//sortdate] order by $doc//sortdate ascending return ... ) On Thu, Jul 12, 2012 at 8:11 AM, Mike Sokolov <[email protected]<mailto:[email protected]>> wrote: Thanks for the response, Geert and Ryan. Actually it turns out that there is some magic afoot, because the cast is not actually needed. I think it used to be in older MarkLogic versions. xdmp:query-trace(true()), ( for $doc in xdmp:directory ("/product/") order by $doc//sortdate descending empty least return ... )[1 to 20] runs fast, with the index (23 msec as opposed to 9+ sec when the 50,000 docs need to be scanned) However, interestingly, if you change the order (ascending instead of descending) so that the empty items sort first, then the index is not used, and the query is even slower (14+ seconds) On 07/12/2012 08:38 AM, Geert Josten wrote: > Hi Mike, > > Did you try: > > order by $doc//sort-date/xs:date(.) > > ? > > Kind regards, > Geert > > >> -----Oorspronkelijk bericht----- >> Van: >> [email protected]<mailto:[email protected]> >> [mailto:general-<mailto:general-> >> [email protected]<mailto:[email protected]>] >> Namens Michael Sokolov >> Verzonden: donderdag 12 juli 2012 14:29 >> Aan: MarkLogic Developer Discussion >> Onderwerp: [MarkLogic Dev General] sorting by date >> >> I have a date-valued element range index. I want to use it for >> sorting. When I do that using an expression like this: >> >> order by xs:date($doc//sort-date) >> >> I get errors when the $doc sequence contains documents that have no >> sort-date. >> >> What is the recommended approach to handling nulls when sorting? Ideally >> we would want them to sort last. >> >> If I just use >> >> order by $doc//sort-date >> >> will the index come in to play? >> >> -Mike Sokolov >> _______________________________________________ >> General mailing list >> [email protected]<mailto:[email protected]> >> http://developer.marklogic.com/mailman/listinfo/general >> > _______________________________________________ > General mailing list > [email protected]<mailto:[email protected]> > http://developer.marklogic.com/mailman/listinfo/general > _______________________________________________ General mailing list [email protected]<mailto:[email protected]> http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________ General mailing list [email protected] http://developer.marklogic.com/mailman/listinfo/general
