在 marklogic 中使用 cts:collection-match 限制结果

limit the result using cts:collection-match in marklogic

我在 collection 秒内有 2 collection 秒。

这有 10 个 id 为

的文档
test1_1
test1_2
.....
test1_10

这里有20个文档,id如下

test2_1
test2_2
.....
test2_20

查询:

let $result := cts:collection-match("/test/*")
let $id :=(
  fn:distinct-values(
    for $doc in fn:collection(result)
    order by $doc//timestamp descending
    return $doc//timestamp/text()
  )[1 to 5]
)
return $id

我想 return 每个 collection 时间戳降序的 前 5 个文档,但它 return 只有 5 个文档不是10 个,即每个 collection

的前 5 名

$result 是大于一个项目的序列时,写 for $doc in fn:collection($result) 会将来自多个集合的所有文档聚合到一个序列中。您需要先遍历集合,然后遍历每个集合中的值,有序且有限。

let $collections := cts:collection-match("/test/*")
let $id :=         
    for $collection in $collections
    return 
      fn:distinct-values(
        for $doc in fn:collection($collection)
        order by $doc//timestamp descending
           return $doc//timestamp/string()
      )[1 to 5]
return $id