Nobody?

I thought of a way to calculate the buckets dynamically. I would reckon there 
should be a smarter way, but it seems to work alright, and fast enough..

        let $dates := fn:analyze-string($q, 'date:[^ ]+')/*:match/text()
        let $q :=
                search:parse($q,
                        <options 
xmlns="http://marklogic.com/appservices/search";>
                                <constraint name="date">
                                        <range type="xs:dateTime">
                                                <element 
ns="http://grtjn.nl/twitter/utils"; name="published"/>
                                                <attribute ns="" 
name="iso-date"/>
                                                {
                                                        for $date in $dates
                                                        let $date := 
fn:substring-after($date, 'date:')
                                                        return
                                                                if 
(fn:matches($date, '^\d\d\d\d$')) then
                                                                        <bucket 
name="{$date}" ge="{$date}-01-01T00:00:00Z" lt="{fn:number($date) + 
1}-01-01T00:00:00Z">{$date}</bucket>
                                                                else if 
(fn:matches($date, '^\d\d\d\d-\d\d\d\d$')) then
                                                                        let 
$start-year := fn:substring-before($date, '-')
                                                                        let 
$end-year := fn:substring-after($date, '-')
                                                                        return
                                                                                
<bucket name="{$date}" ge="{$start-year}-01-01T00:00:00Z" 
lt="{fn:number($end-year) + 1}-01-01T00:00:00Z">{$date}</bucket>
                                                                else if 
(fn:matches($date, '^\d\d\d\d\d\d$')) then
                                                                        let 
$year := fn:substring($date, 1, 4)
                                                                        let 
$month := fn:substring($date, 5, 2)
                                                                        let 
$end-date := xs:dateTime(fn:concat($year, "-", $month, "-01T00:00:00Z")) + 
xs:yearMonthDuration ("P1M")
                                                                        return
                                                                                
<bucket name="{$date}" ge="{$year}-{$month}-01T00:00:00Z" 
lt="{$end-date}">{$date}</bucket>


(etc..)

Feedback welcome..

Kind regards,
Geert

-----Oorspronkelijk bericht-----
Van: [email protected] 
[mailto:[email protected]] Namens Geert Josten
Verzonden: woensdag 10 augustus 2011 17:25
Aan: General MarkLogic Developer Discussion ([email protected])
Onderwerp: [MarkLogic Dev General] Date range constraint a la MarkMail

Hi,

I'd like to use the search library to support easy entry of date range search 
terms, like date:201101-201108 (as in MarkMail). How should my 'date' 
constraint look like? Any examples online?

Kind regards,
Geert
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to