从多个集合中搜索最新文档 [英] search latest document from multiple collection
问题描述
有2个收藏集/test/123和test/567,我想返回最新的 这两个收藏的文件.
There are 2 collections /test/123 and test/567 I want to return the latest document of both the collections.
let $a := cts:collection-match("/test/*")
for $t in $ a
let $latest :=(
for $doc in fn:collection( $t)
order by $doc//timestamp descending
return $doc)[1]
return fn:concat($latest//id/text(),",",$latest//timestamp/text())
- 从馆藏中获取最新文档是否更好?
-
我希望搜索两个集合后的最终查询结果在 时间戳的降序
- Is there a better to get the latest document from the collections
I want the final output of query after searching both the collection to be in descending order for timestamp
输出
1234, 2018-04-05T11:28:47.040Z 4567,2018-04-05T11:28:47.722Z
预期
4567,2018-04-05T11:28:47.722Z 1234, 2018-04-05T11:28:47.040Z
4567,2018-04-05T11:28:47.722Z 1234, 2018-04-05T11:28:47.040Z
推荐答案
I think the answer from @wst in your earlier question talking about just one collection (see https://stackoverflow.com/a/49678200/918496), can be adapted to work for multiple collections as well. It is mostly a matter of placing the parentheses differently. Moreover, fn:collection accepts a sequence too, so adapting the earlier solution would be almost trivial:
let $latest :=( for $doc in fn:collection( cts:collection-match("/test/*") ) order by $doc//timestamp descending return $doc )[1] return fn:concat($latest//id/text(),",",$latest//timestamp/text())
<update>
重新阅读问题(添加的Expected部分有所帮助,谢谢),我发现我可能误解了所需的输出.您不是要在所有匹配集合中寻找最新结果,而是想要每个集合中最新的结果(以降序显示).看起来有些不同,而且您之间相距不远.您只需要第二个BY子句:
Re-reading the question (the added Expected section helped, thanks), I see I may have misunderstood the desired output. You are not looking for the most recent result across all matching collections, but you want the most recent of each collection, shown in descending order. That looks slightly different, and you weren't very far off at all. You just need a second order by clause:
let $a := cts:collection-match("/test/*") for $t in $a let $latest := ( for $doc in fn:collection($t) order by $doc//timestamp descending return $doc )[1] order by $latest//timestamp descending return fn:concat($latest//id/text(),",",$latest//timestamp/text())
话虽如此,但使用MarkLogic可能会有更多高性能的方法.如果您的时间戳记上有dateTime范围索引,则可以允许MarkLogic利用该索引快速真正地找到升序或降序的第一个索引.最清晰的方法是将 cts:search 与
Having said that, there might be more performant ways of doing this with MarkLogic. If you have a dateTime range index on your timestamp, you could allow MarkLogic to make use of that to find the first in ascending or descending order real quickly. The clearest way of doing that is using cts:search with a cts:index-order argument. Something like:
let $a := cts:collection-match("/test/*") for $t in $a let $latest := cts:search( collection(), cts:collection-query($t), cts:index-order( cts:element-reference( fn:QName("","timestamp"), "type=dateTime" ), "descending" ) )[1] order by $latest//timestamp descending return fn:concat($latest//id/text(),",",$latest//timestamp/text())
</update>
HTH!
这篇关于从多个集合中搜索最新文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!