I'd suggest doing two cts:element-attribute-values() calls, both starting at 
the target date, one ascending and one descending.  Fetch just one value in 
each direction.  Decide which is closest.  Then retrieve the document using a 
cts:element-range-query with the "=" comparator to that closest value.

It's less declarative than yours but more efficient.

-jh-

On Sep 8, 2011, at 12:30 AM, Lee, David wrote:

> Suppose I have docs with a date attribute like
> <doc date="2011-01-10T10:59:33.00Z" ...>
>  
> And I want to retrieve the document(s) which are 'nearest' a given date in 
> time.
> I suppose the same logic would apply with integer or floating numbers.
> Suppose I have an attribute range index on the date value
>  
> Is there a cts:query that can do this directly ? or should I resort to xpaths 
> like maybe (shorthand)
>        let $d :=  the date
>                 (for $doc in  //doc
> where $doc/@date le $d or $doc/@date gt $d
>  order by $doc/@date
>                 return $doc)[1]
>  
> (probaly have to add some casts or other stuff ..
>  
> Just looking for a document thats closest in time to a given date.
> I know I could do a bucketed search but I'm not sure ahead of time how far 
> away in time the nearest doc is,  could be a second or could be an hour.
>  
> Suggestions welcome !
>  
> -David
>  
>  
> ----------------------------------------
> David A. Lee
> Senior Principal Software Engineer
> Epocrates, Inc.
> d...@epocrates.com
> 812-482-5224
>  
> _______________________________________________
> 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

Reply via email to