Reputation: 43
Within MarkLogic, I have tried, the following code. However, I am looking to have a procedure that will sort by year, month, day and then limit the results after. The code below limited the results then sorted by YY, MM, DD, which returned the wrong results.
xquery version "1.0-ml";
declare variable $query :=
cts:and-query
((
cts:element-word-query(xs:QName("journal-title"),"MyJournal"),
cts:element-word-query(xs:QName("subject"),"MyNews")));
declare function local:do-query(){
element xml {
for $i in cts:uris( (), ("limit=50"), $query)
let $item := doc($i)
order by ($item//pub-date/year) descending, ($item//pub-date/month) descending, ($item//pub-date/day) descending
return
element article {
element journal-title { $item//journal-title },
element sub {$item//article-meta//article-categories//subj-group[@subj-group-type="content-type"]//subject},
element title { $item//article-title },
element byline { $item//contrib/string-name },
element body { $item//body },
element images {
for $graphic in $item//body//graphic
return element image { ($graphic//@*[name()="xlink:href"]/data()) }},
element year {$item//pub-date/year/string()},
element month {$item//pub-date/month/string()},
element day {$item//pub-date/day/string()},
element doi { $item//article-id[@pub-id-type="doi"]/text() },
element doitrim {substring-after($item//article-id[@pub-id-type="doi"]/text(),"/")}
}
}
};
local:do-query()
Thanks in advance.
Upvotes: 3
Views: 240
Reputation: 66723
Your code is selecting the first 50 URIs that match the query criteria, loading those documents and sorting them.
It sounds as if you want to search for documents that match the query criteria with the results sorted by the year, month, day values, and select the first 50 from that sorted set.
You can use cts:search()
to search for the documents that match your query criteria and use a sequence of cts:index-order()
criteria with cts:element-reference()
to return the results sorted by year, month, and day, and then use a predicate to limit to just the first 50 of those sorted documents.
xquery version "1.0-ml";
declare variable $query :=
cts:and-query((
cts:element-word-query(xs:QName("journal-title"),"MyJournal"),
cts:element-word-query(xs:QName("subject"),"MyNews")
));
declare function local:do-query() {
element xml {
for $item in cts:search(fn:doc(), $query,
("unfiltered",
(
cts:index-order(cts:element-reference(xs:QName("year")), "descending"),
cts:index-order(cts:element-reference(xs:QName("month")), "descending"),
cts:index-order(cts:element-reference(xs:QName("day")), "descending")
)
)
)[1 to 50]
return
element article {
base-uri(),
element journal-title { $item//journal-title },
element sub {$item//article-meta//article-categories//subj-group[@subj-group-type="content-type"]//subject},
element title { $item//article-title },
element byline { $item//contrib/string-name },
element body { $item//body },
element images {
for $graphic in $item//body//graphic
return element image { ($graphic//@*[name()="xlink:href"]/data()) }},
element year {$item//pub-date/year/string()},
element month {$item//pub-date/month/string()},
element day {$item//pub-date/day/string()},
element doi { $item//article-id[@pub-id-type="doi"]/text() },
element doitrim {substring-after($item//article-id[@pub-id-type="doi"]/text(),"/")}
}
}
};
local:do-query()
Upvotes: 4