如何通过在Marklogic中动态排序或下降?

发布于 2025-02-14 00:44:26 字数 213 浏览 1 评论 0原文

let $sortelement := 'Salary' 
let $sortby := 'ascending'
for $doc in collection('employee')
order by $doc/*[local-name() eq $sortelement] $sortby
return $doc

此代码抛出和错误,做到这一点的正确方法是什么?

let $sortelement := 'Salary' 
let $sortby := 'ascending'
for $doc in collection('employee')
order by $doc/*[local-name() eq $sortelement] $sortby
return $doc

This code throws and error, what is the correct way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2025-02-21 00:44:26

如果您只是想在Flwor语句中动态地构建订单,则不能。正如迈克尔·凯(Michael Kay)在评论中指出的那样,您可以使用有条件的语句,并决定是否 reververs() 上升(默认)排序序列。

let $sortelement := 'Salary' 
let $sortby := 'ascending'
let $results :=
  for $doc in collection('employee')
  order by $doc/*[local-name() eq $sortelement] 
  return $doc
return
  if ($sortby eq 'descending')
  then reverse($results)
  else $results

根据您的收藏中有多少个文档,检索每个文档并对其进行排序不会扩展。它可能需要很长时间,并且可以超过扩展的树缓存的内存限制。

如果您在这些元素上有索引,则可以动态构建a cts:index-order () 并将其指定为cts:search()的第三个参数,以便以指定顺序返回它们:

let $sortelement := 'Salary'
let $sortby := 'ascending'
return 
  cts:search(doc(), 
    cts:collection-query("employee"),  
    cts:index-order(cts:element-reference(xs:QName($sortelement)), $sortby)
  )

If you are just looking to build the order by dynamically within a FLWOR statement, you can't. As Michael Kay points out in the comments, you could use a conditional statement and decide whether or not to reverse() the ascending (default) sorted sequence.

let $sortelement := 'Salary' 
let $sortby := 'ascending'
let $results :=
  for $doc in collection('employee')
  order by $doc/*[local-name() eq $sortelement] 
  return $doc
return
  if ($sortby eq 'descending')
  then reverse($results)
  else $results

Depending upon how many documents are in your collection, retrieving every document and sorting them won't scale. It can take a long time, and can exceed memory limits for expanded tree cache.

If you have indexes on those elements, then you can dynamically build a cts:index-order() and specify as the third parameter for cts:search() in order to get them returned in the specified order:

let $sortelement := 'Salary'
let $sortby := 'ascending'
return 
  cts:search(doc(), 
    cts:collection-query("employee"),  
    cts:index-order(cts:element-reference(xs:QName($sortelement)), $sortby)
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文